Search in sources :

Example 26 with Row

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

the class CreateUserDefinedDataFormats method main.

public static void main(String[] args) throws IOException {
    //or new HSSFWorkbook();
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet("format sheet");
    CellStyle style;
    DataFormat format = wb.createDataFormat();
    Row row;
    Cell cell;
    short rowNum = 0;
    short colNum = 0;
    row = sheet.createRow(rowNum);
    cell = row.createCell(colNum);
    cell.setCellValue(11111.25);
    style = wb.createCellStyle();
    style.setDataFormat(format.getFormat("0.0"));
    cell.setCellStyle(style);
    row = sheet.createRow(++rowNum);
    cell = row.createCell(colNum);
    cell.setCellValue(11111.25);
    style = wb.createCellStyle();
    style.setDataFormat(format.getFormat("#,##0.0000"));
    cell.setCellStyle(style);
    FileOutputStream fileOut = new FileOutputStream("ooxml_dataFormat.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) DataFormat(org.apache.poi.ss.usermodel.DataFormat) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook)

Example 27 with Row

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

the class WorkingWithBorders method main.

public static void main(String[] args) throws IOException {
    //or new HSSFWorkbook();
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet("borders");
    // Create a row and put some cells in it. Rows are 0 based.
    Row row = sheet.createRow((short) 1);
    // Create a cell and put a value in it.
    Cell cell = row.createCell((short) 1);
    cell.setCellValue(4);
    // Style the cell with borders all around.
    CellStyle style = wb.createCellStyle();
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
    style.setBorderLeft(BorderStyle.THIN);
    style.setLeftBorderColor(IndexedColors.GREEN.getIndex());
    style.setBorderRight(BorderStyle.THIN);
    style.setRightBorderColor(IndexedColors.BLUE.getIndex());
    style.setBorderTop(BorderStyle.MEDIUM_DASHED);
    style.setTopBorderColor(IndexedColors.BLACK.getIndex());
    cell.setCellStyle(style);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("xssf-borders.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook)

Example 28 with Row

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

the class IterateCells method main.

public static void main(String[] args) throws IOException {
    Workbook wb = new XSSFWorkbook(new FileInputStream(args[0]));
    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        Sheet sheet = wb.getSheetAt(i);
        System.out.println(wb.getSheetName(i));
        for (Row row : sheet) {
            System.out.println("rownum: " + row.getRowNum());
            for (Cell cell : row) {
                System.out.println(cell);
            }
        }
    }
    wb.close();
}
Also used : 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) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) FileInputStream(java.io.FileInputStream)

Example 29 with Row

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

the class XSSFEvaluationSheet method getCell.

@Override
public EvaluationCell getCell(int rowIndex, int columnIndex) {
    // cache for performance: ~30% speedup due to caching
    if (_cellCache == null) {
        _cellCache = new HashMap<CellKey, EvaluationCell>(_xs.getLastRowNum() * 3);
        for (final Row row : _xs) {
            final int rowNum = row.getRowNum();
            for (final Cell cell : row) {
                // cast is safe, the iterator is just defined using the interface
                final CellKey key = new CellKey(rowNum, cell.getColumnIndex());
                final EvaluationCell evalcell = new XSSFEvaluationCell((XSSFCell) cell, this);
                _cellCache.put(key, evalcell);
            }
        }
    }
    final CellKey key = new CellKey(rowIndex, columnIndex);
    EvaluationCell evalcell = _cellCache.get(key);
    // See bug 59958: Add cells on the fly to the evaluation sheet cache on cache miss
    if (evalcell == null) {
        XSSFRow row = _xs.getRow(rowIndex);
        if (row == null) {
            return null;
        }
        XSSFCell cell = row.getCell(columnIndex);
        if (cell == null) {
            return null;
        }
        evalcell = new XSSFEvaluationCell(cell, this);
        _cellCache.put(key, evalcell);
    }
    return evalcell;
}
Also used : EvaluationCell(org.apache.poi.ss.formula.EvaluationCell) Row(org.apache.poi.ss.usermodel.Row) EvaluationCell(org.apache.poi.ss.formula.EvaluationCell) Cell(org.apache.poi.ss.usermodel.Cell)

Example 30 with Row

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

the class CountifsTests method testCallFunction.

/**
     * Basic call
     */
@Test
public void testCallFunction() {
    Sheet sheet = workbook.createSheet("test");
    Row row1 = sheet.createRow(0);
    Cell cellA1 = row1.createCell(0, CellType.FORMULA);
    Cell cellB1 = row1.createCell(1, CellType.NUMERIC);
    Cell cellC1 = row1.createCell(2, CellType.NUMERIC);
    Cell cellD1 = row1.createCell(3, CellType.NUMERIC);
    Cell cellE1 = row1.createCell(4, CellType.NUMERIC);
    cellB1.setCellValue(1);
    cellC1.setCellValue(1);
    cellD1.setCellValue(2);
    cellE1.setCellValue(4);
    cellA1.setCellFormula("COUNTIFS(B1:C1,1, D1:E1,2)");
    FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
    CellValue evaluate = evaluator.evaluate(cellA1);
    assertEquals(1.0d, evaluate.getNumberValue(), 0.000000000000001);
}
Also used : CellValue(org.apache.poi.ss.usermodel.CellValue) 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) 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