Search in sources :

Example 56 with XSSFWorkbook

use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.

the class SettingExternalFunction method main.

public static void main(String[] args) throws IOException {
    // or new HSSFWorkbook()
    Workbook wb = new XSSFWorkbook();
    // register the add-in
    wb.addToolPack(new BloombergAddIn());
    Sheet sheet = wb.createSheet();
    Row row = sheet.createRow(0);
    row.createCell(0).setCellFormula("BDP(\"GOOG Equity\",\"CHG_PCT_YTD\")/100");
    row.createCell(1).setCellFormula("BDH(\"goog us equity\",\"EBIT\",\"1/1/2005\",\"12/31/2009\",\"per=cy\",\"curr=USD\") ");
    row.createCell(2).setCellFormula("BDS(\"goog us equity\",\"top_20_holders_public_filings\") ");
    FileOutputStream out = new FileOutputStream("bloomberg-demo.xlsx");
    wb.write(out);
    out.close();
    wb.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook)

Example 57 with XSSFWorkbook

use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.

the class XSSFFileHandler method handleFile.

@Override
public void handleFile(InputStream stream, String path) throws Exception {
    // ignore password protected files
    if (POIXMLDocumentHandler.isEncrypted(stream))
        return;
    final XSSFWorkbook wb;
    // make sure the potentially large byte-array is freed up quickly again
    {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        IOUtils.copy(stream, out);
        final byte[] bytes = out.toByteArray();
        checkXSSFReader(OPCPackage.open(new ByteArrayInputStream(bytes)));
        wb = new XSSFWorkbook(new ByteArrayInputStream(bytes));
    }
    // use the combined handler for HSSF/XSSF
    handleWorkbook(wb);
    // TODO: some documents fail currently...
    //XSSFFormulaEvaluator evaluator = new XSSFFormulaEvaluator(wb);
    //evaluator.evaluateAll();
    // also verify general POIFS-stuff
    new POIXMLDocumentHandler().handlePOIXMLDocument(wb);
    // and finally ensure that exporting to XML works
    exportToXML(wb);
    // this allows to trigger a heap-dump at this point to see which memory is still allocated
    //HeapDump.dumpHeap("/tmp/poi.hprof", false);
    wb.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 58 with XSSFWorkbook

use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.

the class TestXSSFChartLegend method testLegendPositionAccessMethods.

@Test
public void testLegendPositionAccessMethods() throws IOException {
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet();
    Drawing<?> drawing = sheet.createDrawingPatriarch();
    ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 1, 1, 10, 30);
    Chart chart = drawing.createChart(anchor);
    ChartLegend legend = chart.getOrCreateLegend();
    legend.setPosition(LegendPosition.TOP_RIGHT);
    assertEquals(LegendPosition.TOP_RIGHT, legend.getPosition());
    wb.close();
}
Also used : ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) ChartLegend(org.apache.poi.ss.usermodel.charts.ChartLegend) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Chart(org.apache.poi.ss.usermodel.Chart) Test(org.junit.Test)

Example 59 with XSSFWorkbook

use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.

the class TestXSSFChartLegend method test_setOverlay_chartLegendSetToTrue_expectOverlayInitialValueSetToTrue.

@Test
public void test_setOverlay_chartLegendSetToTrue_expectOverlayInitialValueSetToTrue() throws IOException {
    // Arrange
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet();
    Drawing<?> drawing = sheet.createDrawingPatriarch();
    ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 1, 1, 10, 30);
    Chart chart = drawing.createChart(anchor);
    ChartLegend legend = chart.getOrCreateLegend();
    // Act
    legend.setOverlay(true);
    // Assert
    assertTrue(legend.isOverlay());
    wb.close();
}
Also used : ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) ChartLegend(org.apache.poi.ss.usermodel.charts.ChartLegend) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Chart(org.apache.poi.ss.usermodel.Chart) Test(org.junit.Test)

Example 60 with XSSFWorkbook

use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.

the class XSSFRowShifter method shiftFormula.

/**
     * Shift a formula using the supplied FormulaShifter
     *
     * @param row     the row of the cell this formula belongs to. Used to get a reference to the parent workbook.
     * @param formula the formula to shift
     * @param shifter the FormulaShifter object that operates on the parsed formula tokens
     * @return the shifted formula if the formula was changed,
     *         <code>null</code> if the formula wasn't modified
     */
private static String shiftFormula(Row row, String formula, FormulaShifter shifter) {
    Sheet sheet = row.getSheet();
    Workbook wb = sheet.getWorkbook();
    int sheetIndex = wb.getSheetIndex(sheet);
    final int rowIndex = row.getRowNum();
    XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create((XSSFWorkbook) wb);
    try {
        Ptg[] ptgs = FormulaParser.parse(formula, fpb, FormulaType.CELL, sheetIndex, rowIndex);
        String shiftedFmla = null;
        if (shifter.adjustFormula(ptgs, sheetIndex)) {
            shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs);
        }
        return shiftedFmla;
    } catch (FormulaParseException fpe) {
        // Log, but don't change, rather than breaking
        logger.log(POILogger.WARN, "Error shifting formula on row ", row.getRowNum(), fpe);
        return formula;
    }
}
Also used : FormulaParseException(org.apache.poi.ss.formula.FormulaParseException) XSSFEvaluationWorkbook(org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook) Ptg(org.apache.poi.ss.formula.ptg.Ptg) AreaErrPtg(org.apache.poi.ss.formula.ptg.AreaErrPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFEvaluationWorkbook(org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook)

Aggregations

XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)334 Workbook (org.apache.poi.ss.usermodel.Workbook)131 Sheet (org.apache.poi.ss.usermodel.Sheet)119 Test (org.junit.Test)108 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)82 FileOutputStream (java.io.FileOutputStream)81 Row (org.apache.poi.ss.usermodel.Row)74 Cell (org.apache.poi.ss.usermodel.Cell)68 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)66 IOException (java.io.IOException)65 FileInputStream (java.io.FileInputStream)63 ArrayList (java.util.ArrayList)51 File (java.io.File)46 ByteArrayInputStream (java.io.ByteArrayInputStream)36 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)35 FileNotFoundException (java.io.FileNotFoundException)29 CTChart (org.openxmlformats.schemas.drawingml.x2006.chart.CTChart)27 CellStyle (org.apache.poi.ss.usermodel.CellStyle)26 XSSFChart (org.apache.poi.xssf.usermodel.XSSFChart)26 XSSFDrawing (org.apache.poi.xssf.usermodel.XSSFDrawing)25