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

poi word图片 java poi图片写入word

时间:2020-07-06 09:49:11
poi word 图片 设置位置如何使用JAVA、POI读写word文档??能不能将一个word的内容完全读过来,放到一个新生成的word文件中去,要求能将word中的表格、图片等保留,格式不变。最好
作者:

poi word图片

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

Apache的POI,可以利用POI(java)生成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 }

如何使用POI操作Word文本框中的内容

public class CreateWordDemo { public void createDocContext(String file) throws DocumentException,IOException { // 设置纸张大小 Document document = new Document(PageSize.A4); // 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中 RtfWriter2.getInstance(document, new FileOutputStream(file)); document.open(); // 设置中文字体 BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); // 标题字体风格 Font titleFont = new Font(bfChinese, 12,Font.BOLD); // 正文字体风格 Font contextFont = new Font(bfChinese, 10,Font.NORMAL); Paragraph title = new Paragraph("标题"); // 设置标题格式对齐方式 title.setAlignment(Element.ALIGN_CENTER); title.setFont(titleFont); document.add(title); String contextString ="iText是一个能够快速产生PDF文件的java类库。

" + " \n"// 换行 +"iText的java类对于那些要产生包含文本," + "表格,图形的只读文档是很有用的。

它的类库尤其与java Servlet有很好的给合。

" +"使用iText与PDF能够使你正确的控制Servlet的输出。

"; Paragraph context = new Paragraph(contextString); // 正文格式左对齐 context.setAlignment(Element.ALIGN_LEFT); context.setFont(contextFont); // 离上一段落(标题)空的行数 context.setSpacingBefore(5); // 设置第一行空的列数 context.setFirstLineIndent(20); document.add(context); // 利用类FontFactory结合Font和Color可以设置各种各样字体样式 Paragraph underline = new Paragraph("下划线的实现",FontFactory.getFont( FontFactory.HELVETICA_BOLDOBLIQUE, 18,Font.UNDERLINE, new Color(0, 0,255))); document.add(underline); // 设置 Table 表格 Table aTable = new Table(3); int width[] = { 25, 25, 50 }; aTable.setWidths(width);// 设置每列所占比例 aTable.setWidth(90); // 占页面宽度90% aTable.setAlignment(Element.ALIGN_CENTER);// 居中显示 aTable.setAlignment(Element.ALIGN_MIDDLE);// 纵向居中显示 aTable.setAutoFillEmptyCells(true); // 自动填满 aTable.setBorderWidth(1); // 边框宽度 aTable.setBorderColor(new Color(0, 125, 255)); // 边框颜色 aTable.setPadding(2);// 衬距,看效果就知道什么意思了 aTable.setSpacing(3);// 即单元格之间的间距 aTable.setBorder(2);// 边框 // 设置表头 Cell haderCell = new Cell("表格表头"); haderCell.setHeader(true); haderCell.setColspan(3); aTable.addCell(haderCell); aTable.endHeaders(); Font fontChinese = new Font(bfChinese, 12, Font.NORMAL,Color.GREEN); Cell cell = new Cell(new Phrase("这是一个测试的 3*3 Table 数据",fontChinese)); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setBorderColor(new Color(255, 0,0)); cell.setRowspan(2); aTable.addCell(cell); aTable.addCell(new Cell("#1")); aTable.addCell(new Cell("#2")); aTable.addCell(new Cell("#3")); aTable.addCell(new Cell("#4")); Cell cell3 = new Cell(new Phrase("一行三列数据",fontChinese)); cell3.setColspan(3); cell3.setVerticalAlignment(Element.ALIGN_CENTER); aTable.addCell(cell3); document.add(aTable); document.add(new Paragraph("\n")); // 添加图片 Image.getInstance即可以放路径又可以放二进制字节流 Image img = Image.getInstance("d:\\img01800.jpg"); img.setAbsolutePosition(0,0); img.setAlignment(Image.RIGHT);// 设置图片显示位置 img.scaleAbsolute(60, 60);// 直接设定显示尺寸 // img.scalePercent(50);//表示显示的大小为原尺寸的50% // img.scalePercent(25,12);//图像高宽的显示比例 // img.setRotation(30);//图像旋转一定角度 document.add(img); document.close(); } public static void main(String[] args) { CreateWordDemo word = new CreateWordDemo(); String file ="d:/demo1.doc"; try { word.createDocContext(file); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

java中如何用jacob将数据库中的二进制图片存到word中的指定位置

主要用到org.apache.poi 来操作word,而读取数据库图片 通过读取数据库Blob 字段的列然后通过 public static BufferedImage imgChangeBuffer(Blob blob) 方法钩子一个 BufferedImage 然后把这个 BufferedImage 设置到word中,希望能帮上忙。

poi的word转html,如何显示修订内容的最终状态

实现代码如下: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); } }

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