为什么poi excel export在我的春季项目中不起作用?

时间:2019-06-05 09:09:42

标签: java excel spring apache-poi

我是韩国人,对我的英语不佳表示歉意。

我正在尝试实现一个spring项目,其中可以使用“ poi库”将db值下载到Excel。

我的代码不起作用。 日志中没有错误,所以我什至不知道为什么。 有人知道我的代码不起作用吗?

从数据库获取数据变得很清楚。

因为这是公司项目,所以我更改了变量名称并上传了它。

控制器

@RequestMapping(value="/export/excel.download")
@ResponseBody
public void ExcelDownload(HttpServletRequest request, HttpServletResponse response) throws Exception{
    List list=tempService.getDataList();


    Calendar cal = Calendar.getInstance( );
    String fileName = "excel_"+cal.getTimeInMillis();

    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet worksheet = workbook.createSheet(fileName);

    XSSFRow row = worksheet.createRow(0);
    row.createCell(0).setCellValue("first");
    row.createCell(1).setCellValue("second");
    row.createCell(2).setCellValue("third");
    row.createCell(3).setCellValue("fourth");
    row.createCell(4).setCellValue("fifth");
    row.createCell(5).setCellValue("sixth");
    row.createCell(6).setCellValue("seventh");
    row.createCell(7).setCellValue("eighth");

    for (int i = 0, rowIndex=1; i < list.size(); i++) {

        Map item = (Map) list.get(i);

        row = worksheet.createRow(rowIndex+i);
        row.createCell(0).setCellValue(item.get(0).toString());
        row.createCell(1).setCellValue(item.get(1).toString());
        row.createCell(2).setCellValue(item.get(2).toString());
        row.createCell(3).setCellValue(item.get(3).toString()+" Days");

        if(item.get(4)!= null) 
            row.createCell(4).setCellValue(item.get(4).toString());


        String checkUseYn[]=item.get(5).toString().split(",");

        row.createCell(5).setCellValue(checkUseYn[0]);
        row.createCell(6).setCellValue(checkUseYn[1]);
        row.createCell(7).setCellValue(checkUseYn[2]);

    }

    UtilFunctions.downloadExcel(request, response, workbook, fileName);

}

UtilFunctions.downloadExcel

public static void downloadExcel(HttpServletRequest request, HttpServletResponse response, XSSFWorkbook workbook, String fileName) throws Exception{

    if(workbook != null) {
        String filename = fileName+".xlsx";
        response.setHeader("Content-Type", "application/octet-stream;charset=euc-kr");
        String userAgent = request.getHeader("User-Agent");
        if(userAgent != null && userAgent.indexOf("MSIE 5.5") > -1) 
            response.setHeader("Content-Disposition", "Filename=" + URLEncoder.encode(filename, "UTF-8") + ";");
        else if(userAgent != null && userAgent.indexOf("MSIE") > -1) 
            response.setHeader("Content-Disposition", "ATTachment; Filename=" + URLEncoder.encode(filename, "UTF-8") + ";");
        else
            response.setHeader("Content-Disposition", "ATTachment; Filename=" + new String(filename.getBytes("euc-kr"), "latin1") + ";");

        response.setHeader("Content-Transfer-Encoding", "binary");
        response.setHeader("Pragma", "no-cache;");
        response.setHeader("Expires", "-1;");

        OutputStream out = null;
        try
        {
            out = response.getOutputStream();
            workbook.write(out);
            out.flush();
            out.close();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

0 个答案:

没有答案