@SuppressWarnings("deprecation") public String ExpExcel(List<Code> list, String path) { String filePath = ""; if (list.size() > 0) { /*第一步,创建一个webbook,对应一个Excel文件*/ HSSFWorkbook wb = new HSSFWorkbook(); /*第二步,在webbook中添加一个sheet,对应Excel文件中的sheet*/ HSSFSheet sheet = wb.createSheet("用户列表"); /*第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short*/ HSSFRow row = sheet.createRow((int) 0); /*第四步,创建单元格,并设置值表头 设置表头居中*/ HSSFCellStyle style = wb.createCellStyle(); /*左对齐*/ style.setAlignment(HSSFCellStyle.ALIGN_LEFT); HSSFCell cell = row.createCell((short) 0); cell.setCellValue("编码名称"); cell.setCellStyle(style); cell = row.createCell((short) 1); cell.setCellValue("描述"); cell.setCellStyle(style); cell = row.createCell((short) 2); cell.setCellValue("编码类型"); cell.setCellStyle(style); cell = row.createCell((short) 3); cell.setCellValue("状态"); cell.setCellStyle(style); cell = row.createCell((short) 4); cell.setCellValue("创建日期"); cell.setCellStyle(style); /*第五步,写入实体数据 实际应用中这些数据从数据库得到,*/ for (int i = 0; i < list.size(); i++) { row = sheet.createRow((int) i + 1); Code model = (Code) list.get(i); /*第四步,创建单元格,并设置值*/ row.createCell((short) 0).setCellValue(model.getCodeName()); row.createCell((short) 1).setCellValue(model.getDescription()); row.createCell((short) 2).setCellValue(model.getCategory()); row.createCell((short) 3).setCellValue(model.getStatus()); cell = row.createCell((short) 4); cell.setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(model.getCreateTime())); } /*第六步,将文件存到指定位置*/ try { String fileName = UUID.randomUUID() + ".xls"; String tempFilePath = path + "\\" + fileName; FileOutputStream fout = new FileOutputStream(tempFilePath); wb.write(fout); fout.close(); filePath = tempFilePath; } catch (Exception e) { System.out.print(e.getMessage()); } } return filePath; }