IDEA中JavaExcel和Word文件导出下载

7.28Excel和Word文件导出

文件导出

点击HTML前台导出按钮、

Onclick=:”jshelp.exportExcel()”

查看方法

Exportexcel()

调到common.js页面

var jsHelp = {
    exportExcel: function () {

        $.ajax({
            type: "POST",
            url: "/electricityTrading/priceManagement/exprotExcel",
            data: {},
            cache: false,
            dataType: 'json',
            success: function (result) {
                jsHelp.downloadFilesByNodeName(result.msg,"")



            },
            error: function (error) {

            }
        });
    },

查看URL:url: "/electricityTrading/priceManagement/exprotExcel"

 

/**
     * 根据excel模板导出数据
     * @return
     */
    @RequestMapping("exprotExcel")
    @ResponseBody
    public AjaxResult exprotExcel() {
        TradeDetail tradeDetail=new TradeDetail();
        List list = priceManagementService.selectTradeDetailList(tradeDetail);
        freeMarkerExport fw=new freeMarkerExport();//实例化该导出类
        Map dataMap = new HashMap();
        try{
            dataMap.put("list",list);//将数据装进Map
            dataMap.put("year", "2020");
            dataMap.put("time", "2");
            String excelName=fw.generateExcel(dataMap);//调用导出方法
            String host = env.getProperty("Host");
            String uploadPath = env.getProperty("uploadWordPath");
            return  success(host+uploadPath+excelName);
//            return "http://localhost:8089/upload/购售电信息明细.xls";
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

查看他内部的方法

 String excelName=fw.generateExcel(dataMap)

 /**
     * dataMap为要装载的数据
     *
     * @param dataMap
     */
    public String generateExcel(Map dataMap) {

        // 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servletclasspath,数据库装载,

        // 这里我的模板是放在resources/ftl包下(放在其它位置总会报文件找不到)

        configuration.setClassForTemplateLoading(this.getClass(),

                "/ftl");

        Template t = null;

        try {

            // test2.ftl为要装载的模板

            t = configuration.getTemplate("detail.ftl");

            t.setEncoding("utf-8");

        } catch (IOException e) {

            e.printStackTrace();

        }

        // 输出文档路径及名称


        String name = "购售电信息明细.xls";

        String filePath = Global.getTemplatePath() + "/" + name;
        //输出文档路径及名称
        File outFile = new File(filePath);

//        File outFile = new File("E:/detail3.xls");

        Writer out = null;

        try {

            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));


        } catch (Exception e1) {

            e1.printStackTrace();

        }

        try {

            t.process(dataMap, out);

            out.close();

        } catch (TemplateException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }
        return name;
    }

 

Word文件下载导出

html中代码

actions.push('导出合同 ');

common.js中代码

exportWord: function (contractId) {

$.ajax({
type: "POST",
url: "/electricityTrading/agencyContract/exportWord",
data: {contractId:contractId},
cache: false,
dataType: 'json',
success: function (result) {
jsHelp.downloadFilesByNodeName(result.msg,"")
},
error: function (error) {

}
});
},java 中代码
@GetMapping("/detail")
public String detail() {
return prefix + "/detail";
}

/**
* 根据word模板导出数据
*/
@RequestMapping("/exportWord")
@ResponseBody
public AjaxResult exportWord(String contractId) {
TradeDetail tradeDetail = new TradeDetail();
BizAgencyContract bizAgencyContract = bizAgencyContractService.selectBizAgencyContractById(Long.parseLong(contractId));
freeMarkerExport fw = new freeMarkerExport();//实例化该导出类
Map dataMap = new HashMap();
try {
String wordName= fw.generateWord(bizAgencyContract);//调用导出方法
String host = env.getProperty("Host");
String uploadPath = env.getProperty("uploadWordPath");
return success(host+uploadPath+wordName);
// return url;
} catch (Exception e) {
e.printStackTrace();
return null;
}

}
String wordName= fw.generateWord(bizAgencyContract);//调用导出方法、

进入该方法
public String generateWord(BizAgencyContract bizAgencyContract) {

Map dataMap = new HashMap();
try {
//编号
dataMap.put("contractNo", bizAgencyContract.getContractNo());
dataMap.put("customerName", bizAgencyContract.getCustomerName());
dataMap.put("signedDate", bizAgencyContract.getSignedDate().toString());
dataMap.put("signedAddr", bizAgencyContract.getSignedAddr());
dataMap.put("customerName", bizAgencyContract.getCustomerName());
dataMap.put("familyAddr", bizAgencyContract.getFamilyAddr());
dataMap.put("legalPerson", bizAgencyContract.getLegalPerson());
dataMap.put("creditCode", bizAgencyContract.getCreditCode());
dataMap.put("voltCode", bizAgencyContract.getVoltCode());
dataMap.put("elecTypeCode", bizAgencyContract.getElecTypeCode());
dataMap.put("familyAddr", bizAgencyContract.getFamilyAddr());
dataMap.put("consNo", "");
dataMap.put("contractCap", "");
dataMap.put("electricitySum", bizAgencyContract.getElectricitySum());
dataMap.put("familyAddr", bizAgencyContract.getFamilyAddr());
dataMap.put("faxNo", bizAgencyContract.getFaxNo());
dataMap.put("email", bizAgencyContract.getEmail());
dataMap.put("firstContact", bizAgencyContract.getFirstContact());
dataMap.put("firstOfficeTel", bizAgencyContract.getFirstOfficeTel());
dataMap.put("firstMobile", bizAgencyContract.getFirstMobile());
dataMap.put("secondContact", bizAgencyContract.getSecondContact());
dataMap.put("secondOfficeTel", bizAgencyContract.getSecondOfficeTel());
dataMap.put("secondMobile", bizAgencyContract.getSecondMobile());
dataMap.put("customerName", bizAgencyContract.getCustomerName());
dataMap.put("legalPerson", bizAgencyContract.getLegalPerson());
dataMap.put("signedDate", bizAgencyContract.getSignedDate().toString());
dataMap.put("familyAddr", bizAgencyContract.getFamilyAddr());
dataMap.put("postalCode", bizAgencyContract.getPostalCode());
dataMap.put("firstContact", bizAgencyContract.getFirstContact());
dataMap.put("firstOfficeTel", bizAgencyContract.getFirstOfficeTel());
dataMap.put("faxNo", bizAgencyContract.getFaxNo());
dataMap.put("bankName", bizAgencyContract.getBankName());
dataMap.put("bankAcct", bizAgencyContract.getBankAcct());
dataMap.put("creditCode", bizAgencyContract.getCreditCode());
//Configuration 用于读取ftl文件
Configuration configuration = new Configuration(new Version("2.3.0"));
configuration.setDefaultEncoding("utf-8");

/**
* 以下是两种指定ftl文件所在目录路径的方式,注意这两种方式都是
* 指定ftl文件所在目录的路径,而不是ftl文件的路径
*/
//指定路径的第一种方式(根据某个类的相对路径指定)
// configuration.setClassForTemplateLoading(this.getClass(), "");

//指定路径的第二种方式,我的路径是C:/a.ftl
configuration.setDirectoryForTemplateLoading(new File(Global.getTemplatePath()));
Calendar date = Calendar.getInstance();

String year = String.valueOf(date.get(Calendar.YEAR));
String name = year + "年购售电代理合同.doc";

String filePath = Global.getTemplatePath() + "/" + name;
//输出文档路径及名称
File outFile = new File(filePath);

//以utf-8的编码读取ftl文件
Template template = configuration.getTemplate("agencyContract.ftl", "utf-8");
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
template.process(dataMap, out);
out.close();
// String host = env.getProperty("Host");
// String uploadPath = env.getProperty("uploadPath");
// return host+uploadPath;
return name;
} catch (Exception e) {
e.printStackTrace();
return null;
}

}
 

你可能感兴趣的:(IDEA中JavaExcel和Word文件导出下载)