Package org.apache.poi.xssf.usermodel

Examples of org.apache.poi.xssf.usermodel.XSSFCell


            String xpathString = singleXmlCell.getXpath();
            Node result = (Node) xpath.evaluate(xpathString, doc, XPathConstants.NODE);
            String textContent = result.getTextContent();
            logger.log(POILogger.DEBUG, "Extracting with xpath " + xpathString + " : value is '" + textContent + "'");
            XSSFCell cell = singleXmlCell.getReferencedCell();
            logger.log(POILogger.DEBUG, "Setting '" + textContent + "' to cell " + cell.getColumnIndex() + "-" + cell.getRowIndex() + " in sheet "
                                            + cell.getSheet().getSheetName());
            cell.setCellValue(textContent);
        }

        for (XSSFTable table : tables) {

            String commonXPath = table.getCommonXpath();
            NodeList result = (NodeList) xpath.evaluate(commonXPath, doc, XPathConstants.NODESET);
            int rowOffset = table.getStartCellReference().getRow() + 1;// the first row contains the table header
            int columnOffset = table.getStartCellReference().getCol() - 1;

            for (int i = 0; i < result.getLength(); i++) {

                // TODO: implement support for denormalized XMLs (see
                // OpenOffice part 4: chapter 3.5.1.7)

                for (XSSFXmlColumnPr xmlColumnPr : table.getXmlColumnPrs()) {

                    int localColumnId = (int) xmlColumnPr.getId();
                    int rowId = rowOffset + i;
                    int columnId = columnOffset + localColumnId;
                    String localXPath = xmlColumnPr.getLocalXPath();
                    localXPath = localXPath.substring(localXPath.substring(1).indexOf('/') + 1);

                    // Build an XPath to select the right node (assuming
                    // that the commonXPath != "/")
                    String nodeXPath = commonXPath + "[" + (i + 1) + "]" + localXPath;

                    // TODO: convert the data to the cell format
                    String value = (String) xpath.evaluate(nodeXPath, result.item(i), XPathConstants.STRING);
                    logger.log(POILogger.DEBUG, "Extracting with xpath " + nodeXPath + " : value is '" + value + "'");
                    XSSFRow row = table.getXSSFSheet().getRow(rowId);
                    if (row == null) {
                        row = table.getXSSFSheet().createRow(rowId);
                    }

                    XSSFCell cell = row.getCell(columnId);
                    if (cell == null) {
                        cell = row.createCell(columnId);
                    }
                    logger.log(POILogger.DEBUG, "Setting '" + value + "' to cell " + cell.getColumnIndex() + "-" + cell.getRowIndex() + " in sheet "
                                                    + table.getXSSFSheet().getSheetName());
                    cell.setCellValue(value.trim());
                }
            }
        }
    }
View Full Code Here


            if (!xpath.matches(".*\\[.*")) {

                // Exports elements and attributes mapped with simpleXmlCell
                if (simpleXmlCell!=null) {
                    XSSFCell cell = simpleXmlCell.getReferencedCell();
                    if (cell!=null) {
                        Node currentNode = getNodeByXPath(xpath,doc.getFirstChild(),doc,false);
                        STXmlDataType.Enum dataType = simpleXmlCell.getXmlDataType();
                        mapCellOnNode(cell,currentNode,dataType);
                    }
                }

                // Exports elements and attributes mapped with tables
                if (table!=null) {

                    List<XSSFXmlColumnPr> tableColumns = table.getXmlColumnPrs();

                    XSSFSheet sheet = table.getXSSFSheet();

                    int startRow = table.getStartCellReference().getRow();
                    // In mappings created with Microsoft Excel the first row contains the table header and must be skipped
                    startRow +=1;

                    int endRow = table.getEndCellReference().getRow();

                    for(int i = startRow; i<= endRow; i++) {
                        XSSFRow row = sheet.getRow(i);

                        Node tableRootNode = getNodeByXPath(table.getCommonXpath(),doc.getFirstChild(),doc,true);

                        short startColumnIndex = table.getStartCellReference().getCol();
                        for(int j = startColumnIndex; j<= table.getEndCellReference().getCol();j++) {
                            XSSFCell cell = row.getCell(j);
                            if (cell!=null) {
                                XSSFXmlColumnPr pointer = tableColumns.get(j-startColumnIndex);
                                String localXPath = pointer.getLocalXPath();
                                Node currentNode = getNodeByXPath(localXPath,tableRootNode,doc,false);
                                STXmlDataType.Enum dataType = pointer.getXmlDataType();
View Full Code Here

                        }else{
                          strValue = String.valueOf(hssfCell.getNumericCellValue());
                        }
                      }
                    }else{
                      XSSFCell xssfCell = (XSSFCell)cell;
                      if(HSSFDateUtil.isCellDateFormatted(cell)){
                          String dateFmt = cell.getCellStyle().getDataFormatString();
                          /* strValue = new SimpleDateFormat(dateFmt).format(date); - won't work as
                          Java fmt differs from Excel fmt. If Excel date format is mm/dd/yyyy, Java
                          will always be 00 for date since "m" is minutes of the hour.*/
                          strValue = new CellDateFormatter(dateFmt).format(cell.getDateCellValue());
//                        td.text(strValue);
                      }else{
                        XSSFCellStyle cellStyle = xssfCell.getCellStyle();
                        if(cellStyle!=null && cellStyle.getDataFormatString()!=null){
//                           String dateFmt = cell.getCellStyle().getDataFormatString();
                              /* strValue = new SimpleDateFormat(dateFmt).format(date); - won't work as
                              Java fmt differs from Excel fmt. If Excel date format is mm/dd/yyyy, Java
                              will always be 00 for date since "m" is minutes of the hour.*/
                           strValue = new HSSFDataFormatter().formatRawCellContents(cell.getNumericCellValue(), cellStyle.getDataFormat(), cellStyle.getDataFormatString());
                        }else{
                            strValue = String.valueOf(xssfCell.getNumericCellValue());
                        }
//                        xssfCell.getCellStyle().getDataFormatString();
//                        td.text(String.valueOf(cell.getNumericCellValue()));
                      }
                    }
                    td.text(strValue);
                  }else if(cell.getCellType()==Cell.CELL_TYPE_STRING){
                    td.text(cell.getStringCellValue());
                  }
                  if(sheet instanceof HSSFSheet){
                    HSSFSheet hssfSheet = (HSSFSheet)sheet;
                    HSSFCell hssfCell = (HSSFCell)cell;
                    HSSFCellStyle style = hssfCell.getCellStyle();
//                    判断一下如果式样中有斜杠,那么写一个斜杠
                   
                    HSSFPalette palette = hssfSheet.getWorkbook().getCustomPalette();
                    styleApplier4XLS(td,style,hssfSheet.getWorkbook(),palette);
                  }else{
                    XSSFSheet xssfSheet = (XSSFSheet)sheet;
                    XSSFCell xssfCell = (XSSFCell)cell;
                    XSSFCellStyle style = xssfCell.getCellStyle();
//                    判断一下如果式样中有斜杠,那么写一个斜杠
                    styleApplier4XLSX(td,style,xssfSheet.getWorkbook());
                  }
                }

View Full Code Here

                return;
            }
        }

        // No supported styling applies to this cell
        XSSFCell xcell = (XSSFCell)cell;
        text.append( xcell.getRawValue() );
    }
