Search in sources :

Example 1 with XSSFCell

use of org.apache.poi.xssf.usermodel.XSSFCell in project poi by apache.

the class XSSFExcelExtractor method handleNonStringCell.

private void handleNonStringCell(StringBuffer text, Cell cell, DataFormatter formatter) {
    CellType type = cell.getCellTypeEnum();
    if (type == CellType.FORMULA) {
        type = cell.getCachedFormulaResultTypeEnum();
    }
    if (type == CellType.NUMERIC) {
        CellStyle cs = cell.getCellStyle();
        if (cs != null && cs.getDataFormatString() != null) {
            String contents = formatter.formatRawCellContents(cell.getNumericCellValue(), cs.getDataFormat(), cs.getDataFormatString());
            checkMaxTextSize(text, contents);
            text.append(contents);
            return;
        }
    }
    // No supported styling applies to this cell
    String contents = ((XSSFCell) cell).getRawValue();
    if (contents != null) {
        checkMaxTextSize(text, contents);
        text.append(contents);
    }
}
Also used : CellType(org.apache.poi.ss.usermodel.CellType) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) CellStyle(org.apache.poi.ss.usermodel.CellStyle)

Example 2 with XSSFCell

use of org.apache.poi.xssf.usermodel.XSSFCell in project poi by apache.

the class AligningCells method centerAcrossSelection.

/**
     * Center a text over multiple columns using ALIGN_CENTER_SELECTION
     *
     * @param wb the workbook
     * @param row the row to create the cell in
     * @param start_column  the column number to create the cell in and where the selection starts
     * @param end_column    the column number where the selection ends
     * @param valign the horizontal alignment for the cell.
     */
private static void centerAcrossSelection(XSSFWorkbook wb, XSSFRow row, int start_column, int end_column, VerticalAlignment valign) {
    CreationHelper ch = wb.getCreationHelper();
    // Create cell style with ALIGN_CENTER_SELECTION
    XSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(HorizontalAlignment.CENTER_SELECTION);
    cellStyle.setVerticalAlignment(valign);
    // Create cells over the selected area
    for (int i = start_column; i <= end_column; i++) {
        XSSFCell cell = row.createCell(i);
        cell.setCellStyle(cellStyle);
    }
    // Set value to the first cell
    XSSFCell cell = row.getCell(start_column);
    cell.setCellValue(ch.createRichTextString("Align It"));
    // Make the selection
    CTRowImpl ctRow = (CTRowImpl) row.getCTRow();
    // Add object with format start_coll:end_coll. For example 1:3 will span from
    // cell 1 to cell 3, where the column index starts with 0
    //
    // You can add multiple spans for one row
    Object span = start_column + ":" + end_column;
    List<Object> spanList = new ArrayList<Object>();
    spanList.add(span);
    //add spns to the row
    ctRow.setSpans(spanList);
}
Also used : XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) ArrayList(java.util.ArrayList) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) CTRowImpl(org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTRowImpl)

Example 3 with XSSFCell

use of org.apache.poi.xssf.usermodel.XSSFCell in project poi by apache.

the class AligningCells method createCell.

/**
     * Creates a cell and aligns it a certain way.
     *
     * @param wb     the workbook
     * @param row    the row to create the cell in
     * @param column the column number to create the cell in
     * @param halign the horizontal alignment for the cell.
     */
private static void createCell(XSSFWorkbook wb, XSSFRow row, int column, HorizontalAlignment halign, VerticalAlignment valign) {
    CreationHelper ch = wb.getCreationHelper();
    XSSFCell cell = row.createCell(column);
    cell.setCellValue(ch.createRichTextString("Align It"));
    CellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(halign);
    cellStyle.setVerticalAlignment(valign);
    cell.setCellStyle(cellStyle);
}
Also used : CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) CellStyle(org.apache.poi.ss.usermodel.CellStyle)

Example 4 with XSSFCell

use of org.apache.poi.xssf.usermodel.XSSFCell in project poi by apache.

the class CreateTable method main.

public static void main(String[] args) throws IOException {
    Workbook wb = new XSSFWorkbook();
    XSSFSheet sheet = (XSSFSheet) wb.createSheet();
    //Create 
    XSSFTable table = sheet.createTable();
    table.setDisplayName("Test");
    CTTable cttable = table.getCTTable();
    //Style configurations
    CTTableStyleInfo style = cttable.addNewTableStyleInfo();
    style.setName("TableStyleMedium2");
    style.setShowColumnStripes(false);
    style.setShowRowStripes(true);
    //Set which area the table should be placed in
    AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(2, 2));
    cttable.setRef(reference.formatAsString());
    cttable.setId(1);
    cttable.setName("Test");
    cttable.setTotalsRowCount(1);
    CTTableColumns columns = cttable.addNewTableColumns();
    columns.setCount(3);
    CTTableColumn column;
    XSSFRow row;
    XSSFCell cell;
    for (int i = 0; i < 3; i++) {
        //Create column
        column = columns.addNewTableColumn();
        column.setName("Column");
        column.setId(i + 1);
        //Create row
        row = sheet.createRow(i);
        for (int j = 0; j < 3; j++) {
            //Create cell
            cell = row.createCell(j);
            if (i == 0) {
                cell.setCellValue("Column" + j);
            } else {
                cell.setCellValue("0");
            }
        }
    }
    FileOutputStream fileOut = new FileOutputStream("ooxml-table.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : AreaReference(org.apache.poi.ss.util.AreaReference) CTTableColumns(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumns) CellReference(org.apache.poi.ss.util.CellReference) XSSFTable(org.apache.poi.xssf.usermodel.XSSFTable) CTTableColumn(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) CTTableStyleInfo(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo) FileOutputStream(java.io.FileOutputStream) CTTable(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell)

Example 5 with XSSFCell

use of org.apache.poi.xssf.usermodel.XSSFCell in project poi by apache.

the class XSSFSingleXmlCell method getReferencedCell.

/**
	 * Gets the XSSFCell referenced by the R attribute or creates a new one if cell doesn't exists
	 * @return the referenced XSSFCell, null if the cell reference is invalid
	 */
public XSSFCell getReferencedCell() {
    XSSFCell cell = null;
    CellReference cellReference = new CellReference(singleXmlCell.getR());
    XSSFRow row = parent.getXSSFSheet().getRow(cellReference.getRow());
    if (row == null) {
        row = parent.getXSSFSheet().createRow(cellReference.getRow());
    }
    cell = row.getCell(cellReference.getCol());
    if (cell == null) {
        cell = row.createCell(cellReference.getCol());
    }
    return cell;
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) CellReference(org.apache.poi.ss.util.CellReference)

Aggregations

XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)45 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)34 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)21 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)21 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)10 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 IOException (java.io.IOException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 FileOutputStream (java.io.FileOutputStream)4 HashSet (java.util.HashSet)4 Iterator (java.util.Iterator)4 Map (java.util.Map)4 Workbook (org.apache.poi.ss.usermodel.Workbook)4 XSSFFont (org.apache.poi.xssf.usermodel.XSSFFont)4 RefineTest (com.google.refine.RefineTest)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 Cell (org.apache.poi.ss.usermodel.Cell)3