Search in sources :

Example 11 with SXSSFWorkbook

use of org.apache.poi.xssf.streaming.SXSSFWorkbook in project poi by apache.

the class SXSSFITestDataProvider method openSampleWorkbook.

@Override
public Workbook openSampleWorkbook(String sampleFileName) {
    XSSFWorkbook xssfWorkbook = XSSFITestDataProvider.instance.openSampleWorkbook(sampleFileName);
    SXSSFWorkbook swb = new SXSSFWorkbook(xssfWorkbook);
    instances.add(swb);
    return swb;
}
Also used : SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook)

Example 12 with SXSSFWorkbook

use of org.apache.poi.xssf.streaming.SXSSFWorkbook in project poi by apache.

the class TestXSSFBugs method saveAndReloadReport.

private void saveAndReloadReport(Workbook wb, File outFile) throws IOException {
    // run some method on the font to verify if it is "disconnected" already
    //for(short i = 0;i < 256;i++)
    {
        Font font = wb.getFontAt((short) 0);
        if (font instanceof XSSFFont) {
            XSSFFont xfont = (XSSFFont) wb.getFontAt((short) 0);
            CTFontImpl ctFont = (CTFontImpl) xfont.getCTFont();
            assertEquals(0, ctFont.sizeOfBArray());
        }
    }
    FileOutputStream fileOutStream = new FileOutputStream(outFile);
    try {
        wb.write(fileOutStream);
    } finally {
        fileOutStream.close();
    }
    FileInputStream is = new FileInputStream(outFile);
    try {
        Workbook newWB = null;
        try {
            if (wb instanceof XSSFWorkbook) {
                newWB = new XSSFWorkbook(is);
            } else if (wb instanceof HSSFWorkbook) {
                newWB = new HSSFWorkbook(is);
            } else if (wb instanceof SXSSFWorkbook) {
                newWB = new SXSSFWorkbook(new XSSFWorkbook(is));
            } else {
                throw new IllegalStateException("Unknown workbook: " + wb);
            }
            assertNotNull(newWB.getSheet("test"));
        } finally {
            if (newWB != null) {
                newWB.close();
            }
        }
    } finally {
        is.close();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) CTFontImpl(org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTFontImpl) FileInputStream(java.io.FileInputStream) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 13 with SXSSFWorkbook

use of org.apache.poi.xssf.streaming.SXSSFWorkbook in project poi by apache.

the class TestXSSFSheet method runGetTopRow.

private void runGetTopRow(String file, boolean isXSSF, int... topRows) throws IOException {
    final Workbook wb = (isXSSF) ? XSSFTestDataSamples.openSampleWorkbook(file) : HSSFTestDataSamples.openSampleWorkbook(file);
    for (int si = 0; si < wb.getNumberOfSheets(); si++) {
        Sheet sh = wb.getSheetAt(si);
        assertNotNull(sh.getSheetName());
        assertEquals("Did not match for sheet " + si, topRows[si], sh.getTopRow());
    }
    // for XSSF also test with SXSSF
    if (isXSSF) {
        Workbook swb = new SXSSFWorkbook((XSSFWorkbook) wb);
        for (int si = 0; si < swb.getNumberOfSheets(); si++) {
            Sheet sh = swb.getSheetAt(si);
            assertNotNull(sh.getSheetName());
            assertEquals("Did not match for sheet " + si, topRows[si], sh.getTopRow());
        }
        swb.close();
    }
    wb.close();
}
Also used : SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) BaseTestXSheet(org.apache.poi.ss.usermodel.BaseTestXSheet) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFTestDataSamples.openSampleWorkbook(org.apache.poi.xssf.XSSFTestDataSamples.openSampleWorkbook) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook)

Example 14 with SXSSFWorkbook

use of org.apache.poi.xssf.streaming.SXSSFWorkbook in project poi by apache.

the class TestXSSFTable method bug56274.

