java向Word模板中替换书签数据,插入图片,插入复选框,插入Word中表格的行数据,删除表格行数据
Posted 小蜗牛爱远行
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java向Word模板中替换书签数据,插入图片,插入复选框,插入Word中表格的行数据,删除表格行数据相关的知识,希望对你有一定的参考价值。
-
java向Word模板中替换书签数据,插入图片,插入复选框,插入Word中表格的行数据,删除表格行数据
-
使用插件:spire.doc
-
创建工具类,上代码:
import com.spire.doc.Document; import com.spire.doc.Table; import com.spire.doc.TableCell; import com.spire.doc.TableRow; import com.spire.doc.documents.*; import com.spire.doc.fields.DocPicture; import com.spire.doc.fields.TextRange; import org.apache.commons.collections4.map.LinkedMap; import org.springframework.stereotype.Component; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 向插入Word数据工具类 * @author yyds */ @Component public class WordInsertDataUtil /** * 根据书签向Word插入数据; type=1插入文本,type=2插入图片, type=3插入复选框。。。。 * @param bookmarks * @param bookMarkDatas */ public void insertDataByBookmarks(Document doc, BookmarksNavigator bookmarks, List<Map<String, String>> bookMarkDatas) for (int i = 0; bookMarkDatas.size() > 0 && i < bookMarkDatas.size(); i++) Map<String, String> bookMarkData = bookMarkDatas.get(i); if ("1".equals(bookMarkData.get("type"))) bookmarks.moveToBookmark(bookMarkData.get("bookMark")); bookmarks.replaceBookmarkContent(bookMarkData.get("value"), true); else if ("2".equals(bookMarkData.get("type"))) bookmarks.moveToBookmark(bookMarkData.get("bookMark")); Paragraph para = new Paragraph(doc); //图片位置 DocPicture picture = para.appendPicture(bookMarkData.get("picPath")); //图片高 picture.setHeight(Float.parseFloat(bookMarkData.get("heigth"))); //图片宽 picture.setWidth(Float.parseFloat(bookMarkData.get("width"))); picture.setTextWrappingStyle(TextWrappingStyle.Through); bookmarks.insertParagraph(para); else if ("3".equals(bookMarkData.get("type"))) bookmarks.moveToBookmark(bookMarkData.get("bookMark")); bookmarks.replaceBookmarkContent(bookMarkData.get("value"), true); TextSelection selection = doc.findString(bookMarkData.get("check"),true,true); TextRange tr1 = selection.getAsOneRange(); tr1.getCharacterFormat().setFontName("Wingdings"); //除了16进制,也可以用10进制来表示这个符号,复选框打勾是82 doc.replace(selection.getSelectedText(), "\\uF0FE", true, true); else if("4".equals(bookMarkData.get("type"))) bookmarks.moveToBookmark(bookMarkData.get("bookMark")); bookmarks.replaceBookmarkContent(bookMarkData.get("value"), true); TextSelection selection = doc.findString(bookMarkData.get("uncheck"),true,true); TextRange tr1 = selection.getAsOneRange(); tr1.getCharacterFormat().setFontName("Wingdings"); //除了16进制,也可以用10进制来表示这个符号,复选框打勾是82 doc.replace(selection.getSelectedText(), "\\uF0A8", true, true); /** * 向Word中的表格插入行 * @param table * @param dataList * @param startRow */ public void insertTable(Table table, List<String[]> dataList, int startRow) for (int i = startRow; i < startRow + dataList.size(); i++) TableRow row = table.getRows().get(i - 1).deepClone(); table.getRows().insert(i, row); TableRow jtcyRow = table.getRows().get(i); for (int j = 0; j < dataList.get(i - startRow).length; j++) TableCell cell = jtcyRow.getCells().get(j); cell.getChildObjects().clear(); TextRange range = cell.addParagraph().appendText(dataList.get(i - startRow)[j]); range.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center); range.getCharacterFormat().setFontName("宋体"); range.getCharacterFormat().setFontSize(11); jtcyRow.getRowFormat().setHorizontalAlignment(RowAlignment.Center); jtcyRow.getCells().get(j).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle); /** * 删除行 * @param table 表格 * @param startRow 起始行 * @param removeNum 删除几行 */ public void removeTableRows(Table table, int startRow, int removeNum) for (int i = 0; i < removeNum; i++) TableRow jtcyRow = table.getRows().get(startRow - 1); table.getRows().remove(jtcyRow); public Map<String, String> generalCheckBox(LinkedMap<String, String> dictMap, String bookMark, String checkValue) //todo 多选未开发 List<String> valueList = Arrays.asList(checkValue.split(",")); StringBuilder sbd = new StringBuilder(); int startNum = 1; String checkSymbol = ""; for(Map.Entry<String, String> entry : dictMap.entrySet()) String symbol = "symbol" + startNum; sbd.append(symbol); if (valueList.contains(entry.getValue())) checkSymbol = symbol; startNum++; Map<String, String> bookMarkMap = new HashMap<>(); bookMarkMap.put("bookMark", bookMark); bookMarkMap.put("value", sbd.toString()); bookMarkMap.put("type", "3"); bookMarkMap.put("check", checkSymbol); return bookMarkMap;
-
使用方法(仅供参考,自行调试):
-
根据书签插入数据
//打开Word模板 Document doc = new Document(new FileInputStream(templatePath)); //获取所有书签 BookmarksNavigator bookmarks = new BookmarksNavigator(doc); //业务数据 List<Map<String, String>> bookMarkDatas = new ArrayList<>(); //年-插入文本 Map<String, String> nian = new HashMap<>(); nian.put("bookMark", "NIAN"); nian.put("value", bdcdjPermit2.getNian()); nian.put("type", "1"); bookMarkDatas.add(nian); //图片-插入图片 String tpPath = objectMap.get("TP").toString(); Map<String, String> tp = new HashMap<>(); tp.put("bookMark", "TP"); tp.put("picPath", tpPath); tp.put("heigth", "18"); tp.put("width", "13"); tp.put("type", "2"); bookMarkDatas.add(tp); //向Word模板中插入数据 wordInsertDataUtil.insertDataByBookmarks(doc, bookmarks, bookMarkDatas);
-
Word中插入表格
//打开Word模板 Document doc = new Document(new FileInputStream(templatePath)); //获取所有书签 BookmarksNavigator bookmarks = new BookmarksNavigator(doc); //获取第一个section Section section = doc.getSections().get(0); //获取表格 Table table = section.getTables().get(0); //业务数据 JSONObject result; //直接替换书签数据 bookmarks.moveToBookmark("TEST"); if (Utils.notEmpty(result.get("test"))) bookmarks.replaceBookmarkContent(result.getString("test"), true); //插入表格行的起始位置 int startNum = 22; //插入的行数 int insertNum = 0; //放表格数据列表 List<String[]> tableDataList = new ArrayList<>(); String[] insertData = test, tset, etst, etts; tableDataList.add(insertData); insertNum++; //插入行数据 wordInsertDataUtil.insertTable(table, dyDataList, startNum); //删除行数据 wordInsertDataUtil.removeTableRows(table, startNum, 1);
-
以上是关于java向Word模板中替换书签数据,插入图片,插入复选框,插入Word中表格的行数据,删除表格行数据的主要内容,如果未能解决你的问题,请参考以下文章
跪求POI根据模板生成word文档的例子。 注意啦,是用书签标记的方式,替换书签中的内容。