java poi 填充单元格_Java POI Word模板数据填充

该博客介绍了一个使用Java POI库来填充Word模板的方法,包括替换文本、图片以及表格中的内容。示例代码展示了如何读取模板文件,替换特定占位符,并保存生成的新文件。
摘要由CSDN通过智能技术生成

1 package***;2

3 importjava.io.FileInputStream;4 importjava.io.FileNotFoundException;5 importjava.io.FileOutputStream;6 importjava.util.ArrayList;7 importjava.util.HashMap;8 importjava.util.List;9 importjava.util.Map;10 importjava.util.Set;11

12 importorg.apache.poi.util.Units;13 importorg.apache.poi.xwpf.usermodel.XWPFDocument;14 importorg.apache.poi.xwpf.usermodel.XWPFParagraph;15 importorg.apache.poi.xwpf.usermodel.XWPFRun;16 importorg.apache.poi.xwpf.usermodel.XWPFTable;17 importorg.apache.poi.xwpf.usermodel.XWPFTableCell;18 importorg.apache.poi.xwpf.usermodel.XWPFTableRow;19

20 /**

21 * 文件数据替换22 *@author23 *24 */

25

26 public classReplaceWord {27

28 public static String path = "模板文件的路径/test.docx";29

30 public static void main(String[] args) throwsException {31 Map data = new HashMap<>();32 Map pic = new HashMap<>();33

34 pic.put("${carpicture}", "Files/sign.png");35 pic.put("${licensecopy}", "Files/sign.png");36 data.put("${seal}", "1");37 pic.put("${sing01}", "Files/sign.png");38 pic.put("${sing02}", "Files/sign.png");39 System.out.println(pic.containsKey("${carpicture}"));40

41 //列表(List)是对象的有序集合

42 List> tabledataList = new ArrayList<>();43 getWord(data, tabledataList, pic);44 }45

46 public static void getWord(Map data, List> tabledataList, Mappicmap)47 throwsException {48 try (FileInputStream is = new FileInputStream(path); XWPFDocument document = newXWPFDocument(is)) {49 //替换掉表格之外的文本(仅限文本)

50 changeText(document, data);51

52 //替换表格内的文本对象

53 changeTableText(document, data);54

55 //替换图片

56 changePic(document, picmap);57

58 //替换表格内的图片对象

59 changeTablePic(document, picmap);60

61 long time = System.currentTimeMillis();//获取系统时间

62 System.out.println(time); //打印时间63 //使用try和catch关键字捕获异常

64 try (FileOutputStream out = new FileOutputStream("生成的文件路径/文件名" + ".docx")) {65 document.write(out);66 }67 } catch(FileNotFoundException e) {68 e.printStackTrace();69 }70 }71

72 /**

73 * 替换段落文本74 *@paramdocument docx解析对象75 *@paramtextMap 需要替换的信息集合76 *77 */

78 public static void changeText(XWPFDocument document, MaptextMap) {79 //获取段落集合80 //返回包含页眉或页脚文本的段落

81 List paragraphs =document.getParagraphs();82 //增强型for循环语句,前面一个为声明语句,后一个为表达式

83 for(XWPFParagraph paragraph : paragraphs) {84 //判断此段落是否需要替换

85 String text = paragraph.getText();//检索文档中的所有文本

86 if(checkText(text)) {87 List runs =paragraph.getRuns();88 for(XWPFRun run : runs) {89 //替换模板原来位置

90 Object ob =changeValue(run.toString(), textMap);91 if (ob instanceofString) {92 if(textMap.containsKey(run.toString())) {93 run.setText((String) ob, 0);94 }95 }96 }97 }98 }99 }100

101 /*检查文本中是否包含指定的字符(此处为“$”),并返回值*/

102 public static booleancheckText(String text) {103 boolean check = false;104 if (text.contains("$")) {105 check = true;106 }107 returncheck;108 }109

110 /**

111 * 替换图片112 *@paramdocument113 *@paramtextMap114 *@throwsException115 */

116

117 public static void changePic(XWPFDocument document, Map textMap) throwsException {118 //获取段落集合

119 List paragraphs =document.getParagraphs();120

121 for(XWPFParagraph paragraph : paragraphs) {122 //判断此段落是否需要替换

123 String text =paragraph.getText();124 if(checkText(text)) {125 List runs =paragraph.getRuns();126 for(XWPFRun run : runs) {127 //替换模板原来位置

128 Object ob =changeValue(run.toString(), textMap);129 if (ob instanceofString) {130 if(textMap.containsKey(run.toString())) {131 run.setText("", 0);132 try (FileInputStream is = newFileInputStream((String) ob)) {133 run.addPicture(is, XWPFDocument.PICTURE_TYPE_PNG, (String) ob, Units.toEMU(100),134 Units.toEMU(100));135 }136 }137 }138 }139 }140 }141 }142

143 public static void changeTableText(XWPFDocument document, Mapdata) {144 //获取文件的表格

145 List tableList =document.getTables();146

147 //循环所有需要进行替换的文本,进行替换

148 for (int i = 0; i < tableList.size(); i++) {149 XWPFTable table =tableList.get(i);150 if(checkText(table.getText())) {151 List rows =table.getRows();152 //遍历表格,并替换模板

153 eachTable(document, rows, data);154 }155 }156 }157

158 public static void changeTablePic(XWPFDocument document, Map pic) throwsException {159 List tableList =document.getTables();160

161 //循环所有需要替换的文本,进行替换

162 for (int i = 0; i < tableList.size(); i++) {163 XWPFTable table =tableList.get(i);164 if(checkText(table.getText())) {165 List rows =table.getRows();166 System.out.println("简单表格替换:" +rows);167 //遍历表格,并替换模板

168 eachTablePic(document, rows, pic);169 }170 }171 }172

173 public static void eachTablePic(XWPFDocument document, List rows, Mappic)174 throwsException {175 for(XWPFTableRow row : rows) {176 List cells =row.getTableCells();177 for(XWPFTableCell cell : cells) {178 //判断单元格是否需要替换

179 if(checkText(cell.getText())) {180 List paragraphs =cell.getParagraphs();181 for(XWPFParagraph paragraph : paragraphs) {182 List runs =paragraph.getRuns();183 for(XWPFRun run : runs) {184 Object ob =changeValue(run.toString(), pic);185 if (ob instanceofString) {186 System.out.println("run" + "‘" + run.toString() + "‘");187 if(pic.containsKey(run.toString())) {188 System.out.println("run" + run.toString() + "替换为" +ob);189 run.setText("", 0);190 try (FileInputStream is = newFileInputStream((String) ob)) {191 run.addPicture(is, XWPFDocument.PICTURE_TYPE_PNG, (String) ob, Units.toEMU(100),192 Units.toEMU(100));193 }194 } else{195 System.out.println("‘" + run.toString() + "‘ 不匹配");196 }197 }198 }199 }200 }201 }202 }203 }204

205 public static Object changeValue(String value, MaptextMap) {206 Set> textSets =textMap.entrySet();207 Object valu = "";208 for (Map.EntrytextSet : textSets) {209 //匹配模板与替换值 格式${key}

210 String key =textSet.getKey();211 if(value.contains(key)) {212 valu =textSet.getValue();213 }214 }215 returnvalu;216 }217

218 public static void eachTable(XWPFDocument document, List rows, MaptextMap) {219 for(XWPFTableRow row : rows) {220 List cells =row.getTableCells();221 for(XWPFTableCell cell : cells) {222 //判断单元格是否需要替换

223 if(checkText(cell.getText())) {224 //System.out.println("cell:" + cell.getText());

225 List paragraphs =cell.getParagraphs();226 for(XWPFParagraph paragraph : paragraphs) {227 List runs =paragraph.getRuns();228 for(XWPFRun run : runs) {229

230 Object ob =changeValue(run.toString(), textMap);231 if (ob instanceofString) {232

233 System.out.println("run:" + "‘" + run.toString() + "‘");234 if(textMap.containsKey(run.toString())) {235 System.out.println("run:" + run.toString() + "替换为" +ob);236 run.setText((String) ob, 0);237 } else{238 System.out.println("‘" + run.toString() + "‘不匹配");239 }240 }241 }242 }243 }244 }245 }246 }247 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值