Search in sources :

Example 71 with Row

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

the class TestRegionUtil method getCellStyle.

private CellStyle getCellStyle(int rowIndex, int columnIndex) {
    Row row = sheet.getRow(rowIndex);
    if (row == null)
        row = sheet.createRow(rowIndex);
    Cell cell = row.getCell(columnIndex);
    if (cell == null)
        cell = row.createCell(columnIndex);
    return cell.getCellStyle();
}
Also used : Row(org.apache.poi.ss.usermodel.Row) Cell(org.apache.poi.ss.usermodel.Cell)

Example 72 with Row

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

the class TestSheetUtil method testGetCellWidthNumber.

public void testGetCellWidthNumber() throws IOException {
    Workbook wb = new HSSFWorkbook();
    Sheet sheet = wb.createSheet("sheet");
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);
    cell.setCellValue(88.234);
    assertTrue(SheetUtil.getCellWidth(cell, 1, null, true) > 0);
    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)

Example 73 with Row

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

the class TestSheetUtil method testCellWithMerges.

public void testCellWithMerges() throws Exception {
    Workbook wb = new HSSFWorkbook();
    Sheet s = wb.createSheet();
    // Create some test data
    Row r2 = s.createRow(1);
    r2.createCell(0).setCellValue(10);
    r2.createCell(1).setCellValue(11);
    Row r3 = s.createRow(2);
    r3.createCell(0).setCellValue(20);
    r3.createCell(1).setCellValue(21);
    s.addMergedRegion(new CellRangeAddress(2, 3, 0, 0));
    s.addMergedRegion(new CellRangeAddress(2, 2, 1, 4));
    // With a cell that isn't defined, we'll get null
    assertEquals(null, SheetUtil.getCellWithMerges(s, 0, 0));
    // With a cell that's not in a merged region, we'll get that
    assertEquals(10.0, SheetUtil.getCellWithMerges(s, 1, 0).getNumericCellValue());
    assertEquals(11.0, SheetUtil.getCellWithMerges(s, 1, 1).getNumericCellValue());
    // With a cell that's the primary one of a merged region, we get that cell
    assertEquals(20.0, SheetUtil.getCellWithMerges(s, 2, 0).getNumericCellValue());
    assertEquals(21., SheetUtil.getCellWithMerges(s, 2, 1).getNumericCellValue());
    // With a cell elsewhere in the merged region, get top-left
    assertEquals(20.0, SheetUtil.getCellWithMerges(s, 3, 0).getNumericCellValue());
    assertEquals(21.0, SheetUtil.getCellWithMerges(s, 2, 2).getNumericCellValue());
    assertEquals(21.0, SheetUtil.getCellWithMerges(s, 2, 3).getNumericCellValue());
    assertEquals(21.0, SheetUtil.getCellWithMerges(s, 2, 4).getNumericCellValue());
    wb.close();
}
Also used : Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 74 with Row

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

the class TestBugs method bug53404.

@Test
public void bug53404() throws Exception {
    HSSFWorkbook wb = openSample("53404.xls");
    Sheet sheet = wb.getSheet("test-sheet");
    int rowCount = sheet.getLastRowNum() + 1;
    int newRows = 5;
    Calendar cal = LocaleUtil.getLocaleCalendar();
    for (int r = rowCount; r < rowCount + newRows; r++) {
        Row row = sheet.createRow((short) r);
        row.createCell(0).setCellValue(1.03 * (r + 7));
        row.createCell(1).setCellValue(cal.getTime());
        row.createCell(2).setCellValue(cal);
        row.createCell(3).setCellValue(String.format(Locale.ROOT, "row:%d/col:%d", r, 3));
        row.createCell(4).setCellValue(true);
        row.createCell(5).setCellType(CellType.ERROR);
        row.createCell(6).setCellValue("added cells.");
    }
    writeOutAndReadBack(wb).close();
    wb.close();
}
Also used : Calendar(java.util.Calendar) Row(org.apache.poi.ss.usermodel.Row) InternalSheet(org.apache.poi.hssf.model.InternalSheet) Sheet(org.apache.poi.ss.usermodel.Sheet) Test(org.junit.Test)

Example 75 with Row

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

the class TestBugs method test57925.

@Test
public void test57925() throws IOException {
    Workbook wb = HSSFTestDataSamples.openSampleWorkbook("57925.xls");
    wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        Sheet sheet = wb.getSheetAt(i);
        for (Row row : sheet) {
            for (Cell cell : row) {
                new DataFormatter().formatCellValue(cell);
            }
        }
    }
    wb.close();
}
Also used : Row(org.apache.poi.ss.usermodel.Row) InternalSheet(org.apache.poi.hssf.model.InternalSheet) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) DataFormatter(org.apache.poi.ss.usermodel.DataFormatter) 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