Search in sources :

Example 21 with XSSFCell

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

the class XSSFExportToXml method exportToXML.

/**
     * Exports the data in an XML stream
     *
     * @param os OutputStream in which will contain the output XML
     * @param encoding the output charset encoding
     * @param validate if true, validates the XML against the XML Schema
     * @throws SAXException If validating the document fails
     * @throws TransformerException If transforming the document fails
     */
public void exportToXML(OutputStream os, String encoding, boolean validate) throws SAXException, TransformerException {
    List<XSSFSingleXmlCell> singleXMLCells = map.getRelatedSingleXMLCell();
    List<XSSFTable> tables = map.getRelatedTables();
    String rootElement = map.getCtMap().getRootElement();
    Document doc = DocumentHelper.createDocument();
    final Element root;
    if (isNamespaceDeclared()) {
        root = doc.createElementNS(getNamespace(), rootElement);
    } else {
        root = doc.createElementNS("", rootElement);
    }
    doc.appendChild(root);
    List<String> xpaths = new Vector<String>();
    Map<String, XSSFSingleXmlCell> singleXmlCellsMappings = new HashMap<String, XSSFSingleXmlCell>();
    Map<String, XSSFTable> tableMappings = new HashMap<String, XSSFTable>();
    for (XSSFSingleXmlCell simpleXmlCell : singleXMLCells) {
        xpaths.add(simpleXmlCell.getXpath());
        singleXmlCellsMappings.put(simpleXmlCell.getXpath(), simpleXmlCell);
    }
    for (XSSFTable table : tables) {
        String commonXPath = table.getCommonXpath();
        xpaths.add(commonXPath);
        tableMappings.put(commonXPath, table);
    }
    Collections.sort(xpaths, this);
    for (String xpath : xpaths) {
        XSSFSingleXmlCell simpleXmlCell = singleXmlCellsMappings.get(xpath);
        XSSFTable table = tableMappings.get(xpath);
        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);
                    mapCellOnNode(cell, currentNode);
                    //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);
                            mapCellOnNode(cell, currentNode);
                        }
                    }
                }
            }
        }
    /*else {
                // TODO:  implement filtering management in xpath
            }*/
    }
    boolean isValid = true;
    if (validate) {
        isValid = isValid(doc);
    }
    if (isValid) {
        /////////////////
        //Output the XML
        //set up a transformer
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty(OutputKeys.ENCODING, encoding);
        //create string from xml tree
        StreamResult result = new StreamResult(os);
        DOMSource source = new DOMSource(doc);
        trans.transform(source, result);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) XSSFTable(org.apache.poi.xssf.usermodel.XSSFTable) XSSFSingleXmlCell(org.apache.poi.xssf.usermodel.helpers.XSSFSingleXmlCell) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFXmlColumnPr(org.apache.poi.xssf.usermodel.helpers.XSSFXmlColumnPr) Vector(java.util.Vector) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell)

Example 22 with XSSFCell

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

the class TestWorkbookProtection method testEncryptDecrypt.

@Test
public void testEncryptDecrypt() throws Exception {
    final String password = "abc123";
    final String sheetName = "TestSheet1";
    final String cellValue = "customZipEntrySource";
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet1 = workbook.createSheet(sheetName);
    XSSFRow row1 = sheet1.createRow(1);
    XSSFCell cell1 = row1.createCell(1);
    cell1.setCellValue(cellValue);
    File tf1 = TempFile.createTempFile("poitest", ".xlsx");
    FileOutputStream fos1 = new FileOutputStream(tf1);
    workbook.write(fos1);
    IOUtils.closeQuietly(fos1);
    POIFSFileSystem poiFileSystem = new POIFSFileSystem();
    EncryptionInfo encryptionInfo = new EncryptionInfo(EncryptionMode.agile);
    Encryptor enc = encryptionInfo.getEncryptor();
    enc.confirmPassword(password);
    FileInputStream fis = new FileInputStream(tf1);
    OPCPackage opc = OPCPackage.open(fis);
    IOUtils.closeQuietly(fis);
    try {
        OutputStream os = enc.getDataStream(poiFileSystem);
        opc.save(os);
        IOUtils.closeQuietly(os);
    } finally {
        IOUtils.closeQuietly(opc);
    }
    tf1.delete();
    FileOutputStream fos2 = new FileOutputStream(tf1);
    poiFileSystem.writeFilesystem(fos2);
    IOUtils.closeQuietly(fos2);
    workbook.close();
    fis = new FileInputStream(tf1);
    POIFSFileSystem poiFileSystem2 = new POIFSFileSystem(fis);
    IOUtils.closeQuietly(fis);
    EncryptionInfo encryptionInfo2 = new EncryptionInfo(poiFileSystem2);
    Decryptor decryptor = encryptionInfo2.getDecryptor();
    decryptor.verifyPassword(password);
    XSSFWorkbook workbook2 = new XSSFWorkbook(decryptor.getDataStream(poiFileSystem2));
    workbook2.close();
    tf1.delete();
}
Also used : Decryptor(org.apache.poi.poifs.crypt.Decryptor) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Encryptor(org.apache.poi.poifs.crypt.Encryptor) FileInputStream(java.io.FileInputStream) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) TempFile(org.apache.poi.util.TempFile) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

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