Spire Java代码 替换word里面表格中数据
String[][] data 里面是数据,filename是路径,整个代码没有错误,但是就是插入不进去,文件可以被找到。
public boolean wordsave(String[][] data,String filename){
String basePath = "src/main/resources/static/resources/"+filename+"/cs.docx";
Document doc = new Document();
doc.loadFromFile(basePath);
//获取表格
Section section = doc.getSections().get(0);
Table table =section.getTables().get(0);
for (int r = 0; r < data.length; r++) {
for (int c = 0; c < data[r].length; c++) {
if(c>2){
table.getRows().get(r).getCells().get(c).addParagraph().appendText(data[r][c]);
}
}
}
doc.saveToFile("cs.docx");
doc.dispose();
return true;
}
不清楚有什么问题,希望知情人士可以提供宝贵意见
package com.test;import com.spire.doc.Document;
import com.spire.doc.FileFormat;import java.util.HashMap;
import java.util.Set
;/*** @description: 替换word文档中的变量* @author: 魏一鹤* @createDate: 2023-01-08 10:51**/
public class FileTest3 {public static void main(String[] args) {
//加载Word文档
Document document = new Document("C:\\Users\\PC\\Desktop\\test.docx");
//模板变量
mapHashMap<String, Object> paramsMap = new HashMap<>();
// 填充key和vaulue key=模板变量,value=替换后的值
paramsMap.put("${name}","张三丰");paramsMap.put("${age}","18");
paramsMap.put("${sex}","男");
// 循环
keyfor(String key:paramsMap.keySet()){
// 根据key得到
valueString value = paramsMap.get(key).toString();
//使用新文本替换文档中的指定文本 也就是value替换
keydocument.replace(key, value, false, true);}
//保存文档
document.saveToFile("C:\\Users\\PC\\Desktop\\test3.docx", FileFormat.Docx_2013);
}