用户登录
用户注册

分享至

poi读取按段落word内容 poi 读取word中的段落

  • 作者: 达?矢抾哆拉?
  • 来源: 51数据库
  • 2020-04-14

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

如何使用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使用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(); }

poi读取Excel时,如果单元格设置的是数字格式,如何解决整数与小数...

&nbsp.getSheetAt(0); break; HSSFCell.CELL_TYPE_FORMULA: System; System.out.println( cellFormatted &nbsp.println(oneCell.out;HSSFCell.CELL_TYPE_NUMERIC: HSSFDataFormatter == { continue; &nbsp可以试试下面这段代码:Workbook excelWB continue; }// oneRow &nbsp.out.CELL_TYPE_STRING: System; &nbsp.println(oneCell; 获取第几列对象 oneCell new XSSFWorkbook(excelFile);oneSheet =&nbsp.getCellType()) = null;new HSSFDataFormatter(); String cellFormatted = dataFormatter.formatCellValue(oneCell);excelWB =&nbsp.getStringCellValue());); break; = oneSheet; &nbsp.getCell(1); if (oneCell == null)&nbsp.getCellFormula()); case case break.getRow(rowNum); if (oneRow { case HSSFCell; = oneRow;null) { dataFormatter = excelWB; } switch (oneCell;case&nbsp...

Java POI读取Excel的时候怎么按列读取

实现代码如下:public class Word2Html { public static void main(String argv[]) { try { //word 路径 html输出路径 convert2Html("D:/doctohtml/1.doc","D:/doctohtml/1.html"); } catch (Exception e) { e.printStackTrace(); } } public static void writeFile(String content, String path) { FileOutputStream fos = null; BufferedWriter bw = null; try { File file = new File(path); fos = new FileOutputStream(file); bw = new BufferedWriter(new OutputStreamWriter(fos,"utf-8")); bw.write(content); } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { try { if (bw != null) bw.close(); if (fos != null) fos.close(); } catch (IOException ie) { } } } public static void convert2Html(String fileName, String outPutFile) throws TransformerException, IOException, ParserConfigurationException { HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName));//WordToHtmlUtils.loadDoc(new FileInputStream(inputFile)); WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter( DocumentBuilderFactory.newInstance().newDocumentBuilder() .newDocument()); wordToHtmlConverter.setPicturesManager( new PicturesManager() { public String savePicture( byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches ) { //html 中 图片标签中 显示的图片路路径 return "d:/doctohtml/"+suggestedName; } } ); wordToHtmlConverter.processDocument(wordDocument); //save pictures List pics=wordDocument.getPicturesTable().getAllPictures(); if(pics!=null){ for(int i=0;i Picture pic = (Picture)pics.get(i); System.out.println(); try { //word中图片的存储路径 pic.writeImageContent(new FileOutputStream("D:/doctohtml/" + pic.suggestFullFileName())); } catch (FileNotFoundException e) { e.printStackTrace(); } } } Document htmlDocument = wordToHtmlConverter.getDocument(); ByteArrayOutputStream out = new ByteArrayOutputStream(); DOMSource domSource = new DOMSource(htmlDocument); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.METHOD, "html"); serializer.transform(domSource, streamResult); out.close(); writeFile(new String(out.toByteArray()), outPutFile); } }

WORD文字教程

一、 基础知识1、概述:WORD 是一个文字处理软件,主要用于文字的编辑与排版,是OFFICE办公软件包中的其中一个最适用的办公软件之一。

2、动与退出:启动:双击桌面上的WORD 图标(或点击“开始”菜单——指向“程序”——点击“WORD ”图标)退出:点击“文件”菜单点击“退出”(或单点击WORD窗口标题栏最右端的“关闭”按钮)3、 界面(即窗口)组成:(1)标题栏:图标,名称,最小化,最大化/还原,关闭按钮(2)菜单栏:WORD的所有操作都可用菜单命令实现(3)工具栏:包括常用工具栏和格式工具栏(默认为放在菜单栏的下面)A工具移动:把鼠标放在工具栏的最左边会出现移动符号,按住鼠标左键移动到所需位置再松开鼠标即可。

B工具变形:当工具栏放在文本区内时,把鼠标放在工具栏的最边界处会出现调整符号,按住鼠标左键顺着箭头的方向移动即可。

C显示/隐藏工具栏:点击“视图”菜单——点击“工具栏”——选择所需的工具栏。

(4)文本区:(又称为编辑区或工作窗口)用来输入文本——编辑文档及对象(5)标 尺:包括水平标尽和垂直标尺(点击“视图”——“标尺”可显示/隐藏标尺)(6)滚动条:包括水平滚动条和垂直滚动条,用来查看未显示的文本或对象(7)状态栏:显示文本的一些相关信息二、基本操作1、文本的输入:shift+ctrl:各种输入法的转换;ctrl+空格键:中文与英文输入法的转换;(1)英文:注意大小写字母转换键Caps Lock(2)汉字:在任务栏中转换中文输入法后输入(3)数字:利用数字键盘(先使Num Lock键灯亮)(4)符号:A键盘上的符号按shift+符号B键盘上没有的符号:点击“插入”菜单—点击“符号”—选择所需的符号—点击“插入”——点击“关闭”即可。

C在“输入法状态条”的软键盘上右单击弹出的菜单中选择所需的软键盘,单击所需的符号即可。

2、文本的选择:(1)连续多个字符:按左键从要选择的第一个字符拖到最后一个字符;(2)一行:光标移到待选一行的左边空白处(即是页边距的外边),当鼠标指针变为指向右方箭头时单击;(3)一段落:光标移到待选段落的左边空白处,当鼠标指针变为指向右方箭头时按住鼠标左键拖动即可;(4)整篇文章:点击“编缉”菜单——击“全选”(也可按Ctrl+A快捷键)3、文本的移动、复制与删除: (1)删除文本:选择文本后按键盘上的Delete键(2)移动文本:选择文本——点击“编辑”菜单——点击“剪贴”(可直接点击工具栏中的“剪贴”按钮)——光标放目标位置——点击“编辑”菜单——点击“粘贴” (可直接点击工具栏中的“粘贴”按钮)。

(也可先选择文本后再按住左键拖放到目标位置)(3)复制文本:选择文本——点击“编辑”菜单——点击“复制” (也可直接点击工具栏中的“复制”按钮)——光标放目标位置——点击“编辑” ——点击“粘贴”(也可直接点击工具栏中的“粘贴”按钮)。

(也可选择文本后按住Ctrl键再按住左键拖放到目标位置)(4)撤消与恢复操作:A撤消:作用是撤消上一步操作。

点击常用工具栏上的“撤消”按钮。

B恢复:作用是恢复被撤消的操作。

点击常用工具栏上的“恢复“按钮。

三、文件的操作⑴新建文档:单击常用工具栏上的新建按钮⑵打开文档:点击“文件”菜单——点击“打开”——选择文件——点击“打开” ⑶保存文档:点击“文件”菜单——点击“保存”——在文件名上输入要保存的文件名称(可选择保存位置)——点击“保存”(第一次保存文件还可以用密码保存文件,以防其他人修改或查看文件的内容)⑷关闭文档:点击“文件”菜单——点击“关闭”(或者按标题栏右侧的“关闭”按钮,如出现“是”“否”“取消”三个按钮,“是”是要用户保存该文档后再退出,“否”则是对该文档进行不保存而退出,“取消”则是不退出回到编辑状态。

)⑸另存为:用不同的文件名、位置或文件格式保存活动文件,还可以使用此命令对有密码的文件进行修改密码。

⑹文档切换:单击任务栏上的文档名称按钮四、页面设置方法 :单击“文件”菜单——点击“页面设置”出现页面设置对话框1、“页边距”标签可以设置上、下、左、右页边距;(也可以在页面中采用水平与垂直标尺大概的设置)2、“纸型”标签可以选择纸张大小和方向,设置好后单点击“确定”便可。

五、打印预览(查看文档的最后编辑效果)方法:单击文件菜单——点击打印预览(或单击常用工具栏的打印预览按钮)。

在打印预览中我们可以看到光标变成了放大镜,在文档中移动鼠标单击可以查看的上下左右,预览完后单击关闭按钮关闭预览窗口,回到页面视图中。

六、打印单击文件菜单——点击打印(出现对话框)——设置好后点击“确定”。

WORD第二课 文档格式化一、显示比例:用来调整文档的显示比例单击“常用”工具栏上的“显示比例”按钮右边的下拉箭头,再选择所需的显示比例。

二、字符格式 ★:Word可以先采用默认的格式进行文字的录入,然后对默认的字符格式(先选择)再进行更改至所需的格式;也可以先进行设置好所需的格式后,再进行字符的录入。

1、利用...

利用poi操作word文档时,出现多行多列 如何处理?求大神

mport org.apache.poi.POITextExtractor;import org.apache.poi.hwpf.extractor.WordExtractor;org.apache.poi.hwpf.extractor.WordExtractor doc = new WordExtractor(new FileInputStream(filePath));String text = doc.getText();

转载请注明出处51数据库 » poi读取按段落word内容

软件
前端设计
程序设计
Java相关