VUE动态生成word
发布日期:2022-02-19 23:50:34 浏览次数:42 分类:技术文章

本文共 7015 字,大约阅读时间需要 23 分钟。

不废话,直接上代码。

前端代码:

 

后台:

/** * 生成license申请单 */@RequestMapping(value = "/note", method = RequestMethod.POST)public void requestNote(@RequestBody LicenseRequestNoteModel noteModel, HttpServletRequest req, HttpServletResponse resp) {    File file = null;    InputStream fin = null;    ServletOutputStream out = null;    try {        req.setCharacterEncoding("utf-8");        file = ExportDoc.createWord(noteModel, req, resp);        fin = new FileInputStream(file);        resp.setCharacterEncoding("utf-8");        resp.setContentType("application/octet-stream");        resp.addHeader("Content-Disposition", "attachment;filename="+ noteModel.getOrgName()+"申请单.doc");        resp.flushBuffer();        out = resp.getOutputStream();        byte[] buffer = new byte[512];  // 缓冲区        int bytesToRead = -1;        // 通过循环将读入的Word文件的内容输出到浏览器中        while ((bytesToRead = fin.read(buffer)) != -1) {            out.write(buffer, 0, bytesToRead);        }    } catch (Exception e) {        e.printStackTrace();    } finally {        try {            if (fin != null) fin.close();            if (out != null) out.close();            if (file != null) file.delete(); // 删除临时文件        } catch (IOException e) {            e.printStackTrace();        }    }}

 

 

public class ExportDoc {    private static final Logger logger = LoggerFactory.getLogger(ExportDoc.class);    // 针对下面这行有的报空指针,是目录问题,我的目录(项目/src/main/java,项目/src/main/resources),这块也可以自己指定文件夹    private static final String templateFolder = ExportDoc.class.getClassLoader().getResource("/").getPath();    private static Configuration configuration = null;    private static Map
allTemplates = null; static { configuration = new Configuration(); configuration.setDefaultEncoding("utf-8"); allTemplates = new HashedMap(); try { configuration.setDirectoryForTemplateLoading(new File(templateFolder)); allTemplates.put("resume", configuration.getTemplate("licenseApply.ftl")); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } public static File createWord(LicenseRequestNoteModel noteModel, HttpServletRequest req, HttpServletResponse resp) throws Exception { File file = null; req.setCharacterEncoding("utf-8"); // 调用工具类WordGenerator的createDoc方法生成Word文档 file = createDoc(getData(noteModel), "resume"); return file; } public static File createDoc(Map
dataMap, String type) { String name = "temp" + (int) (Math.random() * 100000) + ".doc"; File f = new File(name); Template t = allTemplates.get(type); try { // 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开 Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8"); t.process(dataMap, w); w.close(); } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException(ex); } return f; } private static Map
getData(LicenseRequestNoteModel noteModel) throws Exception { Map
map = new HashedMap(); map.put("orgName", noteModel.getOrgName()); map.put("applyName", noteModel.getApplyName()); map.put("applyPhone", noteModel.getApplyPhone()); map.put("ncVersion", noteModel.getNcVersionModel()); map.put("environment", noteModel.getEnvironmentModel()); map.put("applyType", noteModel.getApplyTypeModel()); map.put("mac", GetLicenseSource.getMacId()); map.put("ip", GetLicenseSource.getLocalIP()); map.put("startData", DateUtil.Date(noteModel.getStartData())); map.put("endData", DateUtil.Date(noteModel.getEndData())); map.put("hostName", noteModel.getHostNames()); map.put("vmemo", noteModel.getVmemo()); return map; }}
public class LicenseRequestNoteModel{    private String orgName = null;    private String applyName = null;    private String applyPhone = null;        private String ncVersionModel= null;    private String environmentModel= null;    private String applyTypeModel= null;    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")    @DateTimeFormat(pattern = "yyyy-MM-dd")    private Date startData= null;    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")    @DateTimeFormat(pattern = "yyyy-MM-dd")    private Date endData= null;    private String[] hostName= null;    private String vmemo= null;    private String applyMAC= null;    private String applyIP= null;    public String getOrgName() {        return orgName;    }    public void setOrgName(String projectName) {        this.orgName = projectName;    }    public String getApplyName() {        return applyName;    }    public void setApplyName(String applyName) {        this.applyName = applyName;    }    public String getApplyPhone() {        return applyPhone;    }    public void setApplyPhone(String applyPhone) {        this.applyPhone = applyPhone;    }    public String getNcVersionModel() {        return ncVersionModel;    }    public void setNcVersionModel(String ncVersionModel) {        this.ncVersionModel = ncVersionModel;    }    public String getEnvironmentModel() {        return environmentModel;    }    public void setEnvironmentModel(String environmentModel) {        this.environmentModel = environmentModel;    }    public String getApplyTypeModel() {        return applyTypeModel;    }    public void setApplyTypeModel(String applyTypeModel) {        this.applyTypeModel = applyTypeModel;    }    public Date getStartData() {        return startData;    }    public void setStartData(Date startData) {        this.startData = startData;    }    public Date getEndData() {        return endData;    }    public void setEndData(Date endData) {        this.endData = endData;    }    public String[] getHostName() {        return hostName;    }    public String getHostNames() {        return StringUtils.join(this.hostName,",");    }    public void setHostName(String[] hostName) {        this.hostName = hostName;    }    public String getVmemo() {        return vmemo;    }    public void setVmemo(String vmemo) {        this.vmemo = vmemo;    }    public String getApplyMAC() {        return applyMAC;    }    public void setApplyMAC(String applyMAC) {        this.applyMAC = applyMAC;    }    public String getApplyIP() {        return applyIP;    }    public void setApplyIP(String applyIP) {        this.applyIP = applyIP;    }}

 

 

 

 

 

 

 

转载地址:https://blog.csdn.net/LesonSong/article/details/80137218 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:oracle闪回功能详解(oracle独有的flashback功能)
下一篇:PHP使用Sphinx API对Mysql数据进行全文检索

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月15日 04时58分23秒