1.使用poi导出Excel,需要poi的jar包
public static boolean OutExcel(HttpServletRequest request, HttpServletResponse response, Workbook workbook,String createExcelname) throws Exception {
boolean message = false;
String dir = request.getSession().getServletContext().getRealPath("/output");
File fileLocation = new File(dir);
if (!fileLocation.exists()) {
boolean isCreated = fileLocation.mkdir();
if (!isCreated) {
}
}
String webUrl = request.getSession().getServletContext().getRealPath("/output");
String outputFile = webUrl + File.separator + createExcelname;
FileOutputStream fOut = new FileOutputStream(outputFile);
workbook.write(fOut);
fOut.flush();
fOut.close();
File f = new File(outputFile);
if (f.exists() && f.isFile()) {
try {
FileInputStream fis = new FileInputStream(f);
URLEncoder.encode(f.getName(), "utf-8");
byte[] b = new byte[fis.available()];
fis.read(b);
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment; filename=" + createExcelname + "");
ServletOutputStream out = response.getOutputStream();
out.write(b);
out.flush();
out.close();
if (fis != null) {
fis.close();
}
f.delete();
message = true;
} catch (Exception e) {
e.printStackTrace();
}
}
return message;
}
2.测试
List<Map<String, Object>> list = this.getjyxx();
if (list == null || list.size() == 0) {
return false;
}
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
workbook.setSheetName(0, "交易信息");
HSSFRow row1 = sheet.createRow(0);
HSSFCell cell0 = row1.createCell(0, CellType.STRING);
HSSFCell cell1 = row1.createCell(1, CellType.STRING);
HSSFCell cell2 = row1.createCell(2, CellType.STRING);
HSSFCell cell3 = row1.createCell(3, CellType.STRING);
HSSFCell cell4 = row1.createCell(4, CellType.STRING);
HSSFCell cell5 = row1.createCell(5, CellType.STRING);
cell0.setCellValue("序号");
cell1.setCellValue("名称");
cell2.setCellValue("金额(万元)");
cell3.setCellValue("类别");
cell4.setCellValue("方式");
cell5.setCellValue("日期");
for (int i = 0; i < list.size(); i++) {
Map map = list.get(i);
HSSFRow row = sheet.createRow(i + 1);
HSSFCell c0 = row.createCell(0, CellType.STRING);
HSSFCell c1 = row.createCell(1, CellType.STRING);
HSSFCell c2 = row.createCell(2, CellType.STRING);
HSSFCell c3 = row.createCell(3, CellType.STRING);
HSSFCell c4 = row.createCell(4, CellType.STRING);
HSSFCell c5 = row.createCell(5, CellType.STRING);
c0.setCellValue(i + 1);
c1.setCellValue(map.get("xmmc") != null ? map.get("mc").toString() : "名为空!");
c2.setCellValue(map.get("xmje") != null ? map.get("je").toString() : "0");
c3.setCellValue(map.get("jylb") != null ? map.get("lb").toString() : "0");
c4.setCellValue(map.get("jyfs") != null ? map.get("fs").toString() : "0");
c5.setCellValue(map.get("jyrq") != null ? DateTimeUtil.cnDate((Number) map.get("rq")) : "0");
}
try {
String fileName = DateTimeUtil.now8() + "交易信息.xls";
response.setContentType("application/octet-stream");
fileName = new String(fileName.getBytes(), "ISO-8859-1");
boolean flag= ExcelUtils.OutExcel(request, response, workbook, fileName);
return flag;
} catch (Exception e) {
log.error(e.getMessage(),e);
}
return false;