Search in sources :

Example 51 with Workbook

use of org.apache.poi.ss.usermodel.Workbook 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 52 with Workbook

use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.

the class SpreadsheetHandler method handleWorkbook.

public void handleWorkbook(Workbook wb) throws IOException {
    // try to access some of the content
    readContent(wb);
    // write out the file
    writeToArray(wb);
    // access some more content (we had cases where writing corrupts the data in memory)
    readContent(wb);
    // write once more
    ByteArrayOutputStream out = writeToArray(wb);
    // read in the written file
    Workbook read;
    try {
        read = WorkbookFactory.create(new ByteArrayInputStream(out.toByteArray()));
    } catch (InvalidFormatException e) {
        throw new IllegalStateException(e);
    }
    assertNotNull(read);
    readContent(read);
    extractEmbedded(read);
    modifyContent(read);
    read.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Workbook(org.apache.poi.ss.usermodel.Workbook)

Example 53 with Workbook

use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.

the class CellUtil method setFont.

/**
     * Take a cell, and apply a font to it
     *
     * @param cell the cell to set the alignment for
     * @param font The Font that you want to set.
     * @throws IllegalArgumentException if <tt>font</tt> and <tt>cell</tt> do not belong to the same workbook
     */
public static void setFont(Cell cell, Font font) {
    // Check if font belongs to workbook
    Workbook wb = cell.getSheet().getWorkbook();
    final short fontIndex = font.getIndex();
    if (!wb.getFontAt(fontIndex).equals(font)) {
        throw new IllegalArgumentException("Font does not belong to this workbook");
    }
    // Check if cell belongs to workbook
    // (checked in setCellStyleProperty)
    setCellStyleProperty(cell, FONT, fontIndex);
}
Also used : Workbook(org.apache.poi.ss.usermodel.Workbook)

Example 54 with Workbook

use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.

the class TestXSSFChartAxis method testGetChartAxisBug57362.

public void testGetChartAxisBug57362() {
    //Load existing excel with some chart on it having primary and secondary axis.
    final Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("57362.xlsx");
    final Sheet sh = workbook.getSheetAt(0);
    final XSSFSheet xsh = (XSSFSheet) sh;
    final XSSFDrawing drawing = xsh.createDrawingPatriarch();
    final XSSFChart chart = drawing.getCharts().get(0);
    final List<? extends XSSFChartAxis> axisList = chart.getAxis();
    assertEquals(4, axisList.size());
    assertNotNull(axisList.get(0));
    assertNotNull(axisList.get(1));
    assertNotNull(axisList.get(2));
    assertNotNull(axisList.get(3));
}
Also used : Sheet(org.apache.poi.ss.usermodel.Sheet) Workbook(org.apache.poi.ss.usermodel.Workbook)

Example 55 with Workbook

use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.

the class TestXSSFWorkbook method testBug56957CloseWorkbook.

@Test
public void testBug56957CloseWorkbook() throws Exception {
    File file = TempFile.createTempFile("TestBug56957_", ".xlsx");
    final Date dateExp = LocaleUtil.getLocaleCalendar(2014, 10, 9).getTime();
    try {
        // as the file is written to, we make a copy before actually working on it
        FileHelper.copyFile(HSSFTestDataSamples.getSampleFile("56957.xlsx"), file);
        assertTrue(file.exists());
        // read-only mode works!
        Workbook workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ));
        Date dateAct = workbook.getSheetAt(0).getRow(0).getCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
        assertEquals(dateExp, dateAct);
        workbook.close();
        workbook = null;
        workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ));
        dateAct = workbook.getSheetAt(0).getRow(0).getCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
        assertEquals(dateExp, dateAct);
        workbook.close();
        workbook = null;
        // now check read/write mode
        workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ_WRITE));
        dateAct = workbook.getSheetAt(0).getRow(0).getCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
        assertEquals(dateExp, dateAct);
        workbook.close();
        workbook = null;
        workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ_WRITE));
        dateAct = workbook.getSheetAt(0).getRow(0).getCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
        assertEquals(dateExp, dateAct);
        workbook.close();
        workbook = null;
    } finally {
        assertTrue(file.exists());
        assertTrue(file.delete());
    }
}
Also used : TempFile(org.apache.poi.util.TempFile) File(java.io.File) Date(java.util.Date) BaseTestXWorkbook(org.apache.poi.ss.usermodel.BaseTestXWorkbook) CTWorkbook(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Aggregations

Workbook (org.apache.poi.ss.usermodel.Workbook)319 Sheet (org.apache.poi.ss.usermodel.Sheet)224 Test (org.junit.Test)207 Cell (org.apache.poi.ss.usermodel.Cell)140 Row (org.apache.poi.ss.usermodel.Row)123 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)104 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)95 FileOutputStream (java.io.FileOutputStream)33 XSSFChart (org.apache.poi.xssf.usermodel.XSSFChart)32 ByteArrayInputStream (java.io.ByteArrayInputStream)30 FormulaEvaluator (org.apache.poi.ss.usermodel.FormulaEvaluator)30 XSSFDrawing (org.apache.poi.xssf.usermodel.XSSFDrawing)27 CTChart (org.openxmlformats.schemas.drawingml.x2006.chart.CTChart)27 File (java.io.File)26 CellStyle (org.apache.poi.ss.usermodel.CellStyle)25 InternalWorkbook (org.apache.poi.hssf.model.InternalWorkbook)24 LangYearFilterPagingRequest (org.devgateway.ocds.web.rest.controller.request.LangYearFilterPagingRequest)23 IOException (java.io.IOException)22 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)18 FileInputStream (java.io.FileInputStream)15