diff --git a/pom.xml b/pom.xml index 939338ce5fbab13c1d0996dc9d5dd8aeae9fd1fd..5b603d6d55d31cbaacd47c2d8d529f8fe9b4a584 100644 --- a/pom.xml +++ b/pom.xml @@ -81,6 +81,8 @@ com.aspose.words aspose-words 19.5 + ${project.basedir}/lib/aspose-words-19.5.jar + system diff --git a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/utils/WordRunUtils.java b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/utils/WordRunUtils.java index 82d4b7662fae5adf3cf6a196b5c14c60a20f18ae..ebff749b0650a2b684ede74e9e7c9f541038ab9d 100644 --- a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/utils/WordRunUtils.java +++ b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/utils/WordRunUtils.java @@ -9,7 +9,6 @@ import org.apache.poi.xwpf.usermodel.*; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; /** * @program: data_centre_java @@ -103,8 +102,12 @@ public class WordRunUtils { */ public void addParagraphInDocFooter(XWPFParagraph templateParagraph, Text text) { + String newLine = templateParagraph.getText(); + if ("\n".equals(newLine)) { + return; + } - //创建段落对象 + //创建段落对象 XWPFParagraph createParagraph = document.createParagraph(); // 设置段落样式 createParagraph.getCTP().setPPr(templateParagraph.getCTP().getPPr()); diff --git a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/utils/WordTableUtils.java b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/utils/WordTableUtils.java index 5b646e200b1d24e73db2a5671cef0e81bf6ecafd..a93e11e7182d1ae0eb0f38e103a0839b479594fa 100644 --- a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/utils/WordTableUtils.java +++ b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/utils/WordTableUtils.java @@ -7,6 +7,7 @@ import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.tableBean. import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.tableBean.OneTable; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge; import java.util.*; @@ -34,25 +35,26 @@ public class WordTableUtils { * @param oneTable 一个表格 * @param flag (0为静态表格,1为表格整体循环,2为表格内部行循环,3为表格不循环仅简单替换标签即可) */ - public void addTableInDocFooter(XWPFTable templateTable, - OneTable oneTable, int flag) { + public void addTableInDocFooter(XWPFTable templateTable, OneTable oneTable, int flag, boolean isBr) { if (flag == 1) {// 表格整体循环 for (OneRow row : oneTable.getRows()) { List templateTableRows = templateTable.getRows();// 获取模板表格所有行 - XWPFTable newCreateTable = createTable();// 创建新表格,默认一行一列 + XWPFTable newCreateTable = createTable(templateTable);// 创建新表格,默认一行一列 for (int i = 1; i < templateTableRows.size(); i++) { XWPFTableRow newCreateRow = newCreateTable.createRow(); //创建一行 CopyTableRow(newCreateRow, templateTableRows.get(i));// 复制模板行文本和样式到新行 包含列 } newCreateTable.removeRow(0);// 移除多出来的第一行 - document.createParagraph();// 添加回车换行 + if (isBr) { + document.createParagraph();// 添加回车换行 + } replaceTable(newCreateTable, row.getCells());//替换标签 } } else if (flag == 2) { // 表格表格内部行循环 - XWPFTable newCreateTable = createTable();// 创建新表格, 默认一行一列 + XWPFTable newCreateTable = createTable(templateTable);// 创建新表格, 默认一行一列 List TempTableRows = templateTable.getRows();// 获取模板表格所有行 int tagRowsIndex = 0;// 标签行indexs @@ -114,19 +116,23 @@ public class WordTableUtils { replaceTableRow(newCreateRow, oneTable.getTableEndRow().getCells()); // 处理不循环标签的替换 表格尾部的行 如果有信息可以替换 } newCreateTable.removeRow(0);// 移除多出来的第一行 - document.createParagraph();// 添加回车换行 + if (isBr) { + document.createParagraph();// 添加回车换行 + } } else if (flag == 3) { //表格不循环 仅简单替换标签 List templateTableRows = templateTable.getRows();// 获取模板表格所有行 - XWPFTable newCreateTable = createTable();// 创建新表格,默认一行一列 + XWPFTable newCreateTable = createTable(templateTable);// 创建新表格,默认一行一列 for (int i = 1; i < templateTableRows.size(); i++) { //从1 开始 第一行是标志 -->不要 XWPFTableRow newCreateRow = newCreateTable.createRow(); CopyTableRow(newCreateRow, templateTableRows.get(i));// 复制模板行文本和样式到新行 } newCreateTable.removeRow(0);// 移除多出来的第一行 - document.createParagraph();// 添加回车换行 + if (isBr) { + document.createParagraph();// 添加回车换行 + } List cells = new ArrayList<>(); if (oneTable.getTableTitleRow().getCells() != null) { @@ -139,13 +145,15 @@ public class WordTableUtils { } else if (flag == 0) { List templateTableRows = templateTable.getRows();// 获取模板表格所有行 - XWPFTable newCreateTable = createTable();// 创建新表格,默认一行一列 + XWPFTable newCreateTable = createTable(templateTable);// 创建新表格,默认一行一列 for (int i = 1; i < templateTableRows.size(); i++) { //从1 开始 第一行是标志 -->不要 XWPFTableRow newCreateRow = newCreateTable.createRow(); CopyTableRow(newCreateRow, templateTableRows.get(i));// 复制模板行文本和样式到新行 } newCreateTable.removeRow(0);// 移除多出来的第一行 - document.createParagraph();// 添加回车换行 + if (isBr) { + document.createParagraph();// 添加回车换行 + } } } @@ -157,7 +165,7 @@ public class WordTableUtils { * @param target 待修改格式的XWPFTableRow * @param source 模板XWPFTableRow */ - private void CopyTableRow(XWPFTableRow target, XWPFTableRow source) { + public void CopyTableRow(XWPFTableRow target, XWPFTableRow source) { int tempRowCellsize = source.getTableCells().size();// 模板行的列数 for (int i = 0; i < tempRowCellsize - 1; i++) { @@ -292,13 +300,25 @@ public class WordTableUtils { /** * 创建表格 */ - public XWPFTable createTable() { - XWPFTable table = document.createTable(); - // 校验一下grid是否为空,如果为空就创建。转pdf的时候如果为空会报空指针 - CTTblGrid grid = table.getCTTbl().getTblGrid(); - if (grid == null) { - table.getCTTbl().addNewTblGrid(); - } - return table; + public XWPFTable createTable(XWPFTable templateTbl) { + // XWPFTable table = document.createTable(); + // // 校验一下grid是否为空,如果为空就创建。转pdf的时候如果为空会报空指针 + // CTTblGrid grid = table.getCTTbl().getTblGrid(); + // if (grid == null) { + // table.getCTTbl().addNewTblGrid(); + // } + // return table; + XWPFTable table = document.createTable(); + // 复制模板表格的属性 + CTTblPr tblPr = templateTbl.getCTTbl().getTblPr(); + if (tblPr != null) { + table.getCTTbl().setTblPr((CTTblPr) tblPr.copy()); + } + // 校验一下grid是否为空,如果为空就创建。转pdf的时候如果为空会报空指针 + CTTblGrid grid = table.getCTTbl().getTblGrid(); + if (grid == null) { + table.getCTTbl().addNewTblGrid(); + } + return table; } } diff --git a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/utils/WordTemplateUtils.java b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/utils/WordTemplateUtils.java index a5061b789c482614760528efd0b9fdfc5e73c465..c6bd61f70fb79d13d24b77a22c2a3c8dce2789cc 100644 --- a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/utils/WordTemplateUtils.java +++ b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/utils/WordTemplateUtils.java @@ -3,11 +3,15 @@ package com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.utils; import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.WordInfo; import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.tableBean.OneTable; import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.tableBean.TableBeans; +import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.textBean.OneFor; import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.textBean.Text; +import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.textBean.TextFroReaders; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; -import org.apache.poi.wp.usermodel.Paragraph; import org.apache.poi.xwpf.usermodel.*; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrType; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; @@ -15,7 +19,6 @@ import java.io.*; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; /** * @program: data_centre_java @@ -155,18 +158,21 @@ public class WordTemplateUtils { switch (oneTable.getTableType()) { case for_table: //log.info("循环生成表格"); - wordTableUtils.addTableInDocFooter(table, tableBeans.getOneTable(templateTableName), 1); + wordTableUtils.addTableInDocFooter(table, tableBeans.getOneTable(templateTableName), 1, + oneTable.isBr()); break; case for_Row: //log.info("循环生成表格内部的行"); - wordTableUtils.addTableInDocFooter(table, tableBeans.getOneTable(templateTableName), 2); + wordTableUtils.addTableInDocFooter(table, tableBeans.getOneTable(templateTableName), 2, + oneTable.isBr()); break; case replace_value: //log.info("简单替换标签"); - wordTableUtils.addTableInDocFooter(table, tableBeans.getOneTable(templateTableName), 3); + wordTableUtils.addTableInDocFooter(table, tableBeans.getOneTable(templateTableName), 3, + oneTable.isBr()); break; case No_todo: - wordTableUtils.addTableInDocFooter(table, null, 0); + wordTableUtils.addTableInDocFooter(table, null, 0, oneTable.isBr()); break; default: log.info("表格处理方法错误"); @@ -180,11 +186,24 @@ public class WordTemplateUtils { } else if (BodyElementType.PARAGRAPH.equals(body.getElementType())) { // 处理段落 //log.info("获取到段落"); //获取处理的段落信息 - XWPFParagraph ph = body.getBody().getParagraphArray(curP); + IBody body1 = body.getBody(); + XWPFParagraph ph = body1.getParagraphArray(curP); if (ph != null) { //自己封装 段落循环 List runs = ph.getRuns(); + for (XWPFRun run : runs) { + CTR ctr = run.getCTR(); + if (ctr.sizeOfBrArray() > 0) { + for (CTBr br : ctr.getBrArray()) { + if (br.getType() == STBrType.PAGE) { + //创建段落对象 + XWPFParagraph createParagraph = document.createParagraph(); + createParagraph.createRun().addBreak(BreakType.PAGE); + } + } + } + } StringBuffer value = new StringBuffer(); for (int i1 = 0; i1 < runs.size(); i1++) { String wordText = runs.get(i1).getText(0); @@ -225,12 +244,30 @@ public class WordTemplateUtils { String forEachKey = valuetemp.substring(valuetemp.indexOf(WordTemplateKeyEnum.textForKeyPrefixValue.getKeyCode()), valuetemp.indexOf(WordTemplateKeyEnum.keyEnd.getKeyCode())).replace(WordTemplateKeyEnum.textForKeyPrefixValue.getKeyCode(), ""); - oneForTextList = wordInfo.getTextFroReaders().getOneForTextList(forEachKey).getOneForTextList(); - - if (oneForTextList == null) { - throw new RuntimeException("缺少元数据" + valuetemp); - } - } else {//都不是就是渲染的数据 + // oneForTextList = wordInfo.getTextFroReaders().getOneForTextList(forEachKey).getOneForTextList(); + // + // if (oneForTextList == null) { + // throw new RuntimeException("缺少元数据" + valuetemp); + // } + + TextFroReaders textFroReaders = wordInfo.getTextFroReaders(); + if (textFroReaders == null) { + log.error("未创建" + WordTemplateKeyEnum.textForStart.getKeyCode()); + oneForTextList = new ArrayList<>(); + i++; + continue; + } + OneFor oneFor = textFroReaders.getOneForTextList(forEachKey); + if (oneFor == null) { + log.error("未设置" + WordTemplateKeyEnum.textForStart.getKeyCode()); + oneForTextList = new ArrayList<>(); + i++; + continue; + } + oneForTextList = oneFor.getOneForTextList(); + + + } else {//都不是就是渲染的数据 addList.add(phtemp); } } else { diff --git a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/utils/operationUtils/RunReplaceUtils.java b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/utils/operationUtils/RunReplaceUtils.java index 0f55636b608bb890f469ab8ec871f1a3d4585f37..bf9aa1a454119663f3b3f36f5e852625dc2b61a9 100644 --- a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/utils/operationUtils/RunReplaceUtils.java +++ b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/utils/operationUtils/RunReplaceUtils.java @@ -2,6 +2,7 @@ package com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.utils.operationUti import com.xiaominge.utils.IdUtils; import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.utils.WordTemplateKeyEnum; +import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.FontBean; import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.ParameterBean; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; @@ -15,6 +16,7 @@ import org.apache.xmlbeans.XmlObject; import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject; import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; import org.springframework.beans.BeanUtils; @@ -381,11 +383,12 @@ public class RunReplaceUtils { } else { // todo 根据自己项目自己定义 如果模版里面设置了变了 导出的数据中没有 可以抛出异常 也可以默认替换为空格 if (!key.startsWith(WordTemplateKeyEnum.imageKeyPerfix.getKeyCode())) { - throw new RuntimeException("数据源中没有发现key=[" + key + "],的数据"); //抛出异常 + // throw new RuntimeException("数据源中没有发现key=[" + key + "],的数据"); //抛出异常 } - // returnValue = ""; -// return new ParameterBean(key, ""); //默认替换为空格 + log.error("数据源中没有发现key=[" + key + "],的数据"); + + return new ParameterBean(key, ""); //默认替换为空格 } } @@ -423,6 +426,10 @@ public class RunReplaceUtils { insertNewRun.setText(parameterBean.getValue().toString()); return 0; } + FontBean fontBean = parameterBean.getFontBean(); + if (fontBean != null) { + setXWPFRunStyle(insertNewRun, fontBean); + } //下换线寻找切割 AtomicBoolean isSet = new AtomicBoolean(false); List parameterBeans_underLine = parseValue(parameterBean, WordValueEnum.UnderLineStart, WordValueEnum.UnderLineEnd, (old, New) -> { @@ -609,4 +616,14 @@ public class RunReplaceUtils { return anchor; } + private static void setXWPFRunStyle(XWPFRun xwpfRun, FontBean fontBean) { + xwpfRun.setFontSize(fontBean.getFontSize()); + CTRPr rpr = xwpfRun.getCTR().isSetRPr() ? xwpfRun.getCTR().getRPr() : xwpfRun.getCTR().addNewRPr(); + CTFonts fonts = + rpr.sizeOfRFontsArray() != 0 ? rpr.getRFontsArray(rpr.getRFontsArray().length - 1) : rpr.addNewRFonts(); + fonts.setAscii(fontBean.getFontName()); + fonts.setEastAsia(fontBean.getFontName()); + fonts.setHAnsi(fontBean.getFontName()); + } + } diff --git a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/ParameterBean.java b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/ParameterBean.java index ad2ae39ee295ad67ce08500da00d55b0a36b6904..d1d4f1d9004ecb3183800474a70a09d1435ccf33 100644 --- a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/ParameterBean.java +++ b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/ParameterBean.java @@ -91,6 +91,11 @@ public class ParameterBean { */ private boolean nextIsNewPage; + /** + * 字体样式 + */ + private FontBean fontBean; + public ParameterBean(String key, Object value) { this.key = key; this.value = value; @@ -102,6 +107,12 @@ public class ParameterBean { this.nextIsNewPage = nextIsNewPage; } + public ParameterBean(String key, Object value, FontBean fontBean) { + this.key = key; + this.value = value; + this.fontBean = fontBean; + } + /** * @param key 图片的key diff --git a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/tableBean/OneCell.java b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/tableBean/OneCell.java index 656cccda9d0523039fd40ffe3e7696bb7182ed77..8e1854b9857384f740f2db82b4e8c514d7533b3f 100644 --- a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/tableBean/OneCell.java +++ b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/tableBean/OneCell.java @@ -1,5 +1,6 @@ package com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.tableBean; +import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.FontBean; import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.ParameterBean; import lombok.Getter; import lombok.Setter; @@ -31,6 +32,10 @@ public class OneCell extends ParameterBean { super(key, imageType, imageInputStream, width, height, leftOffset, topOffset, false); } + public OneCell(String key, Object value, FontBean fontBean) { + super(key, value, fontBean); + } + /** * 合并行 和列 diff --git a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/tableBean/OneRow.java b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/tableBean/OneRow.java index b8a7c31cf6f573c2e2f733a7fc42ad65c3453528..efd3e4e47c37ece9e764813afb62b49cae94b160 100644 --- a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/tableBean/OneRow.java +++ b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/tableBean/OneRow.java @@ -1,5 +1,7 @@ package com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.tableBean; +import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.FontBean; + import java.io.InputStream; import java.util.ArrayList; import java.util.List; @@ -48,6 +50,12 @@ public class OneRow { return cell; } + public OneCell createCell(String cellKey, Object cellValue, FontBean fontBean) { + OneCell cell = new OneCell(cellKey, cellValue, fontBean); + this.cells.add(cell); + return cell; + } + public OneCell createOneCell(String key, String imageType, InputStream imageInputStream, int width, int height) { OneCell cell = new OneCell(key, imageType, imageInputStream, width, height); this.cells.add(cell); diff --git a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/tableBean/OneTable.java b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/tableBean/OneTable.java index 2a98ae2fdfc7df31fa778483c210f970788bc0db..fef349124c1325b115637e51d625f080f8e444fd 100644 --- a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/tableBean/OneTable.java +++ b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/tableBean/OneTable.java @@ -26,8 +26,11 @@ public class OneTable { //表格尾行 private final OneRow tableEndRow; - //构造 - public OneTable(String tableKey, TableType tableType) { + //表格后是否自动回车 + private boolean isBr = true; + + //构造 + public OneTable(String tableKey, TableType tableType) { this.tableType = tableType; this.tableKey = tableKey; this.rows = new ArrayList<>(); @@ -35,6 +38,23 @@ public class OneTable { this.tableEndRow = new OneRow(); } + public OneTable(String tableKey, TableType tableType, boolean isBr) { + this.tableType = tableType; + this.tableKey = tableKey; + this.rows = new ArrayList<>(); + this.tableTitleRow = new OneRow(); + this.tableEndRow = new OneRow(); + this.isBr = isBr; + } + + public boolean isBr() { + return isBr; + } + + public void setBr(boolean br) { + isBr = br; + } + //清空方法 public void clear() { diff --git a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/tableBean/TableBeans.java b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/tableBean/TableBeans.java index 04c1bb371e7ffdac49329597b2649ffc83bb2449..adae6ebf0680209c7b159def95002b2a12eede9c 100644 --- a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/tableBean/TableBeans.java +++ b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/tableBean/TableBeans.java @@ -29,18 +29,30 @@ public class TableBeans { * @param tableType 表格的替换值类型 * @return 获取一个表格对象 */ - public OneTable CreateOneTable(String oneTableKey, TableType tableType) { + public OneTable CreateOneTable(String oneTableKey, TableType tableType, boolean isBr) { if (StringUtils.isBlank(oneTableKey)) { throw new NullPointerException("表格key 不能为空"); } if (tables.containsKey(oneTableKey)) { throw ParameterRuntimeException.throwException("表格的key 已经存在"); } - OneTable oneTable = new OneTable(oneTableKey, tableType); + OneTable oneTable = new OneTable(oneTableKey, tableType, isBr); this.tables.put(oneTableKey, oneTable); return oneTable; } + public OneTable CreateOneTable(String oneTableKey, TableType tableType) { + if (StringUtils.isBlank(oneTableKey)) { + throw new NullPointerException("表格key 不能为空"); + } + if (tables.containsKey(oneTableKey)) { + throw ParameterRuntimeException.throwException("表格的key 已经存在"); + } + OneTable oneTable = new OneTable(oneTableKey, tableType); + this.tables.put(oneTableKey, oneTable); + return oneTable; + } + //放入一个表格 public void addOneTable(OneTable oneTable) { if (StringUtils.isBlank(oneTable.getTableKey())) { diff --git a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/textBean/Text.java b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/textBean/Text.java index a2031b2fb1b56b085c21820be92c83188f21e126..51c9b01a43e037e01807835b30733a807ab305ad 100644 --- a/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/textBean/Text.java +++ b/src/main/java/com/xiaominge/utils/wordUtils/wordOutputUtils/wordOut/wordBean/textBean/Text.java @@ -1,5 +1,6 @@ package com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.textBean; +import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.FontBean; import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.ParameterBean; import java.io.InputStream; @@ -17,6 +18,9 @@ public class Text { private List texts; + private FontBean fontBean; + + //构造 public Text() { this.texts = new ArrayList<>(); @@ -70,6 +74,11 @@ public class Text { return this; } + public Text putValue(String key, Object value, FontBean fontBean) { + this.texts.add(new ParameterBean(key, value, fontBean)); + return this; + } + public Text putImage(String key, String imageType, InputStream imageInputStream, int width, int height) { this.texts.add(new ParameterBean(key, imageType, imageInputStream, width, height)); diff --git "a/src/main/resources/wordOutput/\350\256\260\345\275\225\350\241\250-\346\265\201\345\212\250\347\233\221\350\200\203\345\221\230\343\200\201\347\275\221\347\273\234\347\233\221\350\200\203\345\221\230\350\256\260\345\275\225\350\241\250.docx" "b/src/main/resources/wordOutput/\350\256\260\345\275\225\350\241\250-\346\265\201\345\212\250\347\233\221\350\200\203\345\221\230\343\200\201\347\275\221\347\273\234\347\233\221\350\200\203\345\221\230\350\256\260\345\275\225\350\241\250.docx" new file mode 100644 index 0000000000000000000000000000000000000000..f9898de12b560e8eff9016eed94091c90d4d9621 Binary files /dev/null and "b/src/main/resources/wordOutput/\350\256\260\345\275\225\350\241\250-\346\265\201\345\212\250\347\233\221\350\200\203\345\221\230\343\200\201\347\275\221\347\273\234\347\233\221\350\200\203\345\221\230\350\256\260\345\275\225\350\241\250.docx" differ diff --git a/src/test/java/com/test/ExcelToHTMLTest.java b/src/test/java/com/test/ExcelToHTMLTest.java index 473b39c6f4ca21e4a997452bfe406f289a2d6634..d8450e50dfd606010884e2a96c6bfb8765a6e441 100644 --- a/src/test/java/com/test/ExcelToHTMLTest.java +++ b/src/test/java/com/test/ExcelToHTMLTest.java @@ -1,20 +1,17 @@ package com.test; -import com.xiaominge.utils.excleUtils.ExcelToHtml.ExcelToHtmlUtil; import org.junit.jupiter.api.Test; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; public class ExcelToHTMLTest { @Test public void run() throws IOException { - FileInputStream fileInputStream = new FileInputStream("D:\\桌面\\test1.xlsx"); - FileOutputStream fileOutputStream = new FileOutputStream("D:\\桌面\\test1.html"); - - boolean isWithStyle = true; - ExcelToHtmlUtil.excelToHtml(fileInputStream, isWithStyle, fileOutputStream); + // FileInputStream fileInputStream = new FileInputStream("D:\\桌面\\test1.xlsx"); + // FileOutputStream fileOutputStream = new FileOutputStream("D:\\桌面\\test1.html"); + // + // boolean isWithStyle = true; + // ExcelToHtmlUtil.excelToHtml(fileInputStream, isWithStyle, fileOutputStream); } } diff --git a/src/test/java/com/test/wordOutput.java b/src/test/java/com/test/wordOutput.java index f3fb2b016064cce45844edc1dd5efa58514df56d..54df01f8f2f7b6836b26eeed46cd4886f3c76752 100644 --- a/src/test/java/com/test/wordOutput.java +++ b/src/test/java/com/test/wordOutput.java @@ -2,6 +2,7 @@ package com.test; import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.utils.WordTemplateUtils; import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.utils.operationUtils.WordValueEnum; +import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.FontBean; import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.WordInfo; import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.tableBean.*; import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.textBean.OneFor; @@ -10,6 +11,7 @@ import com.xiaominge.utils.wordUtils.wordOutputUtils.wordOut.wordBean.textBean.T import org.junit.jupiter.api.Test; import java.io.FileOutputStream; +import java.io.IOException; /** * @program: poi-utils @@ -27,7 +29,8 @@ public class wordOutput { //直接渲染文字 Text text = wordInfo.createText(); text.putValue("name", "张三"); - text.putValue("sex", "男"); + FontBean fontBean = new FontBean("方正小标宋_GBK", 22); + text.putValue("sex", "男", fontBean); //渲染图片 text.putImage( @@ -112,15 +115,39 @@ public class wordOutput { } table_4_oneRow.createCell("name", "表格4合并单元格名字" + i); - table_4_oneRow.createCell("number", "测试下换线"+WordValueEnum.UnderLineStart.getValue()+"表格4合并单元格编号" + WordValueEnum.UnderLineEnd.getValue() + i); + table_4_oneRow.createCell("number", "测试下换线" + WordValueEnum.UnderLineStart.getValue() + "表格4合并单元格编号" + + WordValueEnum.UnderLineEnd.getValue() + i, fontBean); } //加载模板 WordTemplateUtils wordTemplateUtils = new WordTemplateUtils(this.getClass().getResourceAsStream("/wordOutput/template.docx")); //变量替换 wordTemplateUtils.replaceDocument(wordInfo); - FileOutputStream fileOutputStream = new FileOutputStream("D:\\桌面\\aa123.docx"); + FileOutputStream fileOutputStream = new FileOutputStream(System.getProperty("user.dir") + "\\aa123.docx"); //进行输出 wordTemplateUtils.write(fileOutputStream); } + + @Test + void testTable() throws Exception { + WordInfo wordInfo = new WordInfo(); + Text text = wordInfo.getText(); + text.putValue("school", "重庆二外"); + + TableBeans tableBean = wordInfo.createTableBean(); + tableBean.CreateOneTable("table_1", TableType.No_todo, false); + tableBean.CreateOneTable("table_2", TableType.No_todo, false); + tableBean.CreateOneTable("table_3", TableType.No_todo, false); + tableBean.CreateOneTable("table_4", TableType.No_todo, false); + + + WordTemplateUtils wordTemplateUtils = new WordTemplateUtils( + this.getClass().getResourceAsStream("/wordOutput/记录表-流动监考员、网络监考员记录表.docx")); + // 变量替换 + wordTemplateUtils.replaceDocument(wordInfo); + + FileOutputStream fileOutputStream = new FileOutputStream(System.getProperty("user.dir") + "\\321.docx"); + //进行输出 + wordTemplateUtils.write(fileOutputStream); + } }