@Test
public void bug56274() throws IOException {
    // read sample file
    XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("56274.xlsx");
    // read the original sheet header order
    XSSFRow row = wb1.getSheetAt(0).getRow(0);
    List<String> headers = new ArrayList<String>();
    for (Cell cell : row) {
        headers.add(cell.getStringCellValue());
    }
    // save the worksheet as-is using SXSSF
    File outputFile = TempFile.createTempFile("poi-56274", ".xlsx");
    SXSSFWorkbook outputWorkbook = new SXSSFWorkbook(wb1);
    FileOutputStream fos = new FileOutputStream(outputFile);
    outputWorkbook.write(fos);
    fos.close();
    outputWorkbook.close();
    // re-read the saved file and make sure headers in the xml are in the original order
    FileInputStream fis = new FileInputStream(outputFile);
    XSSFWorkbook wb2 = new XSSFWorkbook(fis);
    fis.close();
    CTTable ctTable = wb2.getSheetAt(0).getTables().get(0).getCTTable();
    CTTableColumn[] ctTableColumnArray = ctTable.getTableColumns().getTableColumnArray();
    assertEquals("number of headers in xml table should match number of header cells in worksheet", headers.size(), ctTableColumnArray.length);
    for (int i = 0; i < headers.size(); i++) {
        assertEquals("header name in xml table should match number of header cells in worksheet", headers.get(i), ctTableColumnArray[i].getName());
    }
    assertTrue(outputFile.delete());
    wb2.close();
    wb1.close();
}
Also used : ArrayList(java.util.ArrayList) CTTableColumn(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn) FileInputStream(java.io.FileInputStream) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) FileOutputStream(java.io.FileOutputStream) CTTable(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Cell(org.apache.poi.ss.usermodel.Cell) TempFile(org.apache.poi.util.TempFile) File(java.io.File) Test(org.junit.Test)

Example 15 with SXSSFWorkbook

use of org.apache.poi.xssf.streaming.SXSSFWorkbook in project poi by apache.

the class TestXSSFSheet method runGetLeftCol.

private void runGetLeftCol(String file, boolean isXSSF, int... topRows) throws IOException {
    final Workbook wb = (isXSSF) ? XSSFTestDataSamples.openSampleWorkbook(file) : HSSFTestDataSamples.openSampleWorkbook(file);
    for (int si = 0; si < wb.getNumberOfSheets(); si++) {
        Sheet sh = wb.getSheetAt(si);
        assertNotNull(sh.getSheetName());
        assertEquals("Did not match for sheet " + si, topRows[si], sh.getLeftCol());
    }
    // for XSSF also test with SXSSF
    if (isXSSF) {
        Workbook swb = new SXSSFWorkbook((XSSFWorkbook) wb);
        for (int si = 0; si < swb.getNumberOfSheets(); si++) {
            Sheet sh = swb.getSheetAt(si);
            assertNotNull(sh.getSheetName());
            assertEquals("Did not match for sheet " + si, topRows[si], sh.getLeftCol());
        }
        swb.close();
    }
    wb.close();
}
Also used : SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) BaseTestXSheet(org.apache.poi.ss.usermodel.BaseTestXSheet) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFTestDataSamples.openSampleWorkbook(org.apache.poi.xssf.XSSFTestDataSamples.openSampleWorkbook) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook)

Aggregations

SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)19 Test (org.junit.Test)7 Workbook (org.apache.poi.ss.usermodel.Workbook)6 Sheet (org.apache.poi.ss.usermodel.Sheet)5 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)4 FileInputStream (java.io.FileInputStream)3 Cell (org.apache.poi.ss.usermodel.Cell)3 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)2 BaseTestXSheet (org.apache.poi.ss.usermodel.BaseTestXSheet)2 ClientAnchor (org.apache.poi.ss.usermodel.ClientAnchor)2 CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)2 RichTextString (org.apache.poi.ss.usermodel.RichTextString)2 Row (org.apache.poi.ss.usermodel.Row)2 TempFile (org.apache.poi.util.TempFile)2