本站收集了一篇相关的编程文章,网友张秀丽根据主题投稿了本篇教程内容,涉及到Java、word、文档、替换文字、插入图片、Java操作word文档及图片的方法相关内容,已被627网友关注,内容中涉及的知识点可以在下方直接下载获取。
Java操作word文档及图片的方法
Java 替换word文档文字并指定位置插入图片
先说下 需要的依赖包
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-excelant</artifactId> <version>3.12</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.12</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.8</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.8</version> </dependency> <!-- 生成图片--> <dependency> <groupId>org.jfree</groupId> <artifactId>jfreechart</artifactId> <version>1.0.19</version> </dependency> <dependency> <!--支持插入图片--> <groupId>org.docx4j</groupId> <artifactId>docx4j</artifactId> <version>3.3.1</version> </dependency>
示例,下图
如上图,需要替换的字符串地方“$1”为“1231”,在指定位置插入书签,并命名“test” ,插入的图片如下
本人也没太过多去研究,亲测通过有效,在这分享下
1.demo
import java.awt.Font; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.poi.POIXMLDocument; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.util.IOUtils; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.docx4j.TraversalUtil; import org.docx4j.dml.wordprocessingDrawing.Inline; import org.docx4j.finders.RangeFinder; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; import org.docx4j.wml.Body; import org.docx4j.wml.BooleanDefaultTrue; import org.docx4j.wml.CTBookmark; import org.docx4j.wml.Color; import org.docx4j.wml.Document; import org.docx4j.wml.Drawing; import org.docx4j.wml.HpsMeasure; import org.docx4j.wml.ObjectFactory; import org.docx4j.wml.P; import org.docx4j.wml.R; import org.docx4j.wml.RPr; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.labels.StandardPieSectionLabelGenerator; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.title.TextTitle; import org.jfree.data.general.DefaultPieDataset;import com.aisino.qysds.common.constant.ERRORConstants; import com.aisino.qysds.common.exception.SysException; import com.aisino.qysds.service.IExportBgService; import com.google.common.collect.Maps; public class ExportBgServiceImpl { public static void main(String[] args) throws Exception { Map<String, String> map = Maps.newHashMap(); map.put("$1", "1231"); XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage("D:\\tp\\test.docx")); Iterator<XWPFParagraph> itPara = document.getParagraphsIterator(); while (itPara.hasNext()) { XWPFParagraph paragraph = (XWPFParagraph) itPara.next(); List<XWPFRun> runs = paragraph.getRuns(); for (int i = 0; i < runs.size(); i++) { String oneparaString = runs.get(i).getText(runs.get(i).getTextPosition()); for (Map.Entry<String, String> entry : map.entrySet()) { if (oneparaString.equals(entry.getKey())) { oneparaString = oneparaString.replace(entry.getKey(), entry.getValue()); } } runs.get(i).setText(oneparaString, 0); } } FileOutputStream outStream = null; outStream = new FileOutputStream("D:\\tp\\test1.docx"); document.write(outStream); outStream.close(); //-----------------------------------这块为生成图片 和 插入图片 DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("修改类", 1); dataset.setValue("提示类", 1); dataset.setValue("校验不通过", 3); dataset.setValue("正常类", 3); JFreeChart chart = ChartFactory.createPieChart3D(null, dataset, true, false, false); chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15)); // 设置图例类别字体 // TextTitle title = new TextTitle(titleString); // title.setFont(new Font("黑体", Font.ITALIC, 20));//设置标题字体 // chart.setTitle(title); PiePlot piePlot = (PiePlot) chart.getPlot(); DecimalFormat df = new DecimalFormat("0.00%"); NumberFormat nf = NumberFormat.getInstance(); StandardPieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator("{0} {2}", nf, df);// 获得StandardPieSectionLabelGenerator对象,生成的格式, // {0}表示section名,{1}表示section的值,{2}表示百分比。可以自定义 piePlot.setLabelGenerator(generator);// 设置百分比 piePlot.setLabelFont(new Font("黑体", Font.ITALIC, 15));// 设置饼图中类别字体 piePlot.setNoDataMessage("此时并没有任何数据可用"); piePlot.setCircular(false); piePlot.setLabelGap(0.02D); piePlot.setIgnoreNullValues(true);// 设置不显示空位 piePlot.setIgnoreZeroValues(true);// 设置不显示负值或零值 String fName = "pie.png"; File file = new File("D:\\tp", fName); if (file.exists()) { file.delete(); } try { ChartUtilities.saveChartAsPNG(file, chart, 800, 500); File file2 = new File("D:\\tp\\test1.docx"); WordprocessingMLPackage wPackage = WordprocessingMLPackage.load(new FileInputStream(file2)); MainDocumentPart mainDocumentPart = wPackage.getMainDocumentPart(); Document wmlDoc = (Document) mainDocumentPart.getJaxbElement(); Body body = wmlDoc.getBody(); // 提取正文中所有段落 List<Object> paragraphs = body.getContent(); // 提取书签并创建书签的游标 RangeFinder rt = new RangeFinder("CTBookmark", "CTMarkupRange"); new TraversalUtil(paragraphs, rt); for (CTBookmark bm : rt.getStarts()) { if (bm.getName().equals("test")) {// 这里的test为 word文档中预设的 书签名 InputStream inputStream = new FileInputStream(file); byte[] bytes = IOUtils.toByteArray(inputStream); BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wPackage, bytes); Inline inline = imagePart.createImageInline(null, null, 0, 1, false, 10000);//这里的100000不是正常屏幕大小,用于设置插入图片的大小 P p = (P) (bm.getParent()); ObjectFactory factory = new ObjectFactory(); // R对象是匿名的复杂类型,然而我并不知道具体啥意思,估计这个要好好去看看ooxml才知道 R run = factory.createR(); // drawing理解为画布? Drawing drawing = factory.createDrawing(); drawing.getAnchorOrInline().add(inline); run.getContent().add(drawing); p.getContent().add(run); } } wPackage.save(new FileOutputStream(new File("D:\\tp\\test1.docx"))); } catch (IOException e) { } } }
最后效果图如下:
java简单操作word实例
本文为大家分享了java简单操作word例子,供大家参考,具体内容如下
package apache.poi; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.poifs.filesystem.DirectoryEntry; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class ExportDocTest { public static void main(String[] args) { String destFile="D:\\11.doc"; //#####################根据自定义内容导出Word文档################################################# StringBuffer fileCon=new StringBuffer(); fileCon.append(" 张大炮 男 317258963215223\n" + "2011 09 2013 07 3\n" + " 二炮研究 成人\n" + "2013000001 2013 07 08"); fileCon.append("\n\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); new ExportDocTest().exportDoc(destFile, fileCon.toString()); //##################根据Word模板导出单个Word文档################################################### Map<String, String> map=new HashMap<String, String>(); map.put("name", "Zues"); map.put("sex", "男"); map.put("idCard", "200010"); map.put("year1", "2000"); map.put("month1", "07"); map.put("year2", "2008"); map.put("month2", "07"); map.put("gap", "2"); map.put("zhuanye", "计算机科学与技术"); map.put("type", "研究生"); map.put("bianhao", "2011020301"); map.put("nowy", "2011"); map.put("nowm", "01"); map.put("nowd", "20220301"); //注意biyezheng_moban.doc文档位置,此例中为应用根目录 HWPFDocument document=new ExportDocTest().replaceDoc("biyezheng_moban.doc", map); ByteArrayOutputStream ostream = new ByteArrayOutputStream(); try { document.write(ostream); //输出word文件 OutputStream outs=new FileOutputStream(destFile); outs.write(ostream.toByteArray()); outs.close(); } catch (IOException e) { e.printStackTrace(); } } /** * * @param destFile * @param fileCon */ public void exportDoc(String destFile,String fileCon){ try { //doc content ByteArrayInputStream bais = new ByteArrayInputStream(fileCon.getBytes()); POIFSFileSystem fs = new POIFSFileSystem(); DirectoryEntry directory = fs.getRoot(); directory.createDocument("WordDocument", bais); FileOutputStream ostream = new FileOutputStream(destFile); fs.writeFilesystem(ostream); bais.close(); ostream.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 读取word模板并替换变量 * @param srcPath * @param map * @return */ public HWPFDocument replaceDoc(String srcPath, Map<String, String> map) { try { // 读取word模板 FileInputStream fis = new FileInputStream(new File(srcPath)); HWPFDocument doc = new HWPFDocument(fis); // 读取word文本内容 Range bodyRange = doc.getRange(); // 替换文本内容 for (Map.Entry<String, String> entry : map.entrySet()) { bodyRange.replaceText("${" + entry.getKey() + "}", entry .getValue()); } return doc; } catch (Exception e) { e.printStackTrace(); return null; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助。