資源簡介
完美市縣了DOCX格式的模板文字替換與插入浮于文字上方的圖片。

代碼片段和文件信息
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?parm?=?new?HashMap();
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?destPathMap?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?itPara?=?document.getParagraphsIterator();
????????????while?(itPara.hasNext())?{
????????????????XWPFParagraph?paragraph?=?(XWPFParagraph)?itPara.next();
????????????????//String?s?=?paragraph.getParagraphText();????????
????????????????Set?set?=?map.keySet();
????????????????Iterator?iterator?=?set.iterator();
????????????????while?(iterator.hasNext())?{
????????????????????String?key?=?iterator.next();
????????????????????List?run=paragraph.getRuns();
?????????????????????for(int?i=0;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(k
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????8458??2017-11-22?11:46??CustomXWPFDocument.java
?????文件???????8549??2017-11-23?09:31??CreateFile.java
-----------?---------??----------?-----??----
????????????????17007????????????????????2
評論
共有 條評論