📄 poiworddochandler.java
字号:
package lia.handlingtypes.msdoc;import lia.handlingtypes.framework.DocumentHandler;import lia.handlingtypes.framework.DocumentHandlerException;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.poi.hdf.extractor.WordDocument;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.io.StringWriter;import java.io.PrintWriter;public class POIWordDocHandler implements DocumentHandler { public Document getDocument(InputStream is) throws DocumentHandlerException { String bodyText = null; try { WordDocument wd = new WordDocument(is); StringWriter docTextWriter = new StringWriter(); wd.writeAllText(new PrintWriter(docTextWriter)); docTextWriter.close(); bodyText = docTextWriter.toString(); } catch (Exception e) { throw new DocumentHandlerException( "Cannot extract text from a Word document", e); } if ((bodyText != null) && (bodyText.trim().length() > 0)) { Document doc = new Document(); doc.add(Field.UnStored("body", bodyText)); return doc; } return null; } public static void main(String[] args) throws Exception { POIWordDocHandler handler = new POIWordDocHandler(); Document doc = handler.getDocument( new FileInputStream(new File(args[0]))); System.out.println(doc); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -