use of org.apache.poi.xssf.usermodel.XSSFSheet in project poi by apache.
the class PieChartDemo method main.
public static void main(String[] args) throws Exception {
if (args.length < 2) {
usage();
return;
}
BufferedReader modelReader = new BufferedReader(new FileReader(args[1]));
XMLSlideShow pptx = null;
try {
// first line is chart title
String chartTitle = modelReader.readLine();
pptx = new XMLSlideShow(new FileInputStream(args[0]));
XSLFSlide slide = pptx.getSlides().get(0);
// find chart in the slide
XSLFChart chart = null;
for (POIXMLDocumentPart part : slide.getRelations()) {
if (part instanceof XSLFChart) {
chart = (XSLFChart) part;
break;
}
}
if (chart == null)
throw new IllegalStateException("chart not found in the template");
// embedded Excel workbook that holds the chart data
POIXMLDocumentPart xlsPart = chart.getRelations().get(0);
XSSFWorkbook wb = new XSSFWorkbook();
try {
XSSFSheet sheet = wb.createSheet();
CTChart ctChart = chart.getCTChart();
CTPlotArea plotArea = ctChart.getPlotArea();
CTPieChart pieChart = plotArea.getPieChartArray(0);
//Pie Chart Series
CTPieSer ser = pieChart.getSerArray(0);
// Series Text
CTSerTx tx = ser.getTx();
tx.getStrRef().getStrCache().getPtArray(0).setV(chartTitle);
sheet.createRow(0).createCell(1).setCellValue(chartTitle);
String titleRef = new CellReference(sheet.getSheetName(), 0, 1, true, true).formatAsString();
tx.getStrRef().setF(titleRef);
// Category Axis Data
CTAxDataSource cat = ser.getCat();
CTStrData strData = cat.getStrRef().getStrCache();
// Values
CTNumDataSource val = ser.getVal();
CTNumData numData = val.getNumRef().getNumCache();
// unset old axis text
strData.setPtArray(null);
// unset old values
numData.setPtArray(null);
// set model
int idx = 0;
int rownum = 1;
String ln;
while ((ln = modelReader.readLine()) != null) {
String[] vals = ln.split("\\s+");
CTNumVal numVal = numData.addNewPt();
numVal.setIdx(idx);
numVal.setV(vals[1]);
CTStrVal sVal = strData.addNewPt();
sVal.setIdx(idx);
sVal.setV(vals[0]);
idx++;
XSSFRow row = sheet.createRow(rownum++);
row.createCell(0).setCellValue(vals[0]);
row.createCell(1).setCellValue(Double.valueOf(vals[1]));
}
numData.getPtCount().setVal(idx);
strData.getPtCount().setVal(idx);
String numDataRange = new CellRangeAddress(1, rownum - 1, 1, 1).formatAsString(sheet.getSheetName(), true);
val.getNumRef().setF(numDataRange);
String axisDataRange = new CellRangeAddress(1, rownum - 1, 0, 0).formatAsString(sheet.getSheetName(), true);
cat.getStrRef().setF(axisDataRange);
// updated the embedded workbook with the data
OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream();
try {
wb.write(xlsOut);
} finally {
xlsOut.close();
}
// save the result
OutputStream out = new FileOutputStream("pie-chart-demo-output.pptx");
try {
pptx.write(out);
} finally {
out.close();
}
} finally {
wb.close();
}
} finally {
if (pptx != null)
pptx.close();
modelReader.close();
}
}
use of org.apache.poi.xssf.usermodel.XSSFSheet in project poi by apache.
the class SXSSFPicture method getRowHeightInPixels.
private float getRowHeightInPixels(int rowIndex) {
// THE FOLLOWING THREE LINES ARE THE MAIN CHANGE compared to the non-streaming version: use the SXSSF sheet,
// not the XSSF sheet (which never contais rows when using SXSSF)
XSSFSheet xssfSheet = getSheet();
SXSSFSheet sheet = _wb.getSXSSFSheet(xssfSheet);
Row row = sheet.getRow(rowIndex);
float height = row != null ? row.getHeightInPoints() : sheet.getDefaultRowHeightInPoints();
return height * XSSFShape.PIXEL_DPI / XSSFShape.POINT_DPI;
}
use of org.apache.poi.xssf.usermodel.XSSFSheet in project poi by apache.
the class SXSSFWorkbook method injectData.
protected void injectData(ZipEntrySource zipEntrySource, OutputStream out) throws IOException {
try {
ZipOutputStream zos = new ZipOutputStream(out);
try {
Enumeration<? extends ZipEntry> en = zipEntrySource.getEntries();
while (en.hasMoreElements()) {
ZipEntry ze = en.nextElement();
zos.putNextEntry(new ZipEntry(ze.getName()));
InputStream is = zipEntrySource.getInputStream(ze);
XSSFSheet xSheet = getSheetFromZipEntryName(ze.getName());
if (xSheet != null) {
SXSSFSheet sxSheet = getSXSSFSheet(xSheet);
InputStream xis = sxSheet.getWorksheetXMLInputStream();
try {
copyStreamAndInjectWorksheet(is, zos, xis);
} finally {
xis.close();
}
} else {
IOUtils.copy(is, zos);
}
is.close();
}
} finally {
zos.close();
}
} finally {
zipEntrySource.close();
}
}
use of org.apache.poi.xssf.usermodel.XSSFSheet in project poi by apache.
the class SXSSFPicture method getColumnWidthInPixels.
private float getColumnWidthInPixels(int columnIndex) {
XSSFSheet sheet = getSheet();
CTCol col = sheet.getColumnHelper().getColumn(columnIndex, false);
double numChars = col == null || !col.isSetWidth() ? DEFAULT_COLUMN_WIDTH : col.getWidth();
return (float) numChars * XSSFWorkbook.DEFAULT_CHARACTER_WIDTH;
}
use of org.apache.poi.xssf.usermodel.XSSFSheet in project poi by apache.
the class AligningCells method main.
public static void main(String[] args) throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFRow row = sheet.createRow(2);
row.setHeightInPoints(30);
for (int i = 0; i < 8; i++) {
//column width is set in units of 1/256th of a character width
sheet.setColumnWidth(i, 256 * 15);
}
createCell(wb, row, 0, HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM);
createCell(wb, row, 1, HorizontalAlignment.CENTER_SELECTION, VerticalAlignment.BOTTOM);
createCell(wb, row, 2, HorizontalAlignment.FILL, VerticalAlignment.CENTER);
createCell(wb, row, 3, HorizontalAlignment.GENERAL, VerticalAlignment.CENTER);
createCell(wb, row, 4, HorizontalAlignment.JUSTIFY, VerticalAlignment.JUSTIFY);
createCell(wb, row, 5, HorizontalAlignment.LEFT, VerticalAlignment.TOP);
createCell(wb, row, 6, HorizontalAlignment.RIGHT, VerticalAlignment.TOP);
//center text over B4, C4, D4
row = sheet.createRow(3);
centerAcrossSelection(wb, row, 1, 3, VerticalAlignment.CENTER);
// Write the output to a file
OutputStream fileOut = new FileOutputStream("xssf-align.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();
}
Aggregations