代码之家  ›  专栏  ›  技术社区  ›  Venkat Sadasivam

是否可以从Microsoft word文档导入数据?

  •  2
  • Venkat Sadasivam  · 技术社区  · 16 年前

    通常使用CSV和excel文件格式导入数据,因为通过编程很容易提取数据。我的用户不喜欢excel文件格式的数据输入,他们喜欢word文档。但我不知道如何从microsoftword文档中提取数据。有人试过吗?你有什么建议吗?

    找到了这个 link

    3 回复  |  直到 16 年前
        1
  •  5
  •   Jonathon Faust    16 年前

    Apache POI 这使它比其他方法更容易。

        2
  •  2
  •   gmhk    16 年前

    如果我们想到microsoftofficeworddocument,Java没有任何内置类来处理这个问题,但是apachefoundation开发的apachepoi包提供了用Java读取microsoftword文档的能力。

    import org.apache.poi.poifs.filesystem.*;
    import org.apache.poi.hwpf.*;
    import org.apache.poi.hwpf.extractor.*;
    import java.io.*;
    
    public class readDoc
    {
        public static void main( String[] args )
        {
            String filesname = "Hello.doc";
            POIFSFileSystem fs = null;
            try
            {
                      fs = new POIFSFileSystem(new FileInputStream(filesname; 
                      //Couldn't close the braces at the end as my site did not allow it to close
    
                      HWPFDocument doc = new HWPFDocument(fs);
    
              WordExtractor we = new WordExtractor(doc);
    
              String[] paragraphs = we.getParagraphText();
    
              System.out.println( "Word Document has " + paragraphs.length + " paragraphs" );
              for( int i=0; i<paragraphs .length; i++ ) {
                paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n","");
                        System.out.println( "Length:"+paragraphs[ i ].length());
              }
                    }
                    catch(Exception e) { 
                        e.printStackTrace();
                    }
             }
    }
    

    你仍然可以从这里参考更多 link

        3
  •  0
  •   Community Mohan Dere    9 年前

    我喜欢这样的回答:

    ktingle