Search in sources :

Example 11 with XSSFCell

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

the class TestXSSFCellFill method testColorFromTheme.

@Test
public void testColorFromTheme() throws IOException {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("styles.xlsx");
    XSSFCell cellWithThemeColor = wb.getSheetAt(0).getRow(10).getCell(0);
    //color RGB will be extracted from theme
    XSSFColor foregroundColor = cellWithThemeColor.getCellStyle().getFillForegroundXSSFColor();
    byte[] rgb = foregroundColor.getRGB();
    byte[] rgbWithTint = foregroundColor.getRGBWithTint();
    // Dk2
    assertEquals(rgb[0], 31);
    assertEquals(rgb[1], 73);
    assertEquals(rgb[2], 125);
    // Dk2, lighter 40% (tint is about 0.39998)
    // 31 * (1.0 - 0.39998) + (255 - 255 * (1.0 - 0.39998)) = 120.59552 => 120 (byte)
    // 73 * (1.0 - 0.39998) + (255 - 255 * (1.0 - 0.39998)) = 145.79636 => -111 (byte)
    // 125 * (1.0 - 0.39998) + (255 - 255 * (1.0 - 0.39998)) = 176.99740 => -80 (byte)
    assertEquals(rgbWithTint[0], 120);
    assertEquals(rgbWithTint[1], -111);
    assertEquals(rgbWithTint[2], -80);
    wb.close();
}
Also used : XSSFColor(org.apache.poi.xssf.usermodel.XSSFColor) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) Test(org.junit.Test)

Example 12 with XSSFCell

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

the class XSSFRowShifter method updateRowFormulas.

/**
     * Update the formulas in specified row using the formula shifting policy specified by shifter
     *
     * @param row the row to update the formulas on
     * @param shifter the formula shifting policy
     */
@Internal
public void updateRowFormulas(Row row, FormulaShifter shifter) {
    XSSFSheet sheet = (XSSFSheet) row.getSheet();
    for (Cell c : row) {
        XSSFCell cell = (XSSFCell) c;
        CTCell ctCell = cell.getCTCell();
        if (ctCell.isSetF()) {
            CTCellFormula f = ctCell.getF();
            String formula = f.getStringValue();
            if (formula.length() > 0) {
                String shiftedFormula = shiftFormula(row, formula, shifter);
                if (shiftedFormula != null) {
                    f.setStringValue(shiftedFormula);
                    if (f.getT() == STCellFormulaType.SHARED) {
                        int si = (int) f.getSi();
                        CTCellFormula sf = sheet.getSharedFormula(si);
                        sf.setStringValue(shiftedFormula);
                        updateRefInCTCellFormula(row, shifter, sf);
                    }
                }
            }
            //Range of cells which the formula applies to.
            updateRefInCTCellFormula(row, shifter, f);
        }
    }
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) CTCellFormula(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula) CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell) Cell(org.apache.poi.ss.usermodel.Cell) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) Internal(org.apache.poi.util.Internal)

Example 13 with XSSFCell

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

the class TestCalculationChain method test46535.

public void test46535() {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("46535.xlsx");
    CalculationChain chain = wb.getCalculationChain();
    //the bean holding the reference to the formula to be deleted
    CTCalcCell c = chain.getCTCalcChain().getCArray(0);
    int cnt = chain.getCTCalcChain().sizeOfCArray();
    assertEquals(10, c.getI());
    assertEquals("E1", c.getR());
    XSSFSheet sheet = wb.getSheet("Test");
    XSSFCell cell = sheet.getRow(0).getCell(4);
    assertEquals(CellType.FORMULA, cell.getCellTypeEnum());
    cell.setCellFormula(null);
    //the count of items is less by one
    c = chain.getCTCalcChain().getCArray(0);
    int cnt2 = chain.getCTCalcChain().sizeOfCArray();
    assertEquals(cnt - 1, cnt2);
    //the first item in the calculation chain is the former second one
    assertEquals(10, c.getI());
    assertEquals("C1", c.getR());
    assertEquals(CellType.STRING, cell.getCellTypeEnum());
    cell.setCellValue("ABC");
    assertEquals(CellType.STRING, cell.getCellTypeEnum());
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) CTCalcCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCalcCell)

Example 14 with XSSFCell

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

the class ExcelNodeSerializer method startWriteRootNode.

@Override
protected void startWriteRootNode(RootNode rootNode) throws Exception {
    XSSFCreationHelper creationHelper = workbook.getCreationHelper();
    int rowIdx = 1;
    for (Node collectionNode : rootNode.getChildren()) {
        if (collectionNode.isCollection()) {
            for (Node complexNode : collectionNode.getChildren()) {
                XSSFRow row = sheet.createRow(rowIdx++);
                int cellIdx = 0;
                for (Node node : complexNode.getChildren()) {
                    if (node.isSimple()) {
                        XSSFCell cell = row.createCell(cellIdx++);
                        cell.setCellValue(getValue((SimpleNode) node));
                        if (node.haveProperty() && PropertyType.URL.equals(node.getProperty().getPropertyType())) {
                            XSSFHyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.URL);
                            hyperlink.setAddress(getValue((SimpleNode) node));
                            hyperlink.setLabel(getValue((SimpleNode) node));
                            cell.setHyperlink(hyperlink);
                        } else if (node.haveProperty() && PropertyType.EMAIL.equals(node.getProperty().getPropertyType())) {
                            XSSFHyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.EMAIL);
                            hyperlink.setAddress(getValue((SimpleNode) node));
                            hyperlink.setLabel(getValue((SimpleNode) node));
                            cell.setHyperlink(hyperlink);
                        }
                    }
                }
            }
        }
    }
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFCreationHelper(org.apache.poi.xssf.usermodel.XSSFCreationHelper) CollectionNode(org.hisp.dhis.node.types.CollectionNode) Node(org.hisp.dhis.node.Node) SimpleNode(org.hisp.dhis.node.types.SimpleNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) RootNode(org.hisp.dhis.node.types.RootNode) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) SimpleNode(org.hisp.dhis.node.types.SimpleNode) XSSFHyperlink(org.apache.poi.xssf.usermodel.XSSFHyperlink)

Example 15 with XSSFCell

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

the class ExcelNodeSerializer method startSerialize.

@Override
protected void startSerialize(RootNode rootNode, OutputStream outputStream) throws Exception {
    workbook = new XSSFWorkbook();
    sheet = workbook.createSheet("Sheet1");
    XSSFFont boldFont = workbook.createFont();
    boldFont.setBold(true);
    XSSFCellStyle boldCellStyle = workbook.createCellStyle();
    boldCellStyle.setFont(boldFont);
    // build schema
    for (Node child : rootNode.getChildren()) {
        if (child.isCollection()) {
            if (!child.getChildren().isEmpty()) {
                Node node = child.getChildren().get(0);
                XSSFRow row = sheet.createRow(0);
                int cellIdx = 0;
                for (Node property : node.getChildren()) {
                    if (property.isSimple()) {
                        XSSFCell cell = row.createCell(cellIdx++);
                        cell.setCellValue(property.getName());
                        cell.setCellStyle(boldCellStyle);
                    }
                }
            }
        }
    }
}
Also used : XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFFont(org.apache.poi.xssf.usermodel.XSSFFont) CollectionNode(org.hisp.dhis.node.types.CollectionNode) Node(org.hisp.dhis.node.Node) SimpleNode(org.hisp.dhis.node.types.SimpleNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) RootNode(org.hisp.dhis.node.types.RootNode) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell)

Aggregations

XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)22 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)13 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)11 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)10 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)7 Test (org.junit.Test)7 FileOutputStream (java.io.FileOutputStream)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 CellReference (org.apache.poi.ss.util.CellReference)3 XSSFColor (org.apache.poi.xssf.usermodel.XSSFColor)3 XSSFFont (org.apache.poi.xssf.usermodel.XSSFFont)3 XSSFTable (org.apache.poi.xssf.usermodel.XSSFTable)3 File (java.io.File)2 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)2 SXSSFWorkbookWithCustomZipEntrySource (org.apache.poi.poifs.crypt.temp.SXSSFWorkbookWithCustomZipEntrySource)2 Cell (org.apache.poi.ss.usermodel.Cell)2 CellStyle (org.apache.poi.ss.usermodel.CellStyle)2 CreationHelper (org.apache.poi.ss.usermodel.CreationHelper)2 Workbook (org.apache.poi.ss.usermodel.Workbook)2