View Full Code Here

            if (!xpath.matches(".*\\[.*")) {

                // Exports elements and attributes mapped with simpleXmlCell
                if (simpleXmlCell!=null) {
                    XSSFCell cell = simpleXmlCell.getReferencedCell();
                    if (cell!=null) {
                        Node currentNode = getNodeByXPath(xpath,doc.getFirstChild(),doc,false);
                        STXmlDataType.Enum dataType = simpleXmlCell.getXmlDataType();
                        mapCellOnNode(cell,currentNode,dataType);
                    }
                }

                // Exports elements and attributes mapped with tables
                if (table!=null) {

                    List<XSSFXmlColumnPr> tableColumns = table.getXmlColumnPrs();

                    XSSFSheet sheet = table.getXSSFSheet();

                    int startRow = table.getStartCellReference().getRow();
                    // In mappings created with Microsoft Excel the first row contains the table header and must be skipped
                    startRow +=1;

                    int endRow = table.getEndCellReference().getRow();

                    for(int i = startRow; i<= endRow; i++) {
                        XSSFRow row = sheet.getRow(i);

                        Node tableRootNode = getNodeByXPath(table.getCommonXpath(),doc.getFirstChild(),doc,true);

                        short startColumnIndex = table.getStartCellReference().getCol();
                        for(int j = startColumnIndex; j<= table.getEndCellReference().getCol();j++) {
                            XSSFCell cell = row.getCell(j);
                            if (cell!=null) {
                                XSSFXmlColumnPr pointer = tableColumns.get(j-startColumnIndex);
                                String localXPath = pointer.getLocalXPath();
                                Node currentNode = getNodeByXPath(localXPath,tableRootNode,doc,false);
                                STXmlDataType.Enum dataType = pointer.getXmlDataType();
View Full Code Here

            sCell.setCellValue(str);
            assertEquals(sCell.getStringCellValue(), str);

            // read back as XSSF and check that xml:spaces="preserve" is set
            XSSFWorkbook xwb = (XSSFWorkbook)SXSSFITestDataProvider.instance.writeOutAndReadBack(swb);
            XSSFCell xCell = xwb.getSheetAt(0).getRow(0).getCell(0);

            CTRst is = xCell.getCTCell().getIs();
            XmlCursor c = is.newCursor();
            c.toNextToken();
            String t = c.getAttributeText(new QName("http://www.w3.org/XML/1998/namespace", "space"));
            c.dispose();
            assertEquals("expected xml:spaces=\"preserve\" \"" + str + "\"", "preserve", t);
View Full Code Here

       
        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");
View Full Code Here

                return;
            }
        }

        // No supported styling applies to this cell
        XSSFCell xcell = (XSSFCell)cell;
        text.append( xcell.getRawValue() );
    }
View Full Code Here

            if (!xpath.matches(".*\\[.*")) {

                // Exports elements and attributes mapped with simpleXmlCell
                if (simpleXmlCell!=null) {
                    XSSFCell cell = simpleXmlCell.getReferencedCell();
                    if (cell!=null) {
                        Node currentNode = getNodeByXPath(xpath,doc.getFirstChild(),doc,false);
                        STXmlDataType.Enum dataType = simpleXmlCell.getXmlDataType();
                        mapCellOnNode(cell,currentNode,dataType);
                       
                        //remove nodes which are empty in order to keep the output xml valid
                        if("".equals(currentNode.getTextContent()) && currentNode.getParentNode() != null) {
                          currentNode.getParentNode().removeChild(currentNode);
                        }
                    }
                }

                // Exports elements and attributes mapped with tables
                if (table!=null) {

                    List<XSSFXmlColumnPr> tableColumns = table.getXmlColumnPrs();

                    XSSFSheet sheet = table.getXSSFSheet();

                    int startRow = table.getStartCellReference().getRow();
                    // In mappings created with Microsoft Excel the first row contains the table header and must be skipped
                    startRow +=1;

                    int endRow = table.getEndCellReference().getRow();

                    for(int i = startRow; i<= endRow; i++) {
                        XSSFRow row = sheet.getRow(i);

                        Node tableRootNode = getNodeByXPath(table.getCommonXpath(),doc.getFirstChild(),doc,true);

                        short startColumnIndex = table.getStartCellReference().getCol();
                        for(int j = startColumnIndex; j<= table.getEndCellReference().getCol();j++) {
                            XSSFCell cell = row.getCell(j);
                            if (cell!=null) {
                                XSSFXmlColumnPr pointer = tableColumns.get(j-startColumnIndex);
                                String localXPath = pointer.getLocalXPath();
                                Node currentNode = getNodeByXPath(localXPath,tableRootNode,doc,false);
                                STXmlDataType.Enum dataType = pointer.getXmlDataType();
View Full Code Here

            String xpathString = singleXmlCell.getXpath();
            Node result = (Node) xpath.evaluate(xpathString, doc, XPathConstants.NODE);
            String textContent = result.getTextContent();
            logger.log(POILogger.DEBUG, "Extracting with xpath " + xpathString + " : value is '" + textContent + "'");
            XSSFCell cell = singleXmlCell.getReferencedCell();
            logger.log(POILogger.DEBUG, "Setting '" + textContent + "' to cell " + cell.getColumnIndex() + "-" + cell.getRowIndex() + " in sheet "
                                            + cell.getSheet().getSheetName());
            cell.setCellValue(textContent);
        }

        for (XSSFTable table : tables) {

            String commonXPath = table.getCommonXpath();
            NodeList result = (NodeList) xpath.evaluate(commonXPath, doc, XPathConstants.NODESET);
            int rowOffset = table.getStartCellReference().getRow() + 1;// the first row contains the table header
            int columnOffset = table.getStartCellReference().getCol() - 1;

            for (int i = 0; i < result.getLength(); i++) {

                // TODO: implement support for denormalized XMLs (see
                // OpenOffice part 4: chapter 3.5.1.7)

                for (XSSFXmlColumnPr xmlColumnPr : table.getXmlColumnPrs()) {

                    int localColumnId = (int) xmlColumnPr.getId();
                    int rowId = rowOffset + i;
                    int columnId = columnOffset + localColumnId;
                    String localXPath = xmlColumnPr.getLocalXPath();
                    localXPath = localXPath.substring(localXPath.substring(1).indexOf('/') + 1);

                    // Build an XPath to select the right node (assuming
                    // that the commonXPath != "/")
                    String nodeXPath = commonXPath + "[" + (i + 1) + "]" + localXPath;

                    // TODO: convert the data to the cell format
                    String value = (String) xpath.evaluate(nodeXPath, result.item(i), XPathConstants.STRING);
                    logger.log(POILogger.DEBUG, "Extracting with xpath " + nodeXPath + " : value is '" + value + "'");
                    XSSFRow row = table.getXSSFSheet().getRow(rowId);
                    if (row == null) {
                        row = table.getXSSFSheet().createRow(rowId);
                    }

                    XSSFCell cell = row.getCell(columnId);
                    if (cell == null) {
                        cell = row.createCell(columnId);
                    }
                    logger.log(POILogger.DEBUG, "Setting '" + value + "' to cell " + cell.getColumnIndex() + "-" + cell.getRowIndex() + " in sheet "
                                                    + table.getXSSFSheet().getSheetName());
                    cell.setCellValue(value.trim());
                }
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.poi.xssf.usermodel.XSSFCell

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.