使用freemarker向word中插入多列表格,需要以下条件
1)需要在模板中加入<#list listTest as listKey>标签,标识插入多行
2)需要写一个实体类,将要插入的字段(id,guid...)封装到实体类中,并提供get,set方法
3)公司的工具类DocumentHandler
代码实例
package com.dist.report;
import com.dist.define.TestVO;
import com.dist.dgpserver.base.utils.ftp.FtpConfig;
import com.dist.dto.report.ParentInforVo;
import com.dist.dto.report.ProBaseInforVo;
import com.dist.dto.report.ProLocationVo;
import com.dist.dto.report.TableDataVo;
import com.dist.util.FtpUtil;
import com.dist.util.OfficeToPdf;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 使用freemarker插入多列表格,需要以下条件
* 1)需要在模板中加入<#list listTest as listKey>标签,标识插入多行
* 把代码贴一下
* 2)需要写一个实体类,将要插入的字段(id,guid...)封装到实体类中,并提供get,set方法
* 3)公司的工具类DocumentHandler
*/
@Service("TestReport")
public class TestReport{
private static Logger LOG = Logger.getLogger(TestReport.class);
// 模板名称
private String templateName = "test.ftl";
// 文件名称
private String fileName = "test.doc";
// 扩展名,没有带点号"."
private String fileSaveDir = "rp";
@Autowired
@Qualifier("distDocument")
private DocumentHandler doc;
public void createCheckReport(String localPath) {
long startTime = System.currentTimeMillis();
// LOG.info("合规检查报告JSON:"+jsonString);
Map<String, Object> dataMap = new HashMap<String, Object>();
ArrayList<TestVO> list = new ArrayList<>();
TestVO vo = new TestVO("1","1","1");
TestVO vo2 = new TestVO("2","2","2");
list.add(vo);
list.add(vo2);
dataMap.put("listTest",list);
doc.setDataMap(dataMap);
doc.setTemplateName(this.templateName);
doc.setSaveFileDir(localPath+ fileSaveDir);
doc.setFileName(this.fileName);
// create report
doc.createDoc();
System.out.println("************return file path:" + doc.getReturnFileFullPath());
long endTime2 = System.currentTimeMillis();
LOG.info("单纯生成报告耗时:" + (endTime2 - startTime) + "毫秒/ms");
}
public DocumentHandler getDoc() {
return doc;
}
public void setDoc(DocumentHandler doc) {
this.doc = doc;
}
}