java POI往word文档中指定位置插入表格
2018-06-18 03:30:57来源:未知 阅读 ()
1.Service demo
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.math.BigInteger; import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Map;
import org.apache.poi.POIXMLDocument; 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.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import org.apache.xmlbeans.XmlCursor; 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.CTBookmark; import org.docx4j.wml.Document; import org.docx4j.wml.Drawing; import org.docx4j.wml.ObjectFactory; import org.docx4j.wml.P; import org.docx4j.wml.R; 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.data.general.DefaultPieDataset; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr; import org.springframework.stereotype.Service; import com.google.common.collect.Lists; import com.google.common.collect.Maps;
public class ExportBgServiceImpl { private static final String bookmark = "tpBookmark";// 报告图片位置的书签名 public void exportBg(OutputStream out) { String srcPath = "D:/tp/fx.docx"; String targetPath = "D:/tp/fx2.docx"; String key = "$key";// 在文档中需要替换插入表格的位置 XWPFDocument doc = null; File targetFile = null; try { doc = new XWPFDocument(POIXMLDocument.openPackage(srcPath)); List<XWPFParagraph> paragraphList = doc.getParagraphs(); if (paragraphList != null && paragraphList.size() > 0) { for (XWPFParagraph paragraph : paragraphList) { List<XWPFRun> runs = paragraph.getRuns(); for (int i = 0; i < runs.size(); i++) { String text = runs.get(i).getText(0).trim(); if (text != null) { if (text.indexOf(key) >= 0) { runs.get(i).setText(text.replace(key, ""), 0); XmlCursor cursor = paragraph.getCTP().newCursor(); // 在指定游标位置插入表格 XWPFTable table = doc.insertNewTbl(cursor); CTTblPr tablePr = table.getCTTbl().getTblPr(); CTTblWidth width = tablePr.addNewTblW(); width.setW(BigInteger.valueOf(8500)); this.inserInfo(table); break; } } } } } doc.write(out); out.flush(); out.close(); } catch (Exception e) { throw new SysException(ERRORConstants.COMMON_SYSTEM_ERROR, e); } } /** * 把信息插入表格 * @param table * @param data */ private void inserInfo(XWPFTable table) { List<DTO> data = mapper.getInfo();//需要插入的数据 XWPFTableRow row = table.getRow(0); XWPFTableCell cell = null; for (int col = 1; col < 6; col++) {//默认会创建一列,即从第2列开始 // 第一行创建了多少列,后续增加的行自动增加列 CTTcPr cPr =row.createCell().getCTTc().addNewTcPr(); CTTblWidth width = cPr.addNewTcW(); if(col==1||col==2||col==4){ width.setW(BigInteger.valueOf(2000)); } } row.getCell(0).setText("指标"); row.getCell(1).setText("指标说明"); row.getCell(2).setText("公式"); row.getCell(3).setText("参考值"); row.getCell(4).setText("说明"); row.getCell(5).setText("计算值"); for(DTO item : data){ row = table.createRow(); row.getCell(0).setText(item.getZbmc()); cell = row.getCell(1); cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000)); cell.setText(item.getZbsm()); cell = row.getCell(2); cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000)); cell.setText(item.getJsgs()); if(item.getCkz()!=null&&!item.getCkz().contains("$")){ row.getCell(3).setText(item.getCkz()); } cell = row.getCell(4); cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000)); cell.setText(item.getSm()); row.getCell(5).setText(item.getJsjg()==null?"无法计算":item.getJsjg()); } } }
2.Controller
public void exportBg(HttpServletResponse response) { try { response.setContentType("application/octet-stream"); String name = "报告"; response.addHeader("Content-Disposition", "attachment;filename="+new String(name.getBytes(),"iso-8859-1") +".docx"); OutputStream out = response.getOutputStream(); export.exportBg(out,sblsh); } catch (Exception e) { } }
3.maven
<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>
在文档中指定位置 设置好 关键字如 demo 中的 “$key”
结果图如下:
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- Java--反射(框架设计的灵魂)案例 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash