已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026广东中山大学肿瘤防治中心中心泌尿外科尧凯教授课题组自聘技术员招聘1人笔试模拟试题及答案解析
- 2025广东江门市江海区江南街道办事处专职消防员招聘6人备考题库附答案
- 2025年玉树州公安局面向社会公开招聘警务辅助人员(第二批)考试备考题库附答案
- 2025年安徽省能源集团有限公司校园招聘60人笔试备考试题附答案
- 2025年滁州理想建设投资发展有限公司公开招聘2名参考题库附答案
- 2025年上海工程技术大学公开招聘(第四批)(公共基础知识)测试题附答案
- 2025年黑龙江省铁路集团有限责任公司面向社会公开招聘1人备考题库附答案
- 2025山西忻州市人民医院和忻州市中医医院市招县用招聘工作人员10人备考题库附答案
- 2025年广西民族印刷包装集团有限公司招聘14人笔试备考试题附答案
- 2026中国人民银行清算总中心直属企业中志支付清算服务(北京)有限公司招聘2人笔试参考题库及答案解析
- 2026年共青团中央所属单位招聘66人备考题库及答案详解一套
- 人民警察法培训课件
- 2026年哈尔滨职业技术学院单招职业适应性考试题库参考答案详解
- 2025云南昆明巫家坝建设发展有限责任公司及下属公司第四季度社会招聘31人历年真题汇编带答案解析
- 输尿管切开取石课件
- 小猫绝育协议书
- 66kV及以下架空电力线路设计标准
- 人工搬运培训课件
- 2025年浙江乍浦经济开发区(嘉兴港区)区属国有公司公开招聘28人笔试考试备考试题及答案解析
- 胃肠外科危重患者监护与护理
- 2025年榆林神木市信息产业发展集团招聘备考题库(35人)及答案详解(新)
评论
0/150
提交评论