import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@Slf4j
public class DocumentHandler {
private Configuration configuration = null;
public DocumentHandler() {
configuration = new Configuration(Configuration.VERSION_2_3_27);
configuration.setDefaultEncoding("utf-8");
}
/**
* @param dataMap 数据集合
* @param fileName 导出文件名字
* @param ftlName 模板名字
* @param response 请求响应
*/
public void createDocExport(Object dataMap, String fileName, String ftlName, HttpServletResponse response) {
//dataMap 要填入模本的数据文件
//设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
//这里我们的模板是放在template包下面
response.setContentType("application/msword");
response.setCharacterEncoding("utf-8");
configuration.setClassForTemplateLoading(this.getClass(), "/templates");
try {
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
PrintWriter out = response.getWriter();
Template template = configuration.getTemplate(ftlName, "utf-8");
template.process(dataMap, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @param list 导出数据集
* @param fileName 导出文件名字
* @param zipName 压缩包名字
* @param ftlName 模板名字
* @param response 响应
* @throws IOException
*/
public void createDocZipExport(List<?> list,String fileName, String zipName,String ftlName,HttpServletResponse response) throws IOException {
log.info("createDocZipExport--------开始");
response.setContentType("application/zip");
response.setCharacterEncoding("utf-8");
ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
try {
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipName, "UTF-8"));
for (int i = 0; i < list.size(); i++) {
StringWriter writer = new StringWriter();
createDocStream(writer, list.get(i), ftlName);
writer.flush();
zipOut.putNextEntry(new ZipEntry(i + fileName));
zipOut.write(writer.toString().getBytes(StandardCharsets.UTF_8));
zipOut.closeEntry();
writer.close();
}
log.info("createDocZipExport结束");
}catch (Exception e){
e.printStackTrace();
log.error(String.valueOf(e));
}finally {
try {
zipOut.finish();
zipOut.close();
} catch (IOException e) {
e.printStackTrace();
log.error(String.valueOf(e));
}
log.info("导出完毕");
}
}
/**
* 导出word到压缩包
* @param stringWriter 字符流
* @param object 结果对象
* @param ftlName 模板名字
*/
public void createDocStream(StringWriter stringWriter,Object object, String ftlName){
log.info("createDocStream--------开始");
configuration.setClassForTemplateLoading(this.getClass(), "/templates");
try {
//通过一个文件输出流,就可以写到相应的文件中
Template template = configuration.getTemplate(ftlName, "utf-8");
template.process(object, stringWriter);
log.info("createDocStream--------结束");
} catch (Exception e) {
e.printStackTrace();
log.error(String.valueOf(e));
}
}
}
不需要生成文件 可以直接导出文件