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)。 谢谢