java根据模板导出word

word文档需另存为docx,取值规则以{{}}

 

 

1.前台代码

function exportInvoice(title,url,gname) {
var rows = $("#"+gname).datagrid('getSelections');
if(rows.length==1){
JeecgExcelExport(url+"&ids="+rows[0].id , "ldcOrderList");
}else{
tip('请选择一条数据导出')
}
}

2.后台代码

@RequestMapping(params = "exportbaojia")
@ResponseBody
public void exportbaojia(String ids,HttpServletRequest request,HttpServletResponse response
, DataGrid dataGrid,ModelMap modelMap) throws Exception {
// 1.查询数据
// 2.创建 map
Map<String,Object> data = new HashMap<>();
String type=request.getParameter("type");

LdcOrderEntity ldcOrder = ldcOrderService.getEntity(LdcOrderEntity.class, ids);

String hql0 = "from LdcOrderGoodsEntity where 1 = 1 AND fID = ? ";
List<LdcOrderGoodsEntity> ldcOrderGoodsEntityList = systemService.findHql(hql0, ldcOrder.getId());


data.put("orderSafeno",ldcOrder.getOrderSafeno()); // 检验检疫编号
data.put("fhrs",ldcOrder.getOrderFromCompany()+"/"+ldcOrder.getOrderBookReceiver()); // 发货人/生产商
data.put("fhr",ldcOrder.getOrderFromCompany()); // 发货人
data.put("phone",ldcOrder.getFromphone()); //发货人电话
data.put("fax",ldcOrder.getFrompeople()); //发货人tax
data.put("shr",ldcOrder.getOrderToSupcompany()); // 收货人
data.put("shphone",ldcOrder.getFromphone()); //收货人电话
data.put("shfax",ldcOrder.getFrompeople()); //收货人tax

List<Map<String,Object>> mplist=new ArrayList<>();

if(ldcOrderGoodsEntityList.size()>0) {
for (LdcOrderGoodsEntity map : ldcOrderGoodsEntityList) {
Map<String,Object> data1 = new HashMap<>();
data1.put("myorderno",map.getMyorderno());
data1.put("unit",map.getOrderGoodsUnit());
mplist.add(data1);
}
data.put("hwname",ldcOrderGoodsEntityList.get(0).getOrderGoodsname()); // 货物名称
data.put("qty",ldcOrderGoodsEntityList.get(0).getOrderGoodsCount()); // 数量
}else {
data.put("hwname",""); // 货物名称
data.put("qty",""); // 数量
}

data.put("infoList",mplist); //商品信息
data.put("aa",mplist); //商品信息

data.put("orderAboutno",ldcOrder.getOrderAboutno()); //合同号
data.put("orderEtdTrue",ldcOrder.getOrderEtdTrue()); //签单日期


data.put("outcountry",ldcOrder.getOrderToCountry()); // 目的国
data.put("orderBz",ldcOrder.getOrderBz()); // 包装
data.put("orderZftxq",ldcOrder.getOrderZftxq()); // 货物存放地点
data.put("number",ldcOrder.getOrderBox()); // 发货人生产商
data.put("mz",ldcOrder.getOrderTsgx()+"/"+ldcOrder.getOrderJgyx()); // 发货人生产商
// 3.处理图片
// String httpPath = request.getScheme() + "://" + request.getServerName() + user.getUrl(); // 图片路径
// WordImageEntity image = new WordImageEntity(WordUtil.getImageBase64(httpPath),384,216);
// image.setType(WordImageEntity.Data); // 设置成 Byte字节
// data.put("image",image);

String lujing = request.getSession().getServletContext().getRealPath("/");
String name="";
if(type.equals("1")) {
lujing = lujing + "export\\template\\hg.docx";
name="海关出口工业品检验原始记录.docx";
}
if(type.equals("2")) {
lujing = lujing + "export\\template\\bj.docx";
name="报检.docx";
}
if(type.equals("3")) {
lujing = lujing + "export\\template\\sc.docx";
name="SC.docx";
}
// 4.导出
WordUtil.easyPoiExport(lujing,"tempDir",name,data,request,response);

}

 

3.wordutil类

package com.jeecg.ldcorder.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.jeecgframework.poi.word.WordExportUtil;

public class WordUtil {

/**
* EasyPoi 替换数据 导出 word
* @param templatePath word模板地址
* @param tempDir 临时文件存放地址
* @param filename 文件名称
* @param data 替换参数
* @param request
* @param response
*/
public static void easyPoiExport(String templatePath, String tempDir, String filename, Map<String, Object> data, HttpServletRequest request, HttpServletResponse response) {

if (!tempDir.endsWith("/")) {
tempDir = tempDir + File.separator;
}

File file = new File(tempDir);
if (!file.exists()) {
file.mkdirs();
}

try {
String userAgent = request.getHeader("user-agent").toLowerCase();
if (userAgent.contains("msie") || userAgent.contains("like gecko")) {
filename = URLEncoder.encode(filename, "UTF-8");
} else {
filename = new String(filename.getBytes("utf-8"), "ISO-8859-1");
}

XWPFDocument document = WordExportUtil.exportWord07(templatePath, data);
String tempPath = tempDir + filename;
FileOutputStream out = new FileOutputStream(tempPath);
document.write(out);

// 设置响应规则
response.setContentType("application/force-download");
response.addHeader("Content-Disposition", "attachment;filename=" + filename);
OutputStream stream = response.getOutputStream();
document.write(stream);
stream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
deleteTempFile(tempDir, filename);
}
}

/**
* 删除临时生成的文件
*/
public static void deleteTempFile(String filePath, String fileName) {
File file = new File(filePath + fileName);
File f = new File(filePath);
file.delete();
f.delete();
}
}

 

posted @ 2022-06-02 12:07  许佳挺  阅读(1819)  评论(0编辑  收藏  举报