Search in sources :

Example 36 with Row

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

the class TestSXSSFWorkbook method testTemplateFile.

@Ignore("Just a local test for http://stackoverflow.com/questions/33627329/apache-poi-streaming-api-using-xssf-template")
@Test
public void testTemplateFile() throws IOException {
    XSSFWorkbook workBook = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx");
    SXSSFWorkbook streamingWorkBook = new SXSSFWorkbook(workBook, 10);
    Sheet sheet = streamingWorkBook.getSheet("Sheet1");
    for (int rowNum = 10; rowNum < 1000000; rowNum++) {
        Row row = sheet.createRow(rowNum);
        for (int cellNum = 0; cellNum < 700; cellNum++) {
            Cell cell = row.createCell(cellNum);
            cell.setCellValue("somevalue");
        }
        if (rowNum % 100 == 0) {
            System.out.print(".");
            if (rowNum % 10000 == 0) {
                System.out.println(rowNum);
            }
        }
    }
    FileOutputStream fos = new FileOutputStream("C:\\temp\\streaming.xlsx");
    streamingWorkBook.write(fos);
    fos.close();
    streamingWorkBook.close();
    workBook.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) Cell(org.apache.poi.ss.usermodel.Cell) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 37 with Row

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

the class TestSXSSFWorkbook method assertWorkbookDispose.

protected static void assertWorkbookDispose(SXSSFWorkbook wb) {
    int rowNum = 1000;
    int sheetNum = 5;
    for (int i = 0; i < sheetNum; i++) {
        Sheet sh = wb.createSheet("sheet" + i);
        for (int j = 0; j < rowNum; j++) {
            Row row = sh.createRow(j);
            Cell cell1 = row.createCell(0);
            cell1.setCellValue(new CellReference(cell1).formatAsString());
            Cell cell2 = row.createCell(1);
            cell2.setCellValue(i);
            Cell cell3 = row.createCell(2);
            cell3.setCellValue(j);
        }
    }
    for (Sheet sheet : wb) {
        SXSSFSheet sxSheet = (SXSSFSheet) sheet;
        assertTrue(sxSheet.getSheetDataWriter().getTempFile().exists());
    }
    assertTrue(wb.dispose());
    for (Sheet sheet : wb) {
        SXSSFSheet sxSheet = (SXSSFSheet) sheet;
        assertFalse(sxSheet.getSheetDataWriter().getTempFile().exists());
    }
}
Also used : Row(org.apache.poi.ss.usermodel.Row) CellReference(org.apache.poi.ss.util.CellReference) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell)

Example 38 with Row

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

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

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

the class TestSXSSFSheetAutoSizeColumn method createRowWithCellValues.

private static Cell createRowWithCellValues(final Sheet sheet, final int rowNumber, final String... cellValues) {
    Row row = sheet.createRow(rowNumber);
    int cellIndex = 0;
    Cell firstCell = null;
    for (final String cellValue : cellValues) {
        Cell cell = row.createCell(cellIndex++);
        if (firstCell == null) {
            firstCell = cell;
        }
        cell.setCellValue(cellValue);
    }
    return firstCell;
}
Also used : Row(org.apache.poi.ss.usermodel.Row) Cell(org.apache.poi.ss.usermodel.Cell)

Aggregations

Row (org.apache.poi.ss.usermodel.Row)316 Cell (org.apache.poi.ss.usermodel.Cell)230 Sheet (org.apache.poi.ss.usermodel.Sheet)179 Workbook (org.apache.poi.ss.usermodel.Workbook)125 Test (org.junit.Test)116 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)55 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)44 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)35 CellStyle (org.apache.poi.ss.usermodel.CellStyle)27 CellReference (org.apache.poi.ss.util.CellReference)22 ArrayList (java.util.ArrayList)21 FileOutputStream (java.io.FileOutputStream)20 IOException (java.io.IOException)17 XSSFColor (org.apache.poi.xssf.usermodel.XSSFColor)17 XSSFFont (org.apache.poi.xssf.usermodel.XSSFFont)17 HashMap (java.util.HashMap)16 RichTextString (org.apache.poi.ss.usermodel.RichTextString)16 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)16 List (java.util.List)14 FormulaEvaluator (org.apache.poi.ss.usermodel.FormulaEvaluator)14