开发者

Is it possible to import data from Microsoft word document?

开发者 https://www.devze.com 2023-01-05 17:05 出处:网络
Usually CSV and excel file format will be used to import data as it is easy to extract data programatically. My users doesn\'t like excel file format for data entry, they like word document. But I am

Usually CSV and excel file format will be used to import data as it is easy to extract data programatically. My users doesn't like excel file format for data entry, they like word document. But I am not sure how to extract data from Microsoft word document. Has anyone tried? do you have any suggestions?

Found this link, but not sure how to create such template and what API to use in Java to extrac开发者_Python百科t values.


There are libraries like Apache POI that make it easier than it would otherwise be.


if we think of Microsoft Office Word document Java does not have any in build classes to handle this but Apache POI Package developed by Apache Foundation gives you the power of reading Microsoft Word document in Java.

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();
                }
         }
}

Still you can refer more from this link

I hope this helps to you


I like this answer came in comments:

You might want to explore InfoPath, its the MS forms technology and you can import forms from MS Word. – ktingle Jun 30 at 2:32

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号