Commit 5f759cf3 by yuwei

2.0.0项目初始化

parent 5fbb041f
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>datax-common</artifactId>
<groupId>cn.datax</groupId>
<version>2.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<version>2.0.0</version>
<artifactId>datax-common-office</artifactId>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>${aspose.version}</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package cn.datax.commo.office.word;
import com.aspose.words.IMailMergeDataSource;
import com.aspose.words.ref.Ref;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MapMailMergeDataSource implements IMailMergeDataSource {
private List<Map<String, Object>> dataList;
private int index;
/**
* word模板中的«TableStart:tableName»«TableEnd:tableName»对应
*/
private String tableName = null;
/**
* @param dataList 数据集
* @param tableName 与模板中的Name对应
*/
public MapMailMergeDataSource(List<Map<String, Object>> dataList, String tableName) {
this.dataList = dataList;
this.tableName = tableName;
index = -1;
}
/**
* @param data 单个数据集
* @param tableName 与模板中的Name对应
*/
public MapMailMergeDataSource(Map<String, Object> data, String tableName) {
if (this.dataList == null) {
this.dataList = new ArrayList<Map<String, Object>>();
this.dataList.add(data);
}
this.tableName = tableName;
index = -1;
}
/**
* 获取结果集总数
*
* @return
*/
private int getCount() {
return this.dataList.size();
}
@Override
public IMailMergeDataSource getChildDataSource(String arg0)
throws Exception {
return null;
}
@Override
public String getTableName() throws Exception {
return this.tableName;
}
/**
* 实现接口
* 获取当前index指向数据行的数据
* 将数据存入args数组中即可
*
* @return ***返回false则不绑定数据***
*/
@Override
public boolean getValue(String key, Ref<Object> args) throws Exception {
if (index < 0 || index >= this.getCount()) {
return false;
}
if (args != null) {
args.set(this.dataList.get(index).get(key));
return true;
} else {
return false;
}
}
/**
* 实现接口
* 判断是否还有下一条记录
*/
@Override
public boolean moveNext() throws Exception {
index += 1;
if (index >= this.getCount()) {
return false;
}
return true;
}
}
package cn.datax.commo.office.word;
import com.aspose.words.Document;
import com.aspose.words.MailMerge;
import java.util.List;
import java.util.Map;
public class MergeDataSource {
/**
* word模板普通数据填充
* @param name
* @param value
* @param modelPath
* @return
* @throws Exception
*/
public Document load(String[] name, Object[] value, String modelPath) throws Exception {
Document doc = new Document(modelPath);
// 这里可以做特殊字段处理(如:图片插入、字符对应的特殊符号[https://wenku.baidu.com/view/81b41244336c1eb91a375dcb.html])
// DocumentBuilder builder = new DocumentBuilder(doc);
// builder.moveToMergeField(key);
// builder.insertImage((BufferedImage) value);
MailMerge merge = doc.getMailMerge();
merge.execute(name, value);
return doc;
}
/**
* word模板里有集合的表格填充
* @param name
* @param value
* @param modelPath
* @param dataList
* @return
* @throws Exception
*/
public Document load(String[] name, Object[] value, String modelPath, List<Map<String, Object>> dataList, String tableName) throws Exception {
Document doc = new Document(modelPath);
MailMerge merge = doc.getMailMerge();
doc.getMailMerge().executeWithRegions(new MapMailMergeDataSource(dataList, tableName));
merge.execute(name, value);
return doc;
}
}
package cn.datax.commo.office.word;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.aspose.words.SaveOptions;
import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WordUtil {
private WordUtil() {}
private static volatile WordUtil instance;
public static WordUtil getInstance() {
if(instance == null) {
synchronized (WordUtil.class) {
if(instance == null) {
instance = new WordUtil();
}
}
}
return instance;
}
/**
* 去除水印
*/
static {
try {
new License().setLicense(new ByteArrayInputStream("license".getBytes("UTF-8")));
} catch (Exception e) {}
}
/**
* 普通数据模板 返回缓冲输入流
*
* @param name
* @param value
* @param modelPath 模板文件 F:\模板.docx
* @return 缓冲输入流 供controller层下载
* @throws Exception
*/
public ByteArrayInputStream fillWordData(String[] name, Object[] value, String modelPath) throws Exception {
Document doc = new MergeDataSource().load(name, value, modelPath);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
doc.save(bos, SaveOptions.createSaveOptions(SaveFormat.DOCX));
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
return bis;
}
/**
* 普通数据模板 直接保存到指定位置
*
* @param name
* @param value
* @param modelPath 模板文件 F:\模板.docx
* @param destPath 保存文件 F:\测试.docx
* @throws Exception
*/
public void fillWordData(String[] name, Object[] value, String modelPath, String destPath) throws Exception {
Document doc = new MergeDataSource().load(name, value, modelPath);
doc.save(destPath, SaveOptions.createSaveOptions(SaveFormat.DOCX));
}
/**
* 带集合的数据模板 返回缓冲输入流
*
* @param name
* @param value
* @param modelPath 模板文件 F:\模板.docx
* @param dataList 集合数据
* @param tableName 集合名称
* @throws Exception
*/
public ByteArrayInputStream fillWordListData(String[] name, Object[] value, String modelPath, List<Map<String, Object>> dataList, String tableName) throws Exception {
Document doc = new MergeDataSource().load(name, value, modelPath, dataList, tableName);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
doc.save(bos, SaveOptions.createSaveOptions(SaveFormat.DOCX));
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
return bis;
}
/**
* 带集合的数据模板 直接保存到指定位置
*
* @param name
* @param value
* @param modelPath 模板文件 F:\模板.docx
* @param destPath 保存文件 F:\测试.docx
* @param dataList 集合数据
* @param tableName 集合名称
* @throws Exception
*/
public void fillWordListData(String[] name, Object[] value, String modelPath, String destPath, List<Map<String, Object>> dataList, String tableName) throws Exception {
Document doc = new MergeDataSource().load(name, value, modelPath, dataList, tableName);
doc.save(destPath, SaveOptions.createSaveOptions(SaveFormat.DOCX));
}
/**
* word转pdf
* @param srcPath 文件路径 F:\\test\\审批流提交.docx
* @param destPath 目标路径 F:\\test\\20200420.pdf
* @throws Exception
*/
public void word2pdf(String srcPath, String destPath) throws Exception {
// 转换开始前时间
long old = System.currentTimeMillis();
// 要转换的word文档的路径
Document doc = new Document(srcPath);
// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
doc.save(destPath, SaveOptions.createSaveOptions(SaveFormat.PDF));
// 转换结束后时间
long now = System.currentTimeMillis();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
}
public static void main(String[] args) throws Exception {
Map<String, Object> map = new HashMap<>();
map.put("companyName", "测试");
map.put("totalSalary", new BigDecimal("12.34"));
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> map1 = new HashMap<>();
map1.put("name", "测试1");
map1.put("age", 12);
map1.put("sex", "男");
map1.put("salary", new BigDecimal("5.0"));
list.add(map1);
Map<String, Object> map2 = new HashMap<>();
map2.put("name", "测试2");
map2.put("age", 14);
map2.put("sex", "女");
map2.put("salary", new BigDecimal("7.34"));
list.add(map2);
WordUtil.getInstance().fillWordListData(new String[]{"companyName", "totalSalary"}, new Object[]{"测试", new BigDecimal("12.34")}, "F:\\test\\模板.docx", "F:\\test\\123.docx", list, "workerList");
// WordUtil.getInstance().word2pdf("F:\\test\\审批流提交.docx", "F:\\test\\20200420.pdf");
}
}
......@@ -19,5 +19,6 @@
<module>datax-common-security</module>
<module>datax-common-log</module>
<module>datax-common-database</module>
<module>datax-common-office</module>
</modules>
</project>
\ No newline at end of file
......@@ -45,6 +45,7 @@
<oracle.version>19.3.0.0</oracle.version>
<postgresql.version>42.2.11</postgresql.version>
<sqlserver.version>8.2.1.jre8</sqlserver.version>
<aspose.version>20.3</aspose.version>
</properties>
<modules>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment