Search in sources :

Example 1 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project OpenRefine by OpenRefine.

the class ExcelImporter method createParserUIInitializationData.

@Override
public JSONObject createParserUIInitializationData(ImportingJob job, List<JSONObject> fileRecords, String format) {
    JSONObject options = super.createParserUIInitializationData(job, fileRecords, format);
    JSONArray sheetRecords = new JSONArray();
    JSONUtilities.safePut(options, "sheetRecords", sheetRecords);
    try {
        if (fileRecords.size() > 0) {
            JSONObject firstFileRecord = fileRecords.get(0);
            File file = ImportingUtilities.getFile(job, firstFileRecord);
            InputStream is = new FileInputStream(file);
            if (!is.markSupported()) {
                is = new PushbackInputStream(is, 8);
            }
            try {
                Workbook wb = POIXMLDocument.hasOOXMLHeader(is) ? new XSSFWorkbook(is) : new HSSFWorkbook(new POIFSFileSystem(is));
                int sheetCount = wb.getNumberOfSheets();
                boolean hasData = false;
                for (int i = 0; i < sheetCount; i++) {
                    Sheet sheet = wb.getSheetAt(i);
                    int rows = sheet.getLastRowNum() - sheet.getFirstRowNum() + 1;
                    JSONObject sheetRecord = new JSONObject();
                    JSONUtilities.safePut(sheetRecord, "name", sheet.getSheetName());
                    JSONUtilities.safePut(sheetRecord, "rows", rows);
                    if (hasData) {
                        JSONUtilities.safePut(sheetRecord, "selected", false);
                    } else if (rows > 1) {
                        JSONUtilities.safePut(sheetRecord, "selected", true);
                        hasData = true;
                    }
                    JSONUtilities.append(sheetRecords, sheetRecord);
                }
            } finally {
                is.close();
            }
        }
    } catch (IOException e) {
        logger.error("Error generating parser UI initialization data for Excel file", e);
    } catch (IllegalArgumentException e) {
        logger.error("Error generating parser UI initialization data for Excel file (only Excel 97 & later supported)", e);
    } catch (POIXMLException e) {
        logger.error("Error generating parser UI initialization data for Excel file - invalid XML", e);
    }
    return options;
}
Also used : PushbackInputStream(java.io.PushbackInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JSONArray(org.json.JSONArray) IOException(java.io.IOException) POIXMLException(org.apache.poi.POIXMLException) FileInputStream(java.io.FileInputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) JSONObject(org.json.JSONObject) PushbackInputStream(java.io.PushbackInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) File(java.io.File) Sheet(org.apache.poi.ss.usermodel.Sheet)

Example 2 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.

the class EmbeddedObjects method main.

@SuppressWarnings("unused")
public static void main(String[] args) throws Exception {
    POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(args[0]));
    HSSFWorkbook workbook = new HSSFWorkbook(fs);
    for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) {
        //the OLE2 Class Name of the object
        String oleName = obj.getOLE2ClassName();
        DirectoryNode dn = (obj.hasDirectoryEntry()) ? (DirectoryNode) obj.getDirectory() : null;
        Closeable document = null;
        if (oleName.equals("Worksheet")) {
            document = new HSSFWorkbook(dn, fs, false);
        } else if (oleName.equals("Document")) {
            document = new HWPFDocument(dn);
        } else if (oleName.equals("Presentation")) {
            document = new HSLFSlideShow(dn);
        } else {
            if (dn != null) {
                // The DirectoryEntry is a DocumentNode. Examine its entries to find out what it is
                for (Entry entry : dn) {
                    String name = entry.getName();
                }
            } else {
                // There is no DirectoryEntry
                // Recover the object's data from the HSSFObjectData instance.
                byte[] objectData = obj.getObjectData();
            }
        }
        if (document != null) {
            document.close();
        }
    }
    workbook.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Entry(org.apache.poi.poifs.filesystem.Entry) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) Closeable(java.io.Closeable) HSSFObjectData(org.apache.poi.hssf.usermodel.HSSFObjectData) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) FileInputStream(java.io.FileInputStream) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 3 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.

the class HPSFFileHandler method handleFile.

@Override
public void handleFile(InputStream stream, String path) throws Exception {
    Assume.assumeFalse(EXCLUDES_HANDLE_FILE.contains(path));
    POIFSFileSystem poifs = new POIFSFileSystem(stream);
    HPSFPropertiesOnlyDocument hpsf = new HPSFPropertiesOnlyDocument(poifs);
    DocumentSummaryInformation dsi = hpsf.getDocumentSummaryInformation();
    SummaryInformation si = hpsf.getSummaryInformation();
    boolean hasDSI = hasPropertyStream(poifs, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    boolean hasSI = hasPropertyStream(poifs, SummaryInformation.DEFAULT_STREAM_NAME);
    assertEquals(hasDSI, dsi != null);
    assertEquals(hasSI, si != null);
    handlePOIDocument(hpsf);
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) SummaryInformation(org.apache.poi.hpsf.SummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) HPSFPropertiesOnlyDocument(org.apache.poi.hpsf.HPSFPropertiesOnlyDocument)

Example 4 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.

the class POIFSFileHandler method handlePOIDocument.

protected void handlePOIDocument(POIDocument doc) throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    doc.write(out);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    POIFSFileSystem fs = new POIFSFileSystem(in);
    handlePOIFSFileSystem(fs);
    fs.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with POIFSFileSystem

use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.

the class POIFSFileHandler method handleFile.

@Override
public void handleFile(InputStream stream, String path) throws Exception {
    POIFSFileSystem fs = new POIFSFileSystem(stream);
    try {
        handlePOIFSFileSystem(fs);
        handleHPSFProperties(fs);
    } finally {
        fs.close();
    }
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem)

Aggregations

POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)121 Test (org.junit.Test)58 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)38 InputStream (java.io.InputStream)36 ByteArrayInputStream (java.io.ByteArrayInputStream)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 FileInputStream (java.io.FileInputStream)31 File (java.io.File)25 OPOIFSFileSystem (org.apache.poi.poifs.filesystem.OPOIFSFileSystem)15 FileOutputStream (java.io.FileOutputStream)14 OutputStream (java.io.OutputStream)14 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)13 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)13 TempFile (org.apache.poi.util.TempFile)13 IOException (java.io.IOException)12 MutablePropertySet (org.apache.poi.hpsf.MutablePropertySet)7 MutableSection (org.apache.poi.hpsf.MutableSection)7 HashMap (java.util.HashMap)6 DocumentEntry (org.apache.poi.poifs.filesystem.DocumentEntry)6 NDocumentOutputStream (org.apache.poi.poifs.filesystem.NDocumentOutputStream)6