poi读取word java poi - 电脑|办公 - 电脑办公-杀毒安全-网络-V3学习网
微商网
 
 
导航:首页 |电脑|办公|正文

poi读取word java poi

时间:2020-09-04 10:23:05
怎么使用JAVA,POI读写word文档 如何使用JAVA、POI读写word文档??能不能将一个word的内容完全读过来,放到一个新生成的word文件中去,要求能将word中的表格、图片等保留,格式
作者:

poi读取word

怎么使用JAVA,POI读写word文档

如何使用JAVA、POI读写word文档??能不能将一个word的内容完全读过来,放到一个新生成的word文件中去,要求能将word中的表格、图片等保留,格式不变。

最好能给个例子?网上多是很早以前的那个解决方法如下:,只能读文本内容,且新生成的word文件打开时总是要提示选择编码,不太好用,希望能有新的解决方案??!!poi操作word1.1 添加poi支持:包下载地址1.2 POI对Excel文件的读取操作比较方便,POI还提供对Word的DOC格式文件的读取。

但在它的发行版本中没有发布对Word支持的模块,需要另外下载一个POI的扩展的Jar包。

下载地址为;下载extractors-0.4_zip这个文件2、提取Doc文件内容 public static String readDoc(String doc) throws Exception {// 创建输入流读取DOC文件 FileInputStream in = new FileInputStream(new File(doc)); WordExtractor extractor = null; String text = null;// 创建WordExtractor extractor = new WordExtractor();// 对DOC文件进行提取 text = extractor.extractText(in); return text; } public static void main(String[] args) { try{ String text = WordReader.readDoc("c:/test.doc"); System.out.println(text); }catch(Exception e){ e.printStackTrace(); } }3、写入Doc文档 import java.io.ByteArrayInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.poifs.filesystem.DirectoryEntry; import org.apache.poi.poifs.filesystem.DocumentEntry; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class WordWriter { public static boolean writeDoc(String path, String content) { boolean w = false; try { // byte b[] = content.getBytes("ISO-8859-1"); byte b[] = content.getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(b); POIFSFileSystem fs = new POIFSFileSystem(); DirectoryEntry directory = fs.getRoot(); DocumentEntry de = directory.createDocument("WordDocument", bais); FileOutputStream ostream = new FileOutputStream(path); fs.writeFilesystem(ostream); bais.close(); ostream.close(); } catch (IOException e) { e.printStackTrace(); } return w; } public static void main(String[] args) throws Exception{ String wr=WordReader.readDoc("D:\\test.doc"); boolean b = writeDoc("D:\\result.doc",wr);

JAVA使用POI读写word 乱码

写 public static void main(String args[]) throws Exception { XWPFDocument doc = new XWPFDocument(); XWPFParagraph p1 = doc.createParagraph(); p1.setAlignment(ParagraphAlignment.CENTER); p1.setBorderBottom(Borders.DOUBLE); p1.setBorderTop(Borders.DOUBLE); p1.setBorderRight(Borders.DOUBLE); p1.setBorderLeft(Borders.DOUBLE); p1.setBorderBetween(Borders.SINGLE); p1.setVerticalAlignment(TextAlignment.TOP); XWPFRun r1 = p1.createRun(); r1.setBold(true); r1.setText("The quick brown fox"); r1.setBold(true); r1.setFontFamily("Courier"); r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH); r1.setTextPosition(100); XWPFParagraph p2 = doc.createParagraph(); p2.setAlignment(ParagraphAlignment.RIGHT); p2.setBorderBottom(Borders.DOUBLE); p2.setBorderTop(Borders.DOUBLE); p2.setBorderRight(Borders.DOUBLE); p2.setBorderLeft(Borders.DOUBLE); p2.setBorderBetween(Borders.SINGLE); XWPFRun r2 = p2.createRun(); r2.setText("jumped over the lazy dog"); r2.setStrike(true); r2.setFontSize(20); XWPFRun r3 = p2.createRun(); r3.setText("and went away"); r3.setStrike(true); r3.setFontSize(20); r3.setSubscript(VerticalAlign.SUPERSCRIPT); XWPFParagraph p3 = doc.createParagraph(); p3.setWordWrap(true); p3.setPageBreak(true); p3.setAlignment(ParagraphAlignment.BOTH); p3.setSpacingLineRule(LineSpacingRule.EXACT); p3.setIndentationFirstLine(600); XWPFRun r4 = p3.createRun(); r4.setTextPosition(20); r4.setText("To be, or not to be: that is the question: Whether "tis nobler in the mind to suffer The slings and arrows of outrageous fortune, Or to take arms against a sea of troubles, And by opposing end them? To die: to sleep; "); r4.addBreak(BreakType.PAGE); r4.setText("No more; and by a sleep to say we end The heart-ache and the thousand natural shocks That flesh is heir to, "tis a consummation Devoutly to be wish"d. To die, to sleep; To sleep: perchance to dream: ay, there"s the rub; ......."); r4.setItalic(true); XWPFRun r5 = p3.createRun(); r5.setTextPosition(-10); r5.setText("For in that sleep of death what dreams may come"); r5.addCarriageReturn(); r5.setText("When we have shuffled off this mortal coil,Must give us pause: there"s the respectThat makes calamity of so long life;"); r5.addBreak(); r5.setText("For who would bear the whips and scorns of time,The oppressor"s wrong, the proud man"s contumely,"); r5.addBreak(BreakClear.ALL); r5.setText("The pangs of despised love, the law"s delay,The insolence of office and the spurns......."); FileOutputStream out = new FileOutputStream("simple.docx"); doc.write(out); out.close(); }

java poi导出word 可以设置格式吗

1. 读取word 2003及word 2007需要的jar包2. 读取 2003 版本(.doc)的word文件相对来说比较简单,只需要 poi-3.5-beta6-.jar 和 poi-scratchpad-3.5-beta6-.jar 两个 jar 包即可, 而 2007 版本(.docx)就麻烦多,我说的这个麻烦不是我们写代码的时候麻烦,是要导入的 jar 包比较的多,有如下 7 个之多:3. 1. openxml4j-bin-beta.jar4. 2. poi-3.5-beta6-.jar5. 3. poi-ooxml-3.5-beta6-.jar6. 4 .dom4j-1.6.1.jar7. 5. geronimo-stax-api_1.0_spec-1.0.jar8. 6. ooxml-schemas-1.0.jar9. 7. xmlbeans-2.3.0.jar10. 其中 4-7 是 poi-ooxml-3.5-beta6-.jar 所依赖的 jar 包(在 poi-bin-3.5-beta6-.tar.gz 中的 ooxml-lib 目录下可以找到)。

11. 2.换行符号12. 硬换行:文件中换行,如果是键盘中使用了"enter"的换行。

13. 软换行:文件中一行的字符数容量有限,当字符数量超过一定值时,会自动切到下行显示。

14. 对程序来说,硬换行才是可以识别的、确定的换行,软换行与字体大小、缩进有关。

15. 3.读取的注意事项16. 值得注意的是: POI 在读取不会读取 word 文件中的图片信息; 还有就是对于 2007 版的 word(.docx), 如果 word 文件中有表格,所有表格中的数据都会在读取出来的字符串的最后。

17. 4.读取word文本内容代码1 import java.io.File;2 import java.io.FileInputStream;3 import java.io.InputStream;4 5 import org.apache.poi.POIXMLDocument;6 import org.apache.poi.POIXMLTextExtractor;7 import org.apache.poi.hwpf.extractor.WordExtractor;8 import org.apache.poi.openxml4j.opc.OPCPackage;9 import org.apache.poi.xwpf.extractor.XWPFWordExtractor;10 11 public class Test {12 public static void main(String[] args) {13 try {14 InputStream is = new FileInputStream(new File("2003.doc"));15 WordExtractor ex = new WordExtractor(is);16 String text2003 = ex.getText();17 System.out.println(text2003);18 19 OPCPackage opcPackage = POIXMLDocument.openPackage("2007.docx");20 POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);21 String text2007 = extractor.getText();22 System.out.println(text2007);23 24 } catch (Exception e) {25 e.printStackTrace();26 }27 }28 }

JAVA读取WORD,EXCEL,PDF文件的方法是什么呢?

JAVA读取WORD,EXCEL,POWERPOINT,PDF文件的方法 OFFICE文档使用POI控件,PDF可以使用PDFBOX0.7.3控件,完全支持中文,用XPDF也行,不过感觉PDFBOX比较好,而且作者也在更新。

水平有限,万望各位指正 WORD: import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.poi.hwpf.extractor.WordExtractor; import java.io.File; import java.io.InputStream; import java.io.FileInputStream; import com.search.code.Index; public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException { String bodyText = null; try { WordExtractor ex = new WordExtractor(is);//is是WORD文件的InputStream bodyText = ex.getText(); if(!bodyText.equals("")){ index.AddIndex(url, title, bodyText); } }catch (DocCenterException e) { throw new DocCenterException("无法从该Mocriosoft Word文档中提取内容", e); }catch(Exception e){ e.printStackTrace(); } } return null; } Excel: import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.poi.hwpf.extractor.WordExtractor; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFCell; import java.io.File; import java.io.InputStream; import java.io.FileInputStream; import com.search.code.Index; public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException { StringBuffer content = new StringBuffer(); try{

要用Java POI读取Excel文件中的数据,并且实现对数据的格式校验,...

快速样式, 加粗,右键选修改时可以看到它的定义为, 优先级, 段落间距段前: 二号.., 段后, 与下段同页: 10, 段中不分页可以的, 字距调整二号: 正文.5 磅, 样式: 17 磅, 行距, 。

所谓标题1就是一种样式: 多倍行距 2: 字体, 基于.41 字行: 16, 1 级: 链接

怎样才能把word文件读取并显示在java中?请各位指教!

经过一番选择还是折中点采用rtf最好,毕竟rtf是开源格式java读取word文档时,虽然网上介绍了很多插件poi,各种格式都可以设定、java2Word,不需要借助任何插件,去他们论坛找高人解决也说不出原因,项目部署用它有点玄;itxt好像写很方便但是我查了好久资料没有见到过关于读的好法,操作方法完全一致,有的机器不报错,有的报错,比较诡异,我曾经在不同的机器上试过,做项目不太敢用);java2Word、jacob容易报错找不到注册,poi无法读取格式(新的API估计行好像还在处于研发阶段,不太稳定、itext等等、jacob,只需基本IO操作外加编码转换即可。

rtf格式文件表面看来和doc没啥区别,都可以用word打开。

具体代码可以参考:/...

大家还关注
阅读排行
推荐阅读