继之前的spire,再来一篇。废话少说,就是想白嫖。上干货!
一、引入依赖
1、以下代码写入pom中
<dependency>
<groupId>aspose.words</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/aspose-words-15.8.0-jdk16.jar</systemPath>
</dependency>
2、项目里建个libs文件夹,然后把你的包或者从我这里下载的包粘贴进去
3、公共核心类:
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.License;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.util.Map;
@Component
public class AsPoseWordUtil {
private static License aposeLic = new License();
private static boolean getlic(){
if(!aposeLic.getIsLicensed()){
InputStream is = AsPoseWordUtil.class.getClassLoader()
.getResourceAsStream("asp-license/config.xml");
try {
aposeLic.setLicense(is);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return true;
}
/**
* 定位书签替换内容
* @param docUrl 文件路径
* @param savePath 保存路径
* @param wordMap
* @throws Exception
*/
public static void BookMarkReplace(String docUrl,String savePath,Map<String,String> wordMap) throws Exception
{
if(!getlic()){
System.out.print("获取证书失败");
}
Document doc = new Document(docUrl);
DocumentBuilder builder = new DocumentBuilder(doc);
//将数据插入到书签中
for(Map.Entry<String,String> item:wordMap.entrySet()) {
String keyVal = item.getKey();
String val = item.getValue();
builder.moveToBookmark(keyVal);
builder.write(val);
}
doc.save(savePath);
}
public static void CreateTable(String docUrl,String saveUrl, String bookmark,String[][] data) throws Exception {
if(!getlic()){
System.out.print("获取证书失败");
}
Document doc = new Document(docUrl);
DocumentBuilder builder = new DocumentBuilder(doc);
//移动到该书签位置
builder.moveToBookmark(bookmark);
//创建table
builder.startTable();
for(int i = 0;i<data.length;i++){
for(int j=0;j<data[i].length;j++){
//创建单元格
builder.insertCell();
//创建单元格
builder.write(data[i][j]);
}
//结束当前行
builder.endRow();
}
//结束当前表格
builder.endTable();
doc.save(saveUrl);
}
}
4、配置类,放到resources下,我的目录是见上面的图2,此处不再赘述
<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>23dcc79f-44ec-4a23-be3a-03c1632404e9</SerialNumber>
</Data>
<Signature>0nRuwNEddXwLfXB7pw66G71MS93gW8mNzJ7vuh3Sf4VAEOBfpxtHLCotymv1PoeukxYe31K441Ivq0Pkvx1yZZG4O1KCv3Omdbs7uqzUB4xXHlOub4VsTODzDJ5MWHqlRCB1HHcGjlyT2sVGiovLt0Grvqw5+QXBuinoBY0suX0=</Signature>
</License>
5、测试类
public static void main(String args[]) throws Exception {
String docUrl = "D:/ideaworkspace/files/template/test.docx";
String saveUrl = "D:/ideaworkspace/files/template/test1.docx";
Map<String,String> dataMap = new HashMap<String, String>();
dataMap.put("zxm_option","草泥马a");
dataMap.put("zxm_idea","拉到了裤子里");
AsPoseWordUtil.BookMarkReplace(docUrl,saveUrl,dataMap);
String[][] data = {
new String[]{"序号","姓名","性别","联系方式"},
new String[]{"1","朱晓明","男","123"},
new String[]{"2","潘周丹","女","321"}
};
AsPoseWordUtil.CreateTable(docUrl,saveUrl,"zxm_table",data);
System.out.print("success");
}
6、图片的有空再整,目前干货到这,大家采集用吧。
-----------------------------------------------------------参考文献----------------------------------------------------------
1、aspose插入word:https://blog.csdn.net/a3562323/article/details/123527216?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_ecpm_v1~rank_v31_ecpm-11-123527216-null-null.pc_agg_new_rank&utm_term=asposewords%E5%BE%AA%E7%8E%AF%E6%8F%92%E5%85%A5&spm=1000.2123.3001.4430