Search in sources :

Example 11 with XSSFWorkbook

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

the class XSSFFileHandler method handleFile.

@Override
public void handleFile(InputStream stream, String path) throws Exception {
    // ignore password protected files
    if (POIXMLDocumentHandler.isEncrypted(stream))
        return;
    final XSSFWorkbook wb;
    // make sure the potentially large byte-array is freed up quickly again
    {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        IOUtils.copy(stream, out);
        final byte[] bytes = out.toByteArray();
        checkXSSFReader(OPCPackage.open(new ByteArrayInputStream(bytes)));
        wb = new XSSFWorkbook(new ByteArrayInputStream(bytes));
    }
    // use the combined handler for HSSF/XSSF
    handleWorkbook(wb);
    // TODO: some documents fail currently...
    //XSSFFormulaEvaluator evaluator = new XSSFFormulaEvaluator(wb);
    //evaluator.evaluateAll();
    // also verify general POIFS-stuff
    new POIXMLDocumentHandler().handlePOIXMLDocument(wb);
    // and finally ensure that exporting to XML works
    exportToXML(wb);
    // this allows to trigger a heap-dump at this point to see which memory is still allocated
    //HeapDump.dumpHeap("/tmp/poi.hprof", false);
    wb.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 12 with XSSFWorkbook

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

the class XSSFSave method main.

public static void main(String[] args) throws Exception {
    for (int i = 0; i < args.length; i++) {
        OPCPackage pkg = OPCPackage.open(args[i]);
        XSSFWorkbook wb = new XSSFWorkbook(pkg);
        int sep = args[i].lastIndexOf('.');
        String outfile = args[i].substring(0, sep) + "-save.xls" + (wb.isMacroEnabled() ? "m" : "x");
        FileOutputStream out = new FileOutputStream(outfile);
        wb.write(out);
        out.close();
        pkg.close();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 13 with XSSFWorkbook

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

the class WorkingWithPictures method main.

public static void main(String[] args) throws IOException {
    //create a new workbook
    //or new HSSFWorkbook();
    Workbook wb = new XSSFWorkbook();
    try {
        CreationHelper helper = wb.getCreationHelper();
        //add a picture in this workbook.
        InputStream is = new FileInputStream(args[0]);
        byte[] bytes = IOUtils.toByteArray(is);
        is.close();
        int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
        //create sheet
        Sheet sheet = wb.createSheet();
        //create drawing
        Drawing<?> drawing = sheet.createDrawingPatriarch();
        //add a picture shape
        ClientAnchor anchor = helper.createClientAnchor();
        anchor.setCol1(1);
        anchor.setRow1(1);
        Picture pict = drawing.createPicture(anchor, pictureIdx);
        //auto-size picture
        pict.resize(2);
        //save workbook
        String file = "picture.xls";
        if (wb instanceof XSSFWorkbook) {
            // NOSONAR
            file += "x";
        }
        OutputStream fileOut = new FileOutputStream(file);
        try {
            wb.write(fileOut);
        } finally {
            fileOut.close();
        }
    } finally {
        wb.close();
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) FileInputStream(java.io.FileInputStream) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) Picture(org.apache.poi.ss.usermodel.Picture) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Sheet(org.apache.poi.ss.usermodel.Sheet)

Example 14 with XSSFWorkbook

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

the class LoadEmbedded method loadEmbedded.

public static void loadEmbedded(XSSFWorkbook workbook) throws IOException, InvalidFormatException, OpenXML4JException, XmlException {
    for (PackagePart pPart : workbook.getAllEmbedds()) {
        String contentType = pPart.getContentType();
        if (contentType.equals("application/vnd.ms-excel")) {
            // Excel Workbook - either binary or OpenXML
            HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(pPart.getInputStream());
            embeddedWorkbook.close();
        } else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
            // Excel Workbook - OpenXML file format
            XSSFWorkbook embeddedWorkbook = new XSSFWorkbook(pPart.getInputStream());
            embeddedWorkbook.close();
        } else if (contentType.equals("application/msword")) {
            // Word Document - binary (OLE2CDF) file format
            HWPFDocument document = new HWPFDocument(pPart.getInputStream());
            document.close();
        } else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
            // Word Document - OpenXML file format
            XWPFDocument document = new XWPFDocument(pPart.getInputStream());
            document.close();
        } else if (contentType.equals("application/vnd.ms-powerpoint")) {
            // PowerPoint Document - binary file format
            HSLFSlideShow slideShow = new HSLFSlideShow(pPart.getInputStream());
            slideShow.close();
        } else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) {
            // PowerPoint Document - OpenXML file format
            XMLSlideShow slideShow = new XMLSlideShow(pPart.getInputStream());
            slideShow.close();
        } else {
            // Any other type of embedded object.
            System.out.println("Unknown Embedded Document: " + contentType);
            InputStream inputStream = pPart.getInputStream();
            inputStream.close();
        }
    }
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) InputStream(java.io.InputStream) XMLSlideShow(org.apache.poi.xslf.usermodel.XMLSlideShow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 15 with XSSFWorkbook

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

the class HybridStreaming method main.

public static void main(String[] args) throws IOException, SAXException {
    InputStream sourceBytes = new FileInputStream("workbook.xlsx");
    XSSFWorkbook workbook = new XSSFWorkbook(sourceBytes) {

        /** Avoid DOM parse of large sheet */
        @Override
        public void parseSheet(java.util.Map<String, XSSFSheet> shIdMap, CTSheet ctSheet) {
            if (!SHEET_TO_STREAM.equals(ctSheet.getName())) {
                super.parseSheet(shIdMap, ctSheet);
            }
        }
    };
    // Having avoided a DOM-based parse of the sheet, we can stream it instead.
    ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(workbook.getPackage());
    new XSSFSheetXMLHandler(workbook.getStylesSource(), strings, createSheetContentsHandler(), false);
    workbook.close();
    sourceBytes.close();
}
Also used : ReadOnlySharedStringsTable(org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable) CTSheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) FileInputStream(java.io.FileInputStream) XSSFSheetXMLHandler(org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler)

Aggregations

XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)208 Test (org.junit.Test)95 Sheet (org.apache.poi.ss.usermodel.Sheet)72 Workbook (org.apache.poi.ss.usermodel.Workbook)72 FileOutputStream (java.io.FileOutputStream)48 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)47 Cell (org.apache.poi.ss.usermodel.Cell)36 Row (org.apache.poi.ss.usermodel.Row)35 File (java.io.File)28 ByteArrayInputStream (java.io.ByteArrayInputStream)23 XSSFMap (org.apache.poi.xssf.usermodel.XSSFMap)22 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)19 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)17 POIXMLDocumentPart (org.apache.poi.POIXMLDocumentPart)16 CTChart (org.openxmlformats.schemas.drawingml.x2006.chart.CTChart)16 XSSFChart (org.apache.poi.xssf.usermodel.XSSFChart)15 IOException (java.io.IOException)14 CellStyle (org.apache.poi.ss.usermodel.CellStyle)14 XSSFDrawing (org.apache.poi.xssf.usermodel.XSSFDrawing)14