asposeword实现doc模板操作-实例总结
项⽬中⽤到了根据模板⽣成word⽂件的功能,需要根据模板填充数据,这⾥介绍⼀下使⽤到的asposeword的学习总结
⼀下根据实际开发展⽰如何应⽤
准备⼯作
项⽬使⽤spring boot进⾏maven依赖集成
jar包⽹盘提取:
<!-- 报表依赖 -->
<dependency>
getsavefilename<groupId>com.aspose-words</groupId>
<artifactId>aspose-words</artifactId>
<version>16.8.0</version>
</dependency>
resources下配置认证⽂件l:
<License>
<Data>
<Products>
<Product>Aspose.Total for Java</Product>
<Product>Aspose.Words for Java</Product>
</Products>
<EditionType>Enterprise</EditionType>
<SubscriptionExpiry>20991231</SubscriptionExpiry>
<LicenseExpiry>20991231</LicenseExpiry>
<SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
</Data>
<Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55G </License>
解决jar包由于maven仓库中没有源导致⽆法下载,需要⼿动添加问题:
1. 将依赖的jar包放置在本地磁盘中
2. 确认配置有maven环境并配置有本地仓库,打开cmd
3. 执⾏以下命令(⽰例中jar包放置在F:盘符下)
mvn install:install-file -Dfile=F:\aspose-words-16.8.0-jdk16.jar -DgroupId=com.aspose-words -DartifactId=aspose-words -Dversion=16.8.0 -Dpackaging=jar
-Dfile的后⾯输⼊的为你下载的第三⽅jar包的本地⽂件路径
-DgroupId的后⾯输⼊的为你转maven jar包后groupId的标签内容<groupId>QRCode</groupId>。
-DartifactId的后⾯输⼊的为你转maven jar包后artifactId的标签内<artifactId>QRCode</artifactId>
-Dversion的后⾯输⼊的为你转maven jar包后version的标签内容版本号<version>3.0</version>
4. 确认jar导⼊到本地仓库,在项⽬中pom中添加依赖,根据上述的groupid等信息
⼯具类
AsposeWordUtil
public class AsposeWordUtil {
public class AsposeWordUtil {
private Document doc;
private DocumentBuilder builder;
private InputStream doctemplate;
/**
* 根据模板⽂件创建对象
* @param tempfilepath 模板⽂件路径
* @throws Exception
*/
public AsposeWordUtil(String tempfilepath) throws Exception{
// ClassLoader loader = Thread.currentThread().getContextClassLoader();
// String path = Resource("l").getPath();
// if(path.startsWith("/")){
// path = path.substring(1);
// }
// //Linux 环境下添加此标识,Windows环境下删除此标识
/
/ path = "/"+path;
// InputStream license = new FileInputStream(path);
//以下为认证⽂件license的存放位置
String fileName = "classpath:/l";
ResourceLoader resourceLoader = new DefaultResourceLoader();
Resource res = Resource(fileName);
InputStream license = InputStream();
License aposeLic = new License();
aposeLic.setLicense(license);
doctemplate = Resource(tempfilepath).getInputStream();
doc = new Document(doctemplate);
builder = new DocumentBuilder(doc);
}
/**
* 插⼊书签
* @param locationkey在某个书签后插⼊
* @param key书签名
* @param isnewline是否换⾏
* @throws Exception
*/
public void setBookmark(String locationkey,String key,boolean isnewline) throws Exception{ veToBookmark(locationkey);
if(isnewline) builder.writeln();
builder.startBookmark(key);
// builder.writeln(key);
}
/**
* 动态⽣成书签
* @param key
* @throws Exception
*/
public void setBookmark(String key) throws Exception{
builder.writeln();
builder.startBookmark(key);
// builder.writeln(key);
}
/**
* 书签填⼊内容
* @param key 书签名称
* @param value 填⼊值
* @throws Exception
*/
public void setBookmarksValue(String key,String value) throws Exception{
}
/**
* 多个书签填⼊内容
* @param kvs 书签名称和填⼊值map
* @throws Exception
*/
public void setBookmarksValue(Map<String,String> kvs) throws Exception{
for(Map.Entry<String, String> Set()){
Key(),Value());
}
}
/**
* 插⼊另⼀个⽂档的内容
* @param key 书签位置
* @param out 图⽚流
* @throws Exception
*/
public void setBookmarkDocument(String key,String filename) throws Exception{
Document doc1=new Instance().getReportTemplate(filename)); veToBookmark(key);
builder.insertDocument(doc1, ImportFormatMode.KEEP_DIFFERENT_STYLES);
}
/**
* 插⼊图⽚
* @param key 书签位置
* @param filePath 图⽚⽂件路径
* @throws Exception
*/
public void setBookmarkImage(String key,String filePath) throws Exception{
builder.insertImage(filePath);
}
/**
* 插⼊图⽚
* @param key 书签位置
* @param out 图⽚流
* @throws Exception
*/
public void setBookmarkImage(String key,ByteArrayOutputStream out) throws Exception{
ByteArrayOutputStream baos = (ByteArrayOutputStream) out;
ByteArrayInputStream swapStream = new ByteArray());
builder.insertImage(swapStream);
}
/
**
* 表格增加⾏
* @throws Exception
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public void table(List<ParagraphBean> pblist) throws Exception{
NodeCollection allTables = ChildNodes(NodeType.TABLE, true); //获取模板中的所有表格 Table table = ((0); //取到第⼀个表格
// 创建⾏
Row r = new Row(doc);
Cell c = new Cell(doc);
for(ParagraphBean pb:pblist){
Type()==1){
//⽂本
Paragraph p = new Paragraph(doc);
Run run = new Run(Data().toString());
p.appendChild(run);
Alignment()!=-1){
}
c.appendChild(p);
}else Type()==2){
Paragraph p = new Paragraph(doc);
//图⽚
List<ByteArrayOutputStream> baos = (List<ByteArrayOutputStream>)pb.getData(); for(ByteArrayOutputStream bao:baos){
ByteArrayInputStream swapStream = new ByteArray()); /*
Shape shape=new Shape(doc,ShapeType.IMAGE);
//shape.setHeight(200);
*/
Shape shape = builder.insertImage(swapStream);
p.appendChild(shape);
}
c.appendChild(p);
}
}
}
/**
* 保存到内存流中
* @return
* @throws Exception
*/
public ByteArrayOutputStream save() throws Exception{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
doc.save(outputStream, SaveFormat.DOC);
return outputStream;
}
}
ParagraphBean
public class ParagraphBean {
private int type; // 类型【1:⽂本,2:图⽚】
private int alignment; // 对齐⽅式
private Object data; // 数据
/
**
* 初始化段
* @param type 类型【1:⽂本,2:图⽚】
* @param data 数据
*/
public ParagraphBean(int type,Object data){
this.data=data;
this.alignment=-1;
}
public ParagraphBean(int type,int alignment,Object data){ pe=type;
this.alignment=alignment;
this.data=data;
}
/**
* 获取数据类型
* @return 类型【1:⽂本,2:图⽚】
*/
public int getType() {
return type;
}
public void setType(int type) {
}
public int getAlignment() {
return alignment;
}
public void setAlignment(int alignment) {
this.alignment = alignment;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论