如何从doc文件中读取嵌入的对象?

时间:2014-08-02 08:48:56

标签: java apache-poi

我正在尝试使用POIFS文件系统读取.doc文件中的嵌入文档。根据这个,有一个ObjectPool目录,其中包含所有嵌入的文档,特别是对于doc文件 我找到了目录,但不知道阅读这些文件的方法。
请以任何方式阅读这些文件。如果POIFS不合适,请建议任何其他图书馆。

我的代码是:

 public static void ReadCSV(String fileName) throws IOException{
    FileInputStream myInput = new FileInputStream(fileName);
    POIFSFileSystem fs = new POIFSFileSystem(myInput);
    HSSFWorkbook workbook = new HSSFWorkbook(fs);
    for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) {
        //the OLE2 Class Name of the object
        System.out.println("Objects : "+ obj.getOLE2ClassName()+ "   2 .");
        String oleName = obj.getOLE2ClassName(); 
        if (oleName.equals("Worksheet")) {
            // some code to process embedded excel file;
        } else if (oleName.equals("Document")) {
            System.out.println("Document");
            DirectoryNode dn = (DirectoryNode) obj.getDirectory();
            HWPFDocument embeddedWordDocument = new HWPFDocument(dn,fs);
            System.out.println("Doc : " + embeddedWordDocument.getRange().text());
            // want to extract document not text into a doc file
            //************************
            FileOutputStream fos = new FileOutputStream("E:\\log.txt");
            fos.write(text.getBytes());
            //************************
         }  else if (oleName.equals("Presentation")) {
            // some code to process embedded power point file;
         } else {
            // some code to process other kind of embedded files;
      }
   }
}

public static void ReadCSV(String fileName) throws IOException{ FileInputStream myInput = new FileInputStream(fileName); POIFSFileSystem fs = new POIFSFileSystem(myInput); HSSFWorkbook workbook = new HSSFWorkbook(fs); for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) { //the OLE2 Class Name of the object System.out.println("Objects : "+ obj.getOLE2ClassName()+ " 2 ."); String oleName = obj.getOLE2ClassName(); if (oleName.equals("Worksheet")) { // some code to process embedded excel file; } else if (oleName.equals("Document")) { System.out.println("Document"); DirectoryNode dn = (DirectoryNode) obj.getDirectory(); HWPFDocument embeddedWordDocument = new HWPFDocument(dn,fs); System.out.println("Doc : " + embeddedWordDocument.getRange().text()); // want to extract document not text into a doc file //************************ FileOutputStream fos = new FileOutputStream("E:\\log.txt"); fos.write(text.getBytes()); //************************ } else if (oleName.equals("Presentation")) { // some code to process embedded power point file; } else { // some code to process other kind of embedded files; } } }

0 个答案:

没有答案