WordExport 增加模板读取资源文件流

This commit is contained in:
thinkgem
2020-05-22 23:46:33 +08:00
parent d834002391
commit a3dc51e730

View File

@@ -4,6 +4,7 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
@@ -11,6 +12,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment; import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
@@ -24,6 +26,8 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTrPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTrPr;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import com.jeesite.common.io.ResourceUtils;
/** /**
* 使用POI,进行Word相关的操作 * 使用POI,进行Word相关的操作
* @author xuyu * @author xuyu
@@ -41,15 +45,18 @@ public class WordExport {
/** /**
* 为文档设置模板 * 为文档设置模板
* @param templatePath 模板文件名称 * @param templatePath 模板文件名称
*/ */
public void setTemplate(String templatePath) { public void setTemplate(String templatePath) {
try { try {
this.document = new XWPFDocument(OPCPackage.open(templatePath)); if (StringUtils.startsWith(templatePath, "classpath:")) {
InputStream is = ResourceUtils.getResourceFileStream(templatePath);
document = new XWPFDocument(OPCPackage.open(is));
}else {
document = new XWPFDocument(OPCPackage.open(templatePath));
}
bookMarks = new BookMarks(document); bookMarks = new BookMarks(document);
} catch (IOException e) { } catch (IOException | InvalidFormatException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@@ -237,12 +244,12 @@ public class WordExport {
// public static void main(String[] args) { // public static void main(String[] args) {
// long startTime = System.currentTimeMillis(); // long startTime = System.currentTimeMillis();
// WordExport changer = new WordExport(); // WordExport export = new WordExport();
// String fileName = WordExport.class.getResource("Word模版.docx").getFile(); //
// fileName = EncodeUtils.decodeUrl(fileName.substring(1)); // String fileName = "classpath:" + StringUtils.substringBeforeLast(StringUtils.replace(
// System.out.println(fileName); // WordExport.class.getName(), ".", "/"), "/") + "/Word模版.docx";
// // export.setTemplate(fileName);
// changer.setTemplate(fileName); //
// Map<String, String> content = new HashMap<String, String>(); // Map<String, String> content = new HashMap<String, String>();
// content.put("Principles", "格式规范、标准统一、利于阅览"); // content.put("Principles", "格式规范、标准统一、利于阅览");
// content.put("Purpose", "规范会议操作、提高会议质量"); // content.put("Purpose", "规范会议操作、提高会议质量");
@@ -259,9 +266,9 @@ public class WordExport {
// //
// content.put("company_name", "**有限公司"); // content.put("company_name", "**有限公司");
// content.put("company_address", "机场路2号"); // content.put("company_address", "机场路2号");
// changer.replaceBookMark(content); // export.replaceBookMark(content);
// //
// //替换表格标签 // // 替换表格标签
// List<Map<String, String>> content2 = new ArrayList<Map<String, String>>(); // List<Map<String, String>> content2 = new ArrayList<Map<String, String>>();
// Map<String, String> table1 = new HashMap<String, String>(); // Map<String, String> table1 = new HashMap<String, String>();
// //
@@ -277,10 +284,10 @@ public class WordExport {
// for (int i = 0; i < 3; i++) { // for (int i = 0; i < 3; i++) {
// content2.add(table1); // content2.add(table1);
// } // }
// changer.fillTableAtBookMark("Table", content2); // export.fillTableAtBookMark("Table", content2);
// changer.fillTableAtBookMark("month", content2); // export.fillTableAtBookMark("month", content2);
// //
// //表格中文本的替换 // // 表格中文本的替换
// Map<String, String> table = new HashMap<String, String>(); // Map<String, String> table = new HashMap<String, String>();
// table.put("CUSTOMER_NAME", "**有限公司"); // table.put("CUSTOMER_NAME", "**有限公司");
// table.put("ADDRESS", "机场路2号"); // table.put("ADDRESS", "机场路2号");
@@ -290,11 +297,14 @@ public class WordExport {
// table.put("PRICE_2", "0.906"); // table.put("PRICE_2", "0.906");
// table.put("PRICE_3", "0.433"); // table.put("PRICE_3", "0.433");
// table.put("NUM_PRICE", "0.675"); // table.put("NUM_PRICE", "0.675");
// changer.replaceText(table, "Table2"); // export.replaceText(table, "Table2");
// //
// //保存替换后的WORD // // 保存替换后的 Word
// changer.saveAs(fileName + "_out.docx"); // String outFileName = "target/export.docx";
// System.out.println("time==" + (System.currentTimeMillis() - startTime)); // export.saveAs(outFileName);
// System.out.println("模板:" + fileName);
// System.out.println("输出:" + outFileName);
// System.out.println("用时:" + (System.currentTimeMillis() - startTime));
// //
// } // }