package test;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
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;
public class CreateFile {
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception{
Map<String,String> parm = new HashMap<String, String>();
parm.put("${bjbh}", "_办件编号");
parm.put("${bjbh+}", "行政许可申请材料接收(受理)通知书、行政许可决定书_J14001201401010001");
parm.put("${applyDepartmentPerson}", "_办理人");
parm.put("${receive_year}", "——年");
parm.put("${receive_month}", "——月");
parm.put("${receive_day}", "——日");
parm.put("${dealName}", "——名称");
searchAndReplace ("C:/Users/Administrator/Desktop/送达回证_系统生成.docx","C:/Users/Administrator/Desktop/送达回证_系统生成_g1.docx",parm);
}
@SuppressWarnings("resource")
public static void searchAndReplace(String srcPath, String destPath,Map<String, String> map) {
try {
File fxx = new File(srcPath);
InputStream isxxx = new FileInputStream(fxx);
byte[] xxxx = new byte[isxxx.available()] ;
isxxx.read(xxxx);
//InputStream docis = new FileInputStream(srcPath);
CustomXWPFDocument document = new CustomXWPFDocument(new ByteArrayInputStream(xxxx));
//document.setParagraph(paragraph, pos);
// 替换段落中的指定文字
Iterator<XWPFParagraph> itPara = document.getParagraphsIterator();
while (itPara.hasNext()) {
XWPFParagraph paragraph = (XWPFParagraph) itPara.next();
//String s = paragraph.getParagraphText();
Set<String> set = map.keySet();
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
List<XWPFRun> run=paragraph.getRuns();
for(int i=0;i<run.size();i++)
{
if(run.get(i).getText(run.get(i).getTextPosition())!=null && run.get(i).getText(run.get(i).getTextPosition()).trim().equals(key))
{
/**参数0表示生成的文字是要从哪一个地方开始放置,设置文字从位置0开始
* 就可以把原来的文字全部替换掉了
* */
run.get(i).setText(map.get(key),0);
}
}
}
}
XmlToken xmlToken2 = null;
String prp = ""
+ "<w:r xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
+ "<w:rPr>"
+ "<w:rFonts w:hint=\"eastAsia\" w:ascii=\"仿宋_GB2312\" w:hAnsi=\"宋体\" w:eastAsia=\"仿宋_GB2312\"/>"
+ "<w:sz w:val=\"32\"/>"
+ "</w:rPr>"
+ "<w:t>_spl_</w:t>"
+ "</w:r>"
;
// 替换表格中的指定文字=
Iterator<XWPFTable> itTable = document.getTablesIterator();
while (itTable.hasNext()) {
XWPFTable table = (XWPFTable) itTable.next();
int rcount = table.getNumberOfRows();
for (int i = 0; i < rcount; i++) {
XWPFTableRow row = table.getRow(i);
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
for (Entry<String, String> e : map.entrySet()) {
if (cell.getText()!=null && (cell.getText().trim().equals(e.getKey()) || cell.getText().indexOf(e.getKey())>-1)) {
System.out.println(cell.getText());
String text = new String(cell.getText());
text = text.substring(0, text.indexOf(e.getKey()));
String value = text+e.getValue();
int x = cell.getParagraphs().size();
List<XWPFParagraph> paras = cell.getParagraphs();
for (int y=0;y<x;y++){
cell.removeParagraph(0);
}
if (paras!=null && paras.size()>0){
paras.get(0).removeRun(0);
}
if (e.getKey().equals("${bjbh+}")){
String bjbhPlus = e.getValue();
String[] splBjbh = bjbhPlus.split("_");
String prprpl = prp.replace("_spl_", splBjbh[0]);
String prprp2 = prp.replace("_spl_", splBjbh[1]);
try {
xmlToken2 = XmlToken.Factory.parse(prprpl);
XWPFParagraph par1 = cell.addParagraph();
par1.getCTP().set(xmlToken2);
xmlToken2 = XmlToken.Factory.parse(prprp2);
XWPFParagraph par2 = cell.addParagraph();
par2.getCTP().set(xmlToken2);
} catch (XmlException xe) {
xe.printStackTrace();
}
}else{
String prprpl = prp.replace("_spl_", e.getValue());
try {
xmlToken2 = XmlToken.Factory.parse(prprpl);
} catch (XmlException xe) {
xe.printStackTrace();
}
XWPFParagraph par1 = cell.addParagraph();
par1.getCTP().set(xmlToken2);
}
}
}
}
}
}
InputStream is = new FileInputStream("C:/Users/Administrator/Desktop/img1.png");
String blipId = document.addPictureData(is, CustomXWPFDocument.PICTURE_TYPE_PNG);
XWPFParagraph imgP = document.createPicture2(blipId,document.getNextPicNameNumber(CustomXWPFDocument.PICTURE_TYPE_PNG), 3600000, -6600000);
FileOutputStream outStream = null;
outStream = new FileOutputStream(destPath);
document.write(outStream);
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void rwdoc (){
try{
File file = new File("C:/Users/Administrator/Desktop/aa.doc");
File file1 = new File("C:/Users/Administrator/Desktop/aa_11.doc");
InputStream istr = new FileInputSt
POI DOCX 完美文本、表格模板文字替换并实现在指定位置插入图片浮于文字上方
4星 · 超过85%的资源 需积分: 50 9 浏览量
2017-11-23
09:43:02
上传
评论 8
收藏 5KB RAR 举报
aa80303857
- 粉丝: 43
- 资源: 21
最新资源
- 基于SSM的电影购票系统 框架:Spring+SpringMVC+MyBatis+JSP
- UE5插件 后台WebApi的Restful接口请求交互
- 易语言简单绘制Demo
- 数值积分-复化梯形求积公式 - 北太天元
- DUmeter-8050exe
- 毕业设计:Java项目之jsp医院药品采购管理系统(源码 + 数据库 + 说明文档)
- JavaScript DOM视频教程包含源码作业及项目实战
- 简历-求职简历-word-文件-简历模版免费分享-应届生-高颜值简历模版-个人简历模版-简约大气-大学生在校生-求职-实习
- UMG图表控件+UE5插件+曲线图、饼图、环状图、柱状图
- 简历-求职简历-word-文件-简历模版免费分享-应届生-高颜值简历模版-个人简历模版-简约大气-大学生在校生-求职-实习
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
前往页