




已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
package mon.poi;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Map.Entry;import org.apache.poi.POIXMLDocument;import org.apache.poi.openxml4j.exceptions.InvalidFormatException;import org.apache.poi.xwpf.usermodel.ParagraphAlignment;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.XmlException;import org.apache.xmlbeans.XmlToken;import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;public class PoiWordTemplate private XWPFDocument document; public XWPFDocument getDocument() return document;public void setDocument(String templatePath) try this.document = new XWPFDocument( POIXMLDocument.openPackage(templatePath); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();/this.document = document;/* * 往模板中插入图片 * param document * param mapImage * param width * param height */ public void replaceTextToImage(Map mapImage,int width,int height) List listParagraphs = this.document.getParagraphs(); for(int i=0;ilistParagraphs.size();i+) for (Entry entry : mapImage.entrySet() if (listParagraphs.get(i).getText().trim().indexOf(entry.getKey() != -1) CTInline inline = listParagraphs.get(i).createRun().getCTR().addNewDrawing().addNewInline(); try insertPicture(entry.getValue(),inline,width,height); catch (InvalidFormatException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (FileNotFoundException e) / TODO Auto-generated catch blocke.printStackTrace(); /* * 替换word中的文字 * param document * param mapValue * param newText */ public void replaceTextToText(Map mapValue) List listRun; List listParagraphs = this.document.getParagraphs(); for(int i=0;ilistParagraphs.size();i+) listRun = listParagraphs.get(i).getRuns(); for(int j=0;jlistRun.size();j+) if(mapValue.get(listRun.get(j).getText(0) != null | null.equals(mapValue.get(listRun.get(j).getText(0) listRun.get(j).setText(mapValue.get(listRun.get(j).getText(0),0); /* * 往表格中插入数据 * param document * param list * param position */ public void setTablesValue(ListMap list,int position,int columnNum) Iterator it = this.document.getTablesIterator(); int count = 0; Map columnMap = new HashMap(); while (it.hasNext() XWPFTable table = (XWPFTable) it.next(); if(count = position) XWPFTableRow firstRow = table.getRow(1); List rowCell = firstRow.getTableCells(); for(int i=0;irowCell.size();i+) columnMap.put(i+, rowCell.get(i).getText().trim(); table.removeRow(1);/删除第二行 for(int i=0;ilist.size();i+) XWPFTableRow row = table.createRow();/创建新的一行时创建了第一个cell row.setHeight(30); /* for(int j=0;j=columnNum-2;j+) row.addNewTableCell(); */ int rcount = table.getNumberOfRows(); for (int i = 1; i rcount; i+) XWPFTableRow newRow = table.getRow(i); List cells = newRow.getTableCells(); for(int j=0;jcells.size();j+) cells.get(j).setText(list.get(i-1).get(columnMap.get(j+)+);/标题栏不用改变单元格的值 count+; /* * 设置用电建议书里的基本信息 第一个表格 */ public void setOfferBorkBasicInfo(Map map,ListMap list,int position) Iterator it = this.document.getTablesIterator(); int count = 0; Map columnMap = new HashMap(); while (it.hasNext() XWPFTable table = (XWPFTable) it.next(); if(count = position) int rcount = table.getNumberOfRows(); for (int i = 0; i rcount; i+) if(i=4) XWPFTableRow newRow = table.getRow(i); List cells = newRow.getTableCells(); for(int j=0;jcells.size();j+) if(!null.equals(map.get(cells.get(j).getText().trim()+) String temp = cells.get(j).getText().trim()+; cells.get(j).removeParagraph(0); cells.get(j).setText(map.get(temp)+); XWPFTableRow sixRow = table.getRow(6); List rowCell = sixRow.getTableCells(); for(int i=0;irowCell.size();i+) columnMap.put(i+, rowCell.get(i).getText().trim(); for(int i=0;ilist.size();i+) XWPFTableRow row = table.createRow();/创建新的一行时创建了第一个cell row.setHeight(35); for(int j=0;j=6;j+) row.addNewTableCell(); table.removeRow(6);/删除模板中的字段行 int rowCount = table.getNumberOfRows(); int k= 0; for (int i = 6; i rowCount; i+) XWPFTableRow tempRow = table.getRow(i); List cells = tempRow.getTableCells(); for(int j=0;jcells.size();j+) / cells.get(j).removeParagraph(0); cells.get(j).setText(list.get(k).get(columnMap.get(j+)+); k+; count+; /* * 生成用电分析结果与优化用电建议 * param list * param position */ public void setAnanlyResultTbl(List list,int position) Iterator it = this.document.getTablesIterator(); int count = 0; while (it.hasNext() XWPFTable table = (XWPFTable) it.next(); if(count = position) XWPFTableRow tempRow = table.getRow(1); List cellList = tempRow.getTableCells(); XWPFTableCell cell = cellList.get(0); List listGraph = cell.getParagraphs(); for(int i=0;ilist.size();i+) listGraph.get(0).setAlignment(ParagraphAlignment.LEFT); XWPFRun r2 = listGraph.get(0).createRun(); / cell.setText(list.get(i); / cell. r2.setText(list.get(i); r2.setFontSize(12); r2.addBreak(); count+; /* * 生成预期节能收益 */ public void setExpectedBenefits(List reslut, Map saveMoney,int position) Iterator it = this.document.getTablesIterator(); int count = 0; while (it.hasNext() XWPFTable table = (XWPFTable) it.next(); if(count = position) XWPFTableRow tempRow = table.getRow(1); List cellList = tempRow.getTableCells(); XWPFTableCell cell = cellList.get(0); List listGraph = cell.getParagraphs(); for(int i=0;ireslut.size();i+) listGraph.get(0).setAlignment(ParagraphAlignment.LEFT); XWPFRun r2 = listGraph.get(0).createRun(); r2.setText(reslut.get(i); r2.setFontSize(12); r2.addBreak(); /* cell.setText(reslut.get(i); r2.addBreak();*/ XWPFTableRow fourRow = table.getRow(3); List cells = fourRow.getTableCells(); for(int j=0;jcells.size();j+) if(!null.equals(saveMoney.get(cells.get(j).getText().trim()+) String temp = cells.get(j).getText().trim()+; cells.get(j).removeParagraph(0); cells.get(j).setText(saveMoney.get(temp)+); count+; /* public void setAnanlyResultTbl(Map map,int position) Iterator it = this.document.getTablesIterator(); int count = 0; while (it.hasNext() XWPFTable table = (XWPFTable) it.next(); if(count = position) int rowCount = table.getNumberOfRows(); for (int i = 0; i rowCount; i+) XWPFTableRow tempRow = table.getRow(i); List cells = tempRow.getTableCells(); for(int j=0;jcells.size();j+) List listParagraphs = cells.get(j).getParagraphs(); for(int m=0;mlistParagraphs.size();m+) List listRun = listParagraphs.get(m).getRuns(); for(int n=0;nlistRun.size();n+) if(map.get(listRun.get(n).getText(0) !=null | !null.equals(map.get(listRun.get(n).getText(0) listRun.get(n).setText(map.get(listRun.get(n).getText(0),0); listRun.get(n).setBold(true); for (Entry entry : map.entrySet() if(listRun.get(n).getText(0).equals(entry.getKey() count+; */ public void insertPicture(String filePath,CTInline inline,int width, int height) throws InvalidFormatException, FileNotFoundException try String ind = this.document.addPictureData(new FileInputStream(filePath), 5); catch (InvalidFormatException e) System.out.println(exception); int id = this.document.getAllPictures().size()-1; final int EMU = 9525; width *= EMU; height *= EMU; String blipId = this.document.getAllPictures().get(id).getPackageRelationship() .getId(); String picXml = + + + + + + + + + + + + + + + + + + + + + + + + + ; inline.addNewGraphic().addNewGraphicData(); XmlToken xmlToken = null; try xmlToken = XmlToken.Factory.parse(picXml); catch (XmlException xe) xe.printStackTrace(); inline.set(xmlToken); inline.setDistT(0); inline.setDistB(0); inline.setDistL(0); inline.setDistR(0); CTPositiveSize2D extent = inline.addNewExtent(); extent.setCx(width); extent.setCy(height); CTNonVisualDrawingProps docPr = inline.addNewDocPr(); docPr.setId(id); docPr.setName(IMG_ + id); docPr.setDescr(IMG_ + id); /* public static void replaceTextToImage(String templatePath,String pastePath,Map mapImage,int width,int height) XWPFDocument document;try document = new XWPFDocument( POIXMLDocument.openPackage(templatePath); replaceTextToImage(document,mapImage,400,300); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); */ public void wirteNewWord(String pastePath) File newFile = new File(pastePath);FileOutputStream fos = null;try fos = new FileOutputStream(newFile); catch (FileNotFoundException e) / TODO Auto-generated catch blocke.printStackTrace();try this.document.write(fos);fos.flush();fos.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); public static void main(String args) throws
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 江苏省常熟市重点名校2024-2025学年初三一诊模拟数学试题含解析
- 忻州市保德县2025届三年级数学第二学期期末调研试题含解析
- 西安财经大学《中国古代文学作品选与中学语文(三)》2023-2024学年第一学期期末试卷
- 湖南医药学院《检测仪表与过程控制》2023-2024学年第二学期期末试卷
- 万科物业保洁培训计划
- 湛江市高三测试题(一)文综地理试卷
- 化学产品开发流程的8个关键步骤
- 化学实验室原始记录规范
- 2025版权转让协议合同范本
- 双肾囊肿护理查房
- GB/T 6451-2023油浸式电力变压器技术参数和要求
- 幼儿园中班绘本《城市里最漂亮的巨人》课件
- 医院廉洁行医廉政教育专题课件
- 医务人员职业健康安全健康-课件
- 安全组织机构图
- 旧石器时代考古-基础知识课件
- 江苏省建设工程现场安全文明施工措施费计价管理办法
- 病区药品规范化管理与问题对策黄池桃
- 螺纹塞规操作规程
- 2023年北京天文馆招聘笔试备考题库及答案解析
- 应急救援队伍单兵体能训练项目要求
评论
0/150
提交评论