[Reprint]java get the text inside word

Source: Internet
Author: User
Tags save file

Demand Scenarios

Developing a Web Office system if you need to work with a large number of Word documents (such as thousands of documents), users must ask for a document that contains some keywords, which requires the ability to read text in Word, ignoring text styles, tables, pictures, and so on.

Scenario Analysis

Scenario One: Use Apache POI technology to store the text of documents on all servers in the database, and find documents using SQL statements to retrieve document text stored in the data containing keywords to search for related documents. However, Microsoft Word now has two document formats, Doc and docx, and there are considerable differences in the format in which the two versions store data. The research found that Apache POI provides two different API interfaces for Doc and docx, requiring different code for both document formats, a complex format for Word documents, and code that reads the contents of Word documents, which can cause pressure on the server. And it is not possible to allow users to work with Word documents online.

POI Home Address: https://poi.apache.org/

Scenario Two: Use the Getdocumenttext method of the Filesaver object of the Pageoffice component to get plain text content in a Word document, and if you call Pageoffice to implement this functionality, you can also implement online editing of the Word file.

  

Implementation steps

1. Call Pageoffice to open Word file online, for example: Test.doc

Pageofficectrl poctrl=new Pageofficectrl (request);//Set Server Page Poctrl.setserverpage (request.getcontextpath () + "/ Poserver.zz ");//Set save page to savefile.jsp, or savefile.do savefile.action The action method or Requestmapping method can be Poctrl.setsavefilepage ("savefile.jsp");//Open Word document Poctrl.webopen ("Doc/test.doc", Openmodetype.docnormaledit, "Tom");

2. Perform the save file and get the plain text content in the document in the page (savefile.jsp) or method that handles the save operation:

Filesaver fs=new Filesaver (request,response); Fs.savetofile (Request.getsession (). Getservletcontext (). GetRealPath ( "doc/") + "/" +fs.getfilename ());//save file string strdocumenttext = Fs.getdocumenttext ();//Get the plain text content of the document without any additional formatting//-Write code, Save the text content of the document to the database-//......fs.close ();

3. When a full-text search is required, simply make a SQL query for the database field that holds the plain-content of the Word file in the database.

[Reprint]java get the text inside word

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.