从.xls中提取嵌入式excel(.xlsx)不适用于2016版excel

时间:2018-04-02 08:19:39

标签: java excel apache-poi

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFObjectData;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.Entry;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TestEmbedded {
   public static void main(String[] args) throws Exception {
       String path = "ParentExcel.xls";
       File file = new File(path);

       NPOIFSFileSystem fs = new NPOIFSFileSystem(file);
       HSSFWorkbook wb = new HSSFWorkbook(fs.getRoot(), true);

       for (HSSFObjectData obj : wb.getAllEmbeddedObjects()) {
       System.out.println(obj.getDirectory()+"***************HSSFObject data value************"+obj.getOLE2ClassName());
       String oleName = obj.getOLE2ClassName();
       System.out.println("Has directory Entry node"+obj.hasDirectoryEntry());
       DirectoryNode dn = (DirectoryNode)obj.getDirectory();
       Iterator<Entry> ab = dn.getEntries();
       System.out.println("The entries in the directory node are"+dn.getEntries()+"And also the count"+dn.getEntryCount());

       if(oleName.contains("Worksheet")){

          InputStream is ;
          Entry entry = ab.next();
          is = dn.createDocumentInputStream(entry);
          FileOutputStream fos = new FileOutputStream("EmbeddedWorkbook.xlsx");
          IOUtils.copy(is, fos);
          fos.close();
       }


       }

    }
}

上面的代码用于从.xls中提取嵌入的.xlsx。但它不适用2016版的excel。生成了Excel,但在打开excel时没有内容。enter image description here

0 个答案:

没有答案