Search in sources :

Example 31 with Workbook

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

the class TestUnfixedBugs method testBug54084Unicode.

@Test
public void testBug54084Unicode() throws IOException {
    // sample XLSX with the same text-contents as the text-file above
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("54084 - Greek - beyond BMP.xlsx");
    verifyBug54084Unicode(wb);
    //        OutputStream baos = new FileOutputStream("/tmp/test.xlsx");
    //        try {
    //            wb.write(baos);
    //        } finally {
    //            baos.close();
    //        }
    // now write the file and read it back in
    XSSFWorkbook wbWritten = XSSFTestDataSamples.writeOutAndReadBack(wb);
    verifyBug54084Unicode(wbWritten);
    // finally also write it out via the streaming interface and verify that we still can read it back in
    SXSSFWorkbook swb = new SXSSFWorkbook(wb);
    Workbook wbStreamingWritten = SXSSFITestDataProvider.instance.writeOutAndReadBack(swb);
    verifyBug54084Unicode(wbStreamingWritten);
    wbWritten.close();
    swb.close();
    wbStreamingWritten.close();
    wb.close();
}
Also used : SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 32 with Workbook

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

the class TestUnfixedBugs method test54071.

@Test
public void test54071() throws Exception {
    Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("54071.xlsx");
    Sheet sheet = workbook.getSheetAt(0);
    int rows = sheet.getPhysicalNumberOfRows();
    System.out.println(">> file rows is:" + (rows - 1) + " <<");
    Row title = sheet.getRow(0);
    Date prev = null;
    for (int row = 1; row < rows; row++) {
        Row rowObj = sheet.getRow(row);
        for (int col = 0; col < 1; col++) {
            String titleName = title.getCell(col).toString();
            Cell cell = rowObj.getCell(col);
            if (titleName.startsWith("time")) {
                // here the output will produce ...59 or ...58 for the rows, probably POI is
                // doing some different rounding or some other small difference...
                System.out.println("==Time:" + cell.getDateCellValue());
                if (prev != null) {
                    assertEquals(prev, cell.getDateCellValue());
                }
                prev = cell.getDateCellValue();
            }
        }
    }
    workbook.close();
}
Also used : CTRow(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow) Row(org.apache.poi.ss.usermodel.Row) RichTextString(org.apache.poi.ss.usermodel.RichTextString) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Date(java.util.Date) Test(org.junit.Test)

Example 33 with Workbook

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

the class TestUnfixedBugs method testBug55752.

@Test
public void testBug55752() throws IOException {
    Workbook wb = new XSSFWorkbook();
    try {
        Sheet sheet = wb.createSheet("test");
        for (int i = 0; i < 4; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < 2; j++) {
                Cell cell = row.createCell(j);
                cell.setCellStyle(wb.createCellStyle());
            }
        }
        // set content
        Row row1 = sheet.getRow(0);
        row1.getCell(0).setCellValue("AAA");
        Row row2 = sheet.getRow(1);
        row2.getCell(0).setCellValue("BBB");
        Row row3 = sheet.getRow(2);
        row3.getCell(0).setCellValue("CCC");
        Row row4 = sheet.getRow(3);
        row4.getCell(0).setCellValue("DDD");
        // merge cells
        CellRangeAddress range1 = new CellRangeAddress(0, 0, 0, 1);
        sheet.addMergedRegion(range1);
        CellRangeAddress range2 = new CellRangeAddress(1, 1, 0, 1);
        sheet.addMergedRegion(range2);
        CellRangeAddress range3 = new CellRangeAddress(2, 2, 0, 1);
        sheet.addMergedRegion(range3);
        assertEquals(0, range3.getFirstColumn());
        assertEquals(1, range3.getLastColumn());
        assertEquals(2, range3.getLastRow());
        CellRangeAddress range4 = new CellRangeAddress(3, 3, 0, 1);
        sheet.addMergedRegion(range4);
        // set border
        RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, range1, sheet, wb);
        row2.getCell(0).getCellStyle().setBorderBottom(CellStyle.BORDER_THIN);
        row2.getCell(1).getCellStyle().setBorderBottom(CellStyle.BORDER_THIN);
        Cell cell0 = CellUtil.getCell(row3, 0);
        CellUtil.setCellStyleProperty(cell0, CellUtil.BORDER_BOTTOM, CellStyle.BORDER_THIN);
        Cell cell1 = CellUtil.getCell(row3, 1);
        CellUtil.setCellStyleProperty(cell1, CellUtil.BORDER_BOTTOM, CellStyle.BORDER_THIN);
        RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, range4, sheet, wb);
        // write to file
        OutputStream stream = new FileOutputStream(new File("C:/temp/55752.xlsx"));
        try {
            wb.write(stream);
        } finally {
            stream.close();
        }
    } finally {
        wb.close();
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) CTRow(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow) Row(org.apache.poi.ss.usermodel.Row) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) File(java.io.File) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 34 with Workbook

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

the class TestSXSSFSheet method overrideFlushedRows.

@Test
public void overrideFlushedRows() throws IOException {
    Workbook wb = new SXSSFWorkbook(3);
    try {
        Sheet sheet = wb.createSheet();
        sheet.createRow(1);
        sheet.createRow(2);
        sheet.createRow(3);
        sheet.createRow(4);
        thrown.expect(Throwable.class);
        thrown.expectMessage("Attempting to write a row[1] in the range [0,1] that is already written to disk.");
        sheet.createRow(1);
    } finally {
        wb.close();
    }
}
Also used : Sheet(org.apache.poi.ss.usermodel.Sheet) BaseTestXSheet(org.apache.poi.ss.usermodel.BaseTestXSheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 35 with Workbook

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

the class TestXSSFComment method testBug58175a.

@Ignore("Used for manual testing with opening the resulting Workbook in Excel")
@Test
public void testBug58175a() throws IOException {
    Workbook wb = new SXSSFWorkbook();
    try {
        Sheet sheet = wb.createSheet();
        Row row = sheet.createRow(1);
        Cell cell = row.createCell(3);
        cell.setCellValue("F4");
        Drawing<?> drawing = sheet.createDrawingPatriarch();
        CreationHelper factory = wb.getCreationHelper();
        // When the comment box is visible, have it show in a 1x3 space
        ClientAnchor anchor = factory.createClientAnchor();
        anchor.setCol1(cell.getColumnIndex());
        anchor.setCol2(cell.getColumnIndex() + 1);
        anchor.setRow1(row.getRowNum());
        anchor.setRow2(row.getRowNum() + 3);
        // Create the comment and set the text+author
        Comment comment = drawing.createCellComment(anchor);
        RichTextString str = factory.createRichTextString("Hello, World!");
        comment.setString(str);
        comment.setAuthor("Apache POI");
        /* fixed the problem as well 
             * comment.setColumn(cell.getColumnIndex());
             * comment.setRow(cell.getRowIndex());
             */
        // Assign the comment to the cell
        cell.setCellComment(comment);
        OutputStream out = new FileOutputStream("C:\\temp\\58175.xlsx");
        try {
            wb.write(out);
        } finally {
            out.close();
        }
    } finally {
        wb.close();
    }
}
Also used : BaseTestCellComment(org.apache.poi.ss.usermodel.BaseTestCellComment) Comment(org.apache.poi.ss.usermodel.Comment) CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Row(org.apache.poi.ss.usermodel.Row) RichTextString(org.apache.poi.ss.usermodel.RichTextString) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Ignore(org.junit.Ignore) 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