Search in sources :

Example 91 with Workbook

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

the class TestUnfixedBugs method testFormulaRecordAggregate.

@Test
public void testFormulaRecordAggregate() throws Exception {
    // fails at formula "=MEHRFACH.OPERATIONEN(E$3;$B$5;$D4)"
    Workbook wb = HSSFTestDataSamples.openSampleWorkbook("44958.xls");
    try {
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            Sheet sheet = wb.getSheetAt(i);
            assertNotNull(wb.getSheet(sheet.getSheetName()));
            sheet.groupColumn((short) 4, (short) 5);
            sheet.setColumnGroupCollapsed(4, true);
            sheet.setColumnGroupCollapsed(4, false);
            for (Row row : sheet) {
                for (Cell cell : row) {
                    try {
                        cell.toString();
                    } catch (Exception e) {
                        throw new Exception("While handling: " + sheet.getSheetName() + "/" + row.getRowNum() + "/" + cell.getColumnIndex(), e);
                    }
                }
            }
        }
    } finally {
        wb.close();
    }
}
Also used : Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) RecordFormatException(org.apache.poi.hssf.record.RecordFormatException) IOException(java.io.IOException) Test(org.junit.Test)

Example 92 with Workbook

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

the class TestHSSFWorkbook method testRewriteFileBug58480.

@Test
public void testRewriteFileBug58480() throws IOException {
    final File file = TempFile.createTempFile("TestHSSFWorkbook", ".xls");
    try {
        // create new workbook
        {
            final Workbook workbook = new HSSFWorkbook();
            final Sheet sheet = workbook.createSheet("foo");
            final Row row = sheet.createRow(1);
            row.createCell(1).setCellValue("bar");
            writeAndCloseWorkbook(workbook, file);
        }
        // edit the workbook
        {
            NPOIFSFileSystem fs = new NPOIFSFileSystem(file, false);
            try {
                DirectoryNode root = fs.getRoot();
                final Workbook workbook = new HSSFWorkbook(root, true);
                final Sheet sheet = workbook.getSheet("foo");
                sheet.getRow(1).createCell(2).setCellValue("baz");
                writeAndCloseWorkbook(workbook, file);
            } finally {
                fs.close();
            }
        }
    } finally {
        assertTrue(file.exists());
        assertTrue(file.delete());
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) Row(org.apache.poi.ss.usermodel.Row) TempFile(org.apache.poi.util.TempFile) File(java.io.File) InternalSheet(org.apache.poi.hssf.model.InternalSheet) Sheet(org.apache.poi.ss.usermodel.Sheet) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) BaseTestWorkbook(org.apache.poi.ss.usermodel.BaseTestWorkbook) Test(org.junit.Test)

Example 93 with Workbook

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

the class TestHSSFWorkbook method cellStylesLimit.

@Test
public void cellStylesLimit() throws IOException {
    Workbook wb = new HSSFWorkbook();
    int numBuiltInStyles = wb.getNumCellStyles();
    int MAX_STYLES = 4030;
    int limit = MAX_STYLES - numBuiltInStyles;
    for (int i = 0; i < limit; i++) {
        /* HSSFCellStyle style =*/
        wb.createCellStyle();
    }
    assertEquals(MAX_STYLES, wb.getNumCellStyles());
    try {
        /*HSSFCellStyle style =*/
        wb.createCellStyle();
        fail("expected exception");
    } catch (IllegalStateException e) {
        assertEquals("The maximum number of cell styles was exceeded. " + "You can define up to 4000 styles in a .xls workbook", e.getMessage());
    }
    assertEquals(MAX_STYLES, wb.getNumCellStyles());
    wb.close();
}
Also used : InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) BaseTestWorkbook(org.apache.poi.ss.usermodel.BaseTestWorkbook) Test(org.junit.Test)

Example 94 with Workbook

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

the class TestHSSFWorkbook method closeDoesNotModifyWorkbook.

@Test
public void closeDoesNotModifyWorkbook() throws IOException {
    final String filename = "SampleSS.xls";
    final File file = POIDataSamples.getSpreadSheetInstance().getFile(filename);
    Workbook wb;
    // File via POIFileStream (java.io)
    wb = new HSSFWorkbook(new POIFSFileSystem(file));
    assertCloseDoesNotModifyFile(filename, wb);
    // File via NPOIFileStream (java.nio)
    wb = new HSSFWorkbook(new NPOIFSFileSystem(file));
    assertCloseDoesNotModifyFile(filename, wb);
    // InputStream
    wb = new HSSFWorkbook(new FileInputStream(file));
    assertCloseDoesNotModifyFile(filename, wb);
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) OPOIFSFileSystem(org.apache.poi.poifs.filesystem.OPOIFSFileSystem) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) TempFile(org.apache.poi.util.TempFile) File(java.io.File) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) BaseTestWorkbook(org.apache.poi.ss.usermodel.BaseTestWorkbook) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 95 with Workbook

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

the class TestCellFormat method testApplyFormatHasThreePartsFirstTwoHaveCondition.

@Test
public void testApplyFormatHasThreePartsFirstTwoHaveCondition() throws Exception {
    // Create a workbook, row and cell to test with
    Workbook wb = new HSSFWorkbook();
    Sheet sheet = wb.createSheet();
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);
    CellFormat cf = CellFormat.getInstance("[>=100]0.00;[>=10]0.000;0.0000");
    cell.setCellValue(100);
    assertEquals("100.00", cf.apply(cell).text);
    cell.setCellValue(10);
    assertEquals("10.000", cf.apply(cell).text);
    cell.setCellValue(0);
    assertEquals("0.0000", cf.apply(cell).text);
    cell.setCellValue(-10);
    assertEquals("-10.0000", cf.apply(cell).text);
    cell.setCellValue("abc");
    assertEquals("abc", cf.apply(cell).text);
    wb.close();
}
Also used : Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) 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