Search code examples
javaexcelapache-poioleembedding

How to export embeded file which from excel using POI?


I have made a java basic program written below, which is making 3 kind of files (ppt,doc,txt) embedded in Excel sheet using Apache POI. Now this file I want to Export with its original format. How to do this?

Reference link is Embed files into Excel using Apache POI. I have made program from this link.

In short I want export functionality on Embedded file.

I have tried above problem using give below code but it not working for exporting embedded file in excel sheet:

Here this is the code which is tried to solve:

public static void main(String[] args) throws IOException {
    String fileName = "ole_ppt_in_xls.xls";
    ReadExcel(fileName);
}

 public static void ReadExcel(String fileName) throws IOException {
    FileInputStream inputFileStream = new FileInputStream(fileName);

    POIFSFileSystem fs = new POIFSFileSystem(inputFileStream);
    HSSFWorkbook workbook = new HSSFWorkbook(fs);

    for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) {
        // the OLE2 Class Name of the object
        String oleName = obj.getOLE2ClassName();
        System.out.println(oleName);
        if (oleName.equals("Worksheet")) {
            System.out.println("Worksheet");
            DirectoryNode dn = (DirectoryNode) obj.getDirectory();
            HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(dn, fs, false);

        } else if (oleName.equals("Document")) {
            System.out.println("Document");
            DirectoryNode dn = (DirectoryNode) obj.getDirectory();
            HWPFDocument embeddedWordDocument = new HWPFDocument(dn, fs);
        } else if (oleName.equals("Presentation")) {
            System.out.println("Presentation");
            DirectoryNode dn = (DirectoryNode) obj.getDirectory();
            SlideShow embeddedPowerPointDocument = new SlideShow(
                    new HSLFSlideShow(dn, fs));
        } else if (oleName.equals("Presentation")) {
            System.out.println("Presentation");
            DirectoryNode dn = (DirectoryNode) obj.getDirectory();
            SlideShow embeddedPowerPointDocument = new SlideShow(
                    new HSLFSlideShow(dn, fs));
        }else {
            System.out.println("Else part ");
            if (obj.hasDirectoryEntry()) {
                System.out.println("obj.hasDirectoryEntry()"+obj.hasDirectoryEntry());
                // The DirectoryEntry is a DocumentNode. Examine its entries

                DirectoryNode dn = (DirectoryNode) obj.getDirectory();
                for (Iterator entries = dn.getEntries(); entries.hasNext();) {
                    Entry entry = (Entry) entries.next();
                    System.out.println(oleName + "." + entry.getName());
                }
            } else {
                System.out.println("Else part 22");
                byte[] objectData = obj.getObjectData();
            }
        }
    }

}

Output screen of above program:enter image description here

So ,how to exporting functionality implement?


Solution

  • import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.util.ArrayList;
    
    /**
     * Demonstrates how you can extract embedded data from a .xlsx file
     */
    public class GetEmbedded {
    
        public static void main(String[] args) throws Exception {
            String path = "SomeExcelFile.xlsx"
            XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(path)));
    
                 for (PackagePart pPart : workbook.getAllEmbedds()) {
                                String contentType = pPart.getContentType();
    
                                if (contentType.equals("application/vnd.ms-excel")) { //This is to read xls workbook embedded to xlsx file
                                    HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(pPart.getInputStream());
                                    int countOfSheetXls=embeddedWorkbook.getNumberOfSheets();
    
                     }
                                else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) { //This is to read xlsx workbook embedded to xlsx file
                                     if(pPart.getPartName().getName().equals("/xl/embeddings/Microsoft_Excel_Worksheet12.xlsx")){
                                     //"/xl/embeddings/Microsoft_Excel_Worksheet12.xlsx" - Can read an Excel from a particular sheet 
                                    // This is the worksheet from the Parent Excel-sheet-12
    
                                         XSSFWorkbook embeddedWorkbook = new XSSFWorkbook(pPart.getInputStream());
                                         int countOfSheetXlsx=embeddedWorkbook.getNumberOfSheets();
                                         ArrayList<String> sheetNames= new ArrayList<String>();
                                            for(int i=0;i<countOfSheetXlsx;i++){
                                            String name=workbook.getSheetName(i);
                                            sheetNames.add(name);
                                            }
                                    }
                                }
                    }
         }
    }