Struts2导出为Excel并具有12000行大数据

时间:2018-08-17 10:57:16

标签: java excel struts2 apache-poi

            XSSFWorkbook book = new XSSFWorkbook();
            XSSFSheet sheet = book.createSheet(sheetName);
            XSSFCellStyle style = book.createCellStyle();
            XSSFFont font = book.createFont();
            font.setBold(true);
            style.setFont(font);
            for (int i = 0; i < tempList.size(); i++) {
                Map map = tempList.get(0);
                XSSFRow row = sheet.createRow(i);
                for (int j = 0; j < map.size(); j++) {

                    // Element cell = tr.select("td").get(j);
                    XSSFCell name = row.createCell((short) j);
                    name.setCellValue(String.valueOf(map.get(displayName[j])));

                    if (i == 0) {
                        name.setCellStyle(style);
                        sheet.autoSizeColumn(j);
                    }
                }
            }

            String fileName = "Extract Contacts";

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            book.write(baos);
            byte[] xls = baos.toByteArray();
            ServletOutputStream outputStream = response.getOutputStream();
            baos.writeTo(outputStream);
            response.setContentLength(xls.length);
            response.setHeader("content-Disposition", "attachment; fileName=\"Extract_Contacts.xlsx\"");
            response.setContentType("application/vnd.ms-excel");

上面的代码在最小行数为100时起作用。当行超过10000条记录时。未设置文件名和内容类型。文件名显示为generateExcel.action(即actionName)。 谢谢

0 个答案:

没有答案