Search in sources :

Example 66 with Workbook

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

the class TestXSSFSheetShiftRows method testBug53798.

/**  negative row shift causes corrupted data or throws exception */
@Test
public void testBug53798() throws IOException {
    // NOTE that for HSSF (.xls) negative shifts combined with positive ones do work as expected  
    Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53798.xlsx");
    Sheet testSheet = wb.getSheetAt(0);
    // 1) corrupted xlsx (unreadable data in the first row of a shifted group) already comes about  
    // when shifted by less than -1 negative amount (try -2)
    testSheet.shiftRows(3, 3, -2);
    // 2) attempt to create a new row IN PLACE of a removed row by a negative shift causes corrupted
    // xlsx file with  unreadable data in the negative shifted row. 
    // NOTE it's ok to create any other row.
    Row newRow = testSheet.createRow(3);
    Cell newCell = newRow.createCell(0);
    newCell.setCellValue("new Cell in row " + newRow.getRowNum());
    // 3) once a negative shift has been made any attempt to shift another group of rows 
    // (note: outside of previously negative shifted rows) by a POSITIVE amount causes POI exception: 
    // org.apache.xmlbeans.impl.values.XmlValueDisconnectedException.
    // NOTE: another negative shift on another group of rows is successful, provided no new rows in  
    // place of previously shifted rows were attempted to be created as explained above.
    // -- CHANGE the shift to positive once the behaviour of the above has been tested
    testSheet.shiftRows(6, 7, 1);
    Workbook read = XSSFTestDataSamples.writeOutAndReadBack(wb);
    wb.close();
    assertNotNull(read);
    Sheet readSheet = read.getSheetAt(0);
    verifyCellContent(readSheet, 0, "0.0");
    verifyCellContent(readSheet, 1, "3.0");
    verifyCellContent(readSheet, 2, "2.0");
    verifyCellContent(readSheet, 3, "new Cell in row 3");
    verifyCellContent(readSheet, 4, "4.0");
    verifyCellContent(readSheet, 5, "5.0");
    verifyCellContent(readSheet, 6, null);
    verifyCellContent(readSheet, 7, "6.0");
    verifyCellContent(readSheet, 8, "7.0");
    read.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) POITestCase.skipTest(org.apache.poi.POITestCase.skipTest) Test(org.junit.Test)

Example 67 with Workbook

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

the class TestXSSFSheetShiftRows method testBug56017.

/** Shifting rows with comment result - Unreadable content error and comment deletion */
@Test
public void testBug56017() throws IOException {
    Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56017.xlsx");
    Sheet sheet = wb.getSheetAt(0);
    Comment comment = sheet.getCellComment(new CellAddress(0, 0));
    assertNotNull(comment);
    assertEquals("Amdocs", comment.getAuthor());
    assertEquals("Amdocs:\ntest\n", comment.getString().getString());
    sheet.shiftRows(0, 1, 1);
    // comment in row 0 is gone
    comment = sheet.getCellComment(new CellAddress(0, 0));
    assertNull(comment);
    // comment is now in row 1
    comment = sheet.getCellComment(new CellAddress(1, 0));
    assertNotNull(comment);
    assertEquals("Amdocs", comment.getAuthor());
    assertEquals("Amdocs:\ntest\n", comment.getString().getString());
    Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
    wb.close();
    assertNotNull(wbBack);
    Sheet sheetBack = wbBack.getSheetAt(0);
    // comment in row 0 is gone
    comment = sheetBack.getCellComment(new CellAddress(0, 0));
    assertNull(comment);
    // comment is now in row 1
    comment = sheetBack.getCellComment(new CellAddress(1, 0));
    assertNotNull(comment);
    assertEquals("Amdocs", comment.getAuthor());
    assertEquals("Amdocs:\ntest\n", comment.getString().getString());
    wbBack.close();
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) Comment(org.apache.poi.ss.usermodel.Comment) Sheet(org.apache.poi.ss.usermodel.Sheet) Workbook(org.apache.poi.ss.usermodel.Workbook) POITestCase.skipTest(org.apache.poi.POITestCase.skipTest) Test(org.junit.Test)

Example 68 with Workbook

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

the class TestXSSFSheetShiftRows method bug59733.

// This test is written as expected-to-fail and should be rewritten
// as expected-to-pass when the bug is fixed.
//@Ignore("Bug 59733 - shiftRows() causes org.apache.xmlbeans.impl.values.XmlValueDisconnectedException")
@Test
public void bug59733() throws IOException {
    Workbook workbook = new XSSFWorkbook();
    Sheet sheet = workbook.createSheet();
    for (int r = 0; r < 4; r++) {
        sheet.createRow(r);
    }
    // Shift the 2nd row on top of the 0th row
    sheet.shiftRows(2, 2, -2);
    // FIXME: remove try, catch, and testPassesNow, skipTest when test passes
    try {
        sheet.removeRow(sheet.getRow(0));
        assertEquals(1, sheet.getRow(1).getRowNum());
        testPassesNow(59733);
    } catch (XmlValueDisconnectedException e) {
        skipTest(e);
    }
    workbook.close();
}
Also used : XmlValueDisconnectedException(org.apache.xmlbeans.impl.values.XmlValueDisconnectedException) Sheet(org.apache.poi.ss.usermodel.Sheet) Workbook(org.apache.poi.ss.usermodel.Workbook) POITestCase.skipTest(org.apache.poi.POITestCase.skipTest) Test(org.junit.Test)

Example 69 with Workbook

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

the class TestSubtotal method testAvg.

@Test
public void testAvg() throws IOException {
    Workbook wb = new HSSFWorkbook();
    FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
    Sheet sh = wb.createSheet();
    Cell a1 = sh.createRow(1).createCell(1);
    a1.setCellValue(1);
    Cell a2 = sh.createRow(2).createCell(1);
    a2.setCellValue(3);
    Cell a3 = sh.createRow(3).createCell(1);
    a3.setCellFormula("SUBTOTAL(1,B2:B3)");
    Cell a4 = sh.createRow(4).createCell(1);
    a4.setCellValue(1);
    Cell a5 = sh.createRow(5).createCell(1);
    a5.setCellValue(7);
    Cell a6 = sh.createRow(6).createCell(1);
    a6.setCellFormula("SUBTOTAL(1,B2:B6)*2 + 2");
    Cell a7 = sh.createRow(7).createCell(1);
    a7.setCellFormula("SUBTOTAL(1,B2:B7)");
    Cell a8 = sh.createRow(8).createCell(1);
    a8.setCellFormula("SUBTOTAL(1,B2,B3,B4,B5,B6,B7,B8)");
    fe.evaluateAll();
    assertEquals(2.0, a3.getNumericCellValue(), 0);
    assertEquals(8.0, a6.getNumericCellValue(), 0);
    assertEquals(3.0, a7.getNumericCellValue(), 0);
    assertEquals(3.0, a8.getNumericCellValue(), 0);
    wb.close();
}
Also used : Sheet(org.apache.poi.ss.usermodel.Sheet) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) 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) FormulaEvaluator(org.apache.poi.ss.usermodel.FormulaEvaluator) Test(org.junit.Test)

Example 70 with Workbook

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

the class TestSubtotal method testMin.

@Test
public void testMin() throws IOException {
    Workbook wb = new HSSFWorkbook();
    FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
    Sheet sh = wb.createSheet();
    Cell a1 = sh.createRow(1).createCell(1);
    a1.setCellValue(1);
    Cell a2 = sh.createRow(2).createCell(1);
    a2.setCellValue(3);
    Cell a3 = sh.createRow(3).createCell(1);
    a3.setCellFormula("SUBTOTAL(5,B2:B3)");
    Cell a4 = sh.createRow(4).createCell(1);
    a4.setCellValue(1);
    Cell a5 = sh.createRow(5).createCell(1);
    a5.setCellValue(7);
    Cell a6 = sh.createRow(6).createCell(1);
    a6.setCellFormula("SUBTOTAL(5,B2:B6)*2 + 2");
    Cell a7 = sh.createRow(7).createCell(1);
    a7.setCellFormula("SUBTOTAL(5,B2:B7)");
    Cell a8 = sh.createRow(8).createCell(1);
    a8.setCellFormula("SUBTOTAL(5,B2,B3,B4,B5,B6,B7,B8)");
    fe.evaluateAll();
    assertEquals(1.0, a3.getNumericCellValue(), 0);
    assertEquals(4.0, a6.getNumericCellValue(), 0);
    assertEquals(1.0, a7.getNumericCellValue(), 0);
    assertEquals(1.0, a8.getNumericCellValue(), 0);
    wb.close();
}
Also used : Sheet(org.apache.poi.ss.usermodel.Sheet) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) 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) FormulaEvaluator(org.apache.poi.ss.usermodel.FormulaEvaluator) 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