NutzCN Logo
问答 Excel导出报错org.springframework.web.util.NestedServletException: Request processing failed;
发布于 2372天前 作者 Nooft 6114 次浏览 复制 上一个帖子 下一个帖子
标签:
    public static Boolean getExcel(HttpServletResponse response, ArrayList<ArrayList<String>> values, String fileName, String templatePath, Integer titelRow, Integer titelColumn, ArrayList<String> describe) {

        WritableWorkbook writableWorkbook = null;
        Workbook workbook = null;// 创建一个工作文件
        File excelFile = new File(fileName + ".xls");// 文件格式
        WritableCellFormat format = new WritableCellFormat();//设置样式
        WritableCellFormat format1 = new WritableCellFormat();
        WritableFont contentFont = new WritableFont(WritableFont.createFont("宋体"), 15, WritableFont.BOLD);
        try {
            format1.setAlignment(Alignment.CENTRE);
            format1.setFont(contentFont);
            //设置格式
            format.setBorder(Border.ALL, BorderLineStyle.THIN);
            Resource resource = new ClassPathResource(templatePath);
            workbook = Workbook.getWorkbook(resource.getInputStream());//获取模板文件
            writableWorkbook = Workbook.createWorkbook(excelFile, workbook);
            WritableSheet sheet = writableWorkbook.getSheet(0);
            sheet.addCell(new Label(0, 0, fileName, format1));
            int countNumber = 0;
            if (values != null) {
                for (ArrayList<String> context : values) {// 把集合写入到excel中
                    if (context != null) {
                        for (int i = 0; i < context.size(); i++) {
                            sheet.addCell(new Label(i + titelColumn, titelRow, context.get(i), format));
                        }
                    }
                    titelRow++;
                    countNumber++;
                    if (describe != null && describe.size() > 0 && values.size() == countNumber) {
                        for (String describes : describe) {
                            sheet.mergeCells(0, titelRow, 23, titelRow);
                            sheet.addCell(new Label(0, titelRow, describes, format));
                            titelRow++;
                        }
                    }

                }
            }
            writableWorkbook.write();
            writableWorkbook.close();
            response.setContentType("application/x-msdownload");
            String encodetittle = new String(fileName.getBytes("GBK"), "ISO-8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + encodetittle + ".xls");
            FileInputStream finput = new FileInputStream(excelFile);
            OutputStream output = response.getOutputStream();
            BufferedInputStream buffin = new BufferedInputStream(finput);
            BufferedOutputStream buffout = new BufferedOutputStream(output);
            byte[] buffer = new byte[4096];
            int count = 0;
            while ((count = buffin.read(buffer, 0, buffer.length)) > 0) {
                buffout.write(buffer, 0, count);
            }

            buffin.close();
            buffout.close();
            finput.close();
            output.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } catch (WriteException e) {
            e.printStackTrace();
            return false;
        } catch (BiffException e) {
            e.printStackTrace();
            return false;
        }finally {
            excelFile.delete();
        }
    }
1 回复
添加回复
该帖子已被锁定,不能回复.
回到顶部