栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

xwpftemplate的时间设置

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

xwpftemplate的时间设置

importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.util.HashMap;importjava.util.Iterator;importjava.util.List;importjava.util.Map;importjava.util.Map.Entry;importjava.util.regex.Matcher;importjava.util.regex.Pattern;importorg.apache.poi.POIXMLProperties.CoreProperties;importorg.apache.poi.hwpf.HWPFDocument;importorg.apache.poi.hwpf.usermodel.Range;importorg.apache.poi.xwpf.extractor.XWPFWordExtractor;importorg.apache.poi.xwpf.usermodel.XWPFDocument;importorg.apache.poi.xwpf.usermodel.XWPFParagraph;importorg.apache.poi.xwpf.usermodel.XWPFRun;importorg.apache.poi.xwpf.usermodel.XWPFTable;importorg.apache.poi.xwpf.usermodel.XWPFTableCell;importorg.apache.poi.xwpf.usermodel.XWPFTableRow;importcom.deepoove.poi.XWPFTemplate;importcom.deepoove.poi.data.PictureRenderData;

public classWriteWordUtil {public void writeDocx(String path, Map map) throwsException {

InputStream is= newFileInputStream(path);

XWPFDocument doc= newXWPFDocument(is);//XWPFWordExtractor extractor = new XWPFWordExtractor(doc) ;//String text = extractor.getText();//System.out.println(text);//CoreProperties coreProps = extractor.getCoreProperties();//this.printCoreProperties(coreProps);//this.close(is);

}

private void replaceInPara(XWPFDocument doc, Mapparams) {

Iterator iterator =doc.getParagraphsIterator();

XWPFParagraph para;while(iterator.hasNext()) {

para=iterator.next();this.replaceInPara(para, params);

}

}

private void replaceInPara(XWPFParagraph para, Mapparams) {

Listruns;

Matcher matcher;if (this.matcher(para.getParagraphText()).find()) {

runs=para.getRuns();for (int i = 0; i < runs.size(); i++) {

XWPFRun run=runs.get(i);

String runText=run.toString();

matcher= this.matcher(runText);if(matcher.find()) {while ((matcher = this.matcher(runText)).find()) {

runText= matcher.replaceFirst(String.valueOf(params.get(matcher.group(1))));

}//直接调用XWPFRun的setText()方法设置文本时,在底层会重新创建一个XWPFRun,把文本附加在当前文本后面,//所以我们不能直接设值,需要先删除当前run,然后再自己手动插入一个新的run。

para.removeRun(i);if(runText.equals("null")){

runText="";

}

para.insertNewRun(i).setText(runText);

}

}

}

}

private void replaceInTable(XWPFDocument doc, Mapparams) {

Iterator iterator =doc.getTablesIterator();

XWPFTable table;

Listrows;

Listcells;

Listparas;while(iterator.hasNext()) {

table=iterator.next();

rows=table.getRows();for(XWPFTableRow row : rows) {

cells=row.getTableCells();for(XWPFTableCell cell : cells) {

String cellTextString=cell.getText();for (Entrye : params.entrySet()) {if (cellTextString.contains("${"+e.getKey()+"}"))

cellTextString= cellTextString.replace("${"+e.getKey()+"}", e.getValue().toString());

}

cell.removeParagraph(0);if(cellTextString.contains("${") && cellTextString.contains("}")){

cellTextString= "";

}

cell.setText(cellTextString);//paras = cell.getParagraphs();//for (XWPFParagraph para : paras) {//this.replaceInPara(para, params);//}

}

}

}

}

privateMatcher matcher(String str) {

Pattern pattern= Pattern.compile("\$\{(.+?)\}", Pattern.CASE_INSENSITIVE);

Matcher matcher=pattern.matcher(str);returnmatcher;

}

private voidclose(InputStream is) {if (is != null) {try{

is.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

private voidclose(OutputStream os) {if (os != null) {try{

os.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

private voidprintCoreProperties(CoreProperties coreProps) {

System.out.println(coreProps.getCategory());//分类

System.out.println(coreProps.getCreator()); //创建者

System.out.println(coreProps.getCreated()); //创建时间

System.out.println(coreProps.getTitle()); //标题

}

public static String templateWrite(String filePath, Map params,String outFilePath)throwsException{

InputStream is= newFileInputStream(filePath);

WriteWordUtil writeWordUtil= newWriteWordUtil();

XWPFDocument doc= newXWPFDocument(is);//替换段落里面的变量

writeWordUtil.replaceInPara(doc, params);//替换表格里面的变量

writeWordUtil.replaceInTable(doc, params);

OutputStream os= newFileOutputStream(outFilePath);

doc.write(os);

writeWordUtil.close(os);

writeWordUtil.close(is);

os.flush();

os.close();return "";

}

public static String templateWrite2(String filePath, Map params,String outFilePath)throwsException{

XWPFTemplate template=XWPFTemplate.compile(filePath).render(params);

FileOutputStream out= newFileOutputStream(outFilePath);

template.write(out);

out.flush();

out.close();

template.close();return "";

}public static void main(String[] args) throwsException {

Map params = new HashMap();

params.put("JSDWMC", "项目1rna");//

params.put("XMMC", "项目2rnb");//

params.put("1", "1");// params.put("2", "2");// params.put("object1", "o1");//

params.put("object2", "o2");//                params.put("localPicture", new PictureRenderData(120, 120, "D:\A.png"));

templateWrite("D:\template\1.docx", params, "D:\template\5.docx");

templateWrite2("D:\2.docx", params, "D:\template\7.docx");

}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985205.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号