Search in sources :

Example 86 with Row

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

the class TestCellFormat method testApplyFormatHasTwoPartsFirstHasConditionSecondIsGeneral.

@Test
public void testApplyFormatHasTwoPartsFirstHasConditionSecondIsGeneral() 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;General");
    cell.setCellValue(100);
    assertEquals("100.00", cf.apply(cell).text);
    cell.setCellValue(10);
    assertEquals("10", cf.apply(cell).text);
    cell.setCellValue(0);
    assertEquals("0", cf.apply(cell).text);
    cell.setCellValue(-10);
    assertEquals("-10", 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)

Example 87 with Row

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

the class TestCellFormat method testApplyJLabelCellForAtFormat.

@Test
public void testApplyJLabelCellForAtFormat() 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 cell0 = row.createCell(0);
    Cell cell1 = row.createCell(1);
    Cell cell2 = row.createCell(2);
    Cell cell3 = row.createCell(3);
    Cell cell4 = row.createCell(4);
    CellFormat cf = CellFormat.getInstance("@");
    JLabel label0 = new JLabel();
    JLabel label1 = new JLabel();
    JLabel label2 = new JLabel();
    JLabel label3 = new JLabel();
    JLabel label4 = new JLabel();
    // case CellType.BLANK
    CellFormatResult result0 = cf.apply(label0, cell0);
    assertEquals("", result0.text);
    assertEquals("", label0.getText());
    // case CellType.BOOLEAN
    cell1.setCellValue(true);
    CellFormatResult result1 = cf.apply(label1, cell1);
    assertEquals("TRUE", result1.text);
    assertEquals("TRUE", label1.getText());
    // case CellType.NUMERIC
    cell2.setCellValue(1.23);
    CellFormatResult result2 = cf.apply(label2, cell2);
    assertEquals("1.23", result2.text);
    assertEquals("1.23", label2.getText());
    cell3.setCellValue(123.0);
    CellFormatResult result3 = cf.apply(label3, cell3);
    assertEquals("123", result3.text);
    assertEquals("123", label3.getText());
    // case CellType.STRING
    cell4.setCellValue("abc");
    CellFormatResult result4 = cf.apply(label4, cell4);
    assertEquals("abc", result4.text);
    assertEquals("abc", label4.getText());
    wb.close();
}
Also used : JLabel(javax.swing.JLabel) 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)

Example 88 with Row

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

the class TestMissingWorkbook method testMissingWorkbookMissing.

public void testMissingWorkbookMissing() throws IOException {
    FormulaEvaluator evaluator = mainWorkbook.getCreationHelper().createFormulaEvaluator();
    Sheet lSheet = mainWorkbook.getSheetAt(0);
    Row lARow = lSheet.getRow(0);
    Cell lA1Cell = lARow.getCell(0);
    assertEquals(CellType.FORMULA, lA1Cell.getCellTypeEnum());
    try {
        evaluator.evaluateFormulaCellEnum(lA1Cell);
        fail("Missing external workbook reference exception expected!");
    } catch (RuntimeException re) {
        assertTrue("Unexpected exception: " + re, re.getMessage().indexOf(SOURCE_DUMMY_WORKBOOK_FILENAME) != -1);
    }
}
Also used : Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) FormulaEvaluator(org.apache.poi.ss.usermodel.FormulaEvaluator)

Example 89 with Row

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

the class TestCellFormat method testApplyFormatHasTwoPartsBothHaveCondition.

@Test
public void testApplyFormatHasTwoPartsBothHaveCondition() 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");
    cell.setCellValue(100);
    assertEquals("100.00", cf.apply(cell).text);
    cell.setCellValue(10);
    assertEquals("10.000", cf.apply(cell).text);
    cell.setCellValue(0);
    assertEquals(_255_POUND_SIGNS, cf.apply(cell).text);
    cell.setCellValue(-0.123456789012345);
    assertEquals(_255_POUND_SIGNS, cf.apply(cell).text);
    cell.setCellValue(-10);
    assertEquals(_255_POUND_SIGNS, 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)

Example 90 with Row

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

the class TestCellFormat method testApplyJLabelCellForGeneralFormat.

@Test
public void testApplyJLabelCellForGeneralFormat() 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 cell0 = row.createCell(0);
    Cell cell1 = row.createCell(1);
    Cell cell2 = row.createCell(2);
    Cell cell3 = row.createCell(3);
    Cell cell4 = row.createCell(4);
    CellFormat cf = CellFormat.getInstance("General");
    JLabel label0 = new JLabel();
    JLabel label1 = new JLabel();
    JLabel label2 = new JLabel();
    JLabel label3 = new JLabel();
    JLabel label4 = new JLabel();
    // case CellType.BLANK
    CellFormatResult result0 = cf.apply(label0, cell0);
    assertEquals("", result0.text);
    assertEquals("", label0.getText());
    // case CellType.BOOLEAN
    cell1.setCellValue(true);
    CellFormatResult result1 = cf.apply(label1, cell1);
    assertEquals("TRUE", result1.text);
    assertEquals("TRUE", label1.getText());
    // case CellType.NUMERIC
    cell2.setCellValue(1.23);
    CellFormatResult result2 = cf.apply(label2, cell2);
    assertEquals("1.23", result2.text);
    assertEquals("1.23", label2.getText());
    cell3.setCellValue(123.0);
    CellFormatResult result3 = cf.apply(label3, cell3);
    assertEquals("123", result3.text);
    assertEquals("123", label3.getText());
    // case CellType.STRING
    cell4.setCellValue("abc");
    CellFormatResult result4 = cf.apply(label4, cell4);
    assertEquals("abc", result4.text);
    assertEquals("abc", label4.getText());
    wb.close();
}
Also used : JLabel(javax.swing.JLabel) 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

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