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

时间:2010-06-30 02:22:08

标签: java ms-word

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

找到了这个link,但不确定如何创建这样的模板以及在Java中使用哪些API来提取值。

3 个答案:

答案 0 :(得分:5)

Apache POI这样的库让它变得更容易。

答案 1 :(得分:2)

如果我们想到Microsoft Office Word文档Java没有任何构建类来处理这个问题,但Apache Foundation开发的Apache POI包使您能够以Java语言阅读Microsoft Word文档。

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

我希望这会对你有所帮助

答案 2 :(得分:0)

我喜欢这个答案来自评论:

您可能想要探索InfoPath,它是MS表单技术,您可以从MS Word导入表单。 - ktingle 6月30日2:32