创建生成周报excel表(一)-问题 无法使用ajax做excel导出

目前前端html:




    
    我不要写周报1.0







请填写您的名称
用户名

js:

var department = '研发部';

var summaryList = new Array();
summaryList.push('summaryList1');
summaryList.push('summaryList2');
summaryList.push('summaryList3');
summaryList.push('summaryList4');

var executionList = new Array();
executionList.push('summaryList1');
executionList.push('summaryList2');
executionList.push('summaryList3');
executionList.push('summaryList4');

var executionList = new Array();
executionList.push('executionList1');
executionList.push('executionList2');
executionList.push('executionList3');
executionList.push('executionList4');

var planList = new Array();
planList.push('planList1');
planList.push('planList2');
planList.push('planList3');
planList.push('planList4');

var planResultList = new Array();
planResultList.push('planResultList1');
planResultList.push('planResultList2');
planResultList.push('planResultList3');
planResultList.push('planResultList4');

var nameList = new Array();
nameList.push('nameList1');
nameList.push('nameList2');
nameList.push('nameList3');
nameList.push('nameList4');


var poiBeans = new Array();
poiBeans.push(department);
poiBeans.push(JSON.stringify(summaryList));
poiBeans.push(JSON.stringify(executionList));
poiBeans.push(JSON.stringify(planList));
poiBeans.push(JSON.stringify(planResultList));
poiBeans.push(JSON.stringify(nameList));


$.ajax({
  type : 'POST',
    url:'/create',
    data: JSON.stringify(poiBeans),
    contentType : 'application/json',
    success : function (data) {
        alert("上传成功!")
        window.open('/download?data='+data);
    },
    error : function () {
        alert('error');
    }
});



预想是:

在加载 html时 通过ajax 将预设值好的 多个list转为json 最后发送至后台,将后台请求好的excel表通过新开窗口携带参数进行导出。

后台controller代码:

package com.zqj.controller;
/*
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖保佑             永无BUG     

/**
* @ClassName: poi控制层
* @Description: 生成表格(这里用一句话描述这个类的作用)
* @author Gin_HK
* @date 20200815
*
*/

import com.alibaba.fastjson.JSONObject;
import com.zqj.pojo.eneity.PoiBean;
import com.zqj.services.PoiServices;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Controller
public class PoiController {

    @Autowired
    private PoiServices poiServices;


    @RequestMapping("/createTable")
    public String getCreateTable() {
        System.out.println("createTable");
        return "createTable";
    }

    @RequestMapping("/download")
    public String getDownload() {
        System.out.println("download");
        return "download";
    }

    @RequestMapping("/create")
    @ResponseBody
    public String createWorkbook(HttpServletRequest request, HttpServletResponse response, @RequestBody List excelData) throws IOException {

        response.reset();/*清缓存*/

        String createDate = new SimpleDateFormat("YYYY-MM-dd").format(new Date());

        System.out.println("获取到数据");

        /*也只有五个*/

        /*封装数据*/
        String department = excelData.get(0);
        String jsonSummaryList = excelData.get(1);
        String jsonExecutionList = excelData.get(2);
        String jsonPlanList = excelData.get(3);
        String jsonPlanResultList = excelData.get(4);
        String jsonNameList = excelData.get(5);

        /*初始化数据*/

        List summaryList  = JSONObject.parseArray(jsonSummaryList,String.class);
        List executionList  = JSONObject.parseArray(jsonExecutionList,String.class);
        List planList  = JSONObject.parseArray(jsonPlanList,String.class);
        List planResultList  = JSONObject.parseArray(jsonPlanResultList,String.class);
        List nameList  = JSONObject.parseArray(jsonNameList,String.class);

        HSSFWorkbook excel = poiServices.createExcel(department, createDate, summaryList, executionList, planList, planResultList, nameList);

        /*导出 导出 导出 导出 导出 导出 导出 导出 导出 导出 导出 导出 */
        /*修改中文编码格式*/
        String agent = request.getHeader("USER-AGENT").toLowerCase();
        String codedFileName = java.net.URLEncoder.encode(department, "UTF-8");
        /*针对不同浏览器进行调整格式*/
        if (agent.contains("firefox")) {
            response.setCharacterEncoding("utf-8");
            response.setHeader("content-disposition", "attachment;filename=" + new String(department.getBytes(), "ISO8859-1") + ".xls");
        } else {
            response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
        }

        ServletOutputStream outputStream = response.getOutputStream();
        excel.write(outputStream);
        outputStream.close();

        return null;
    }
}

但是问题出现了:

问题原因:ajax做excel无法导出。

参考文档:https://www.cnblogs.com/coder-wf/p/12490749.html

准备明天将ajax改为使用 form进行测试。

你可能感兴趣的:(poi,前端,个人笔记,javascript,java)