1、POI 导出 Excel
public static void exportUserExcel(String[] headers, List<Muser> userList, OutputStream out) { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("会员表"); HSSFRow row = sheet.createRow(0); HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); for (int i = 0; i < headers.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(style); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } for (int i = 0; i < userList.size(); i++) { Muser user = userList.get(i); row = sheet.createRow(i + 1); row.createCell(0).setCellValue(user.getId()); row.createCell(1).setCellValue(user.getName()); row.createCell(2).setCellValue(user.getPhone()); row.createCell(3).setCellValue(user.getIdCard()); row.createCell(4).setCellValue(user.getAge()); row.createCell(5).setCellValue(user.getAddress()); row.createCell(6).setCellValue(user.getBirthplace()); row.createCell(7).setCellValue(user.getCredits().toString()); row.createCell(8).setCellValue(user.getCreatedtimeString()); row.createCell(9).setCellValue(user.getLastlogtimeString()); row.createCell(10).setCellValue( user.getStatus().equals("0") ? "正常" : "黑名单"); } try { //FileOutputStream out = new FileOutputStream(path); wb.write(out); out.close(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } }
2、JXL导出Excel
public static void expertHotelOrderExcel(List<HotelOrder> hotelOrderList){ try { HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("application/octet-stream;charset=UTF-8"); response.setHeader("Content-Disposition","attachment;filename="+Helper.getDate()+".xls"); // 注意这里的header参数为Content-Disposition而非第一种中的contentDisposition参数 // Excel获得文件 // 打开一个文件的副本,并且指定数据写回到原文件 WritableWorkbook book = Workbook.createWorkbook(response.getOutputStream()); // 添加一个工作表 WritableSheet sheet = book.createSheet("酒店订单表", 0); sheet.addCell(new Label(0, 0, "客栈名称")); sheet.addCell(new Label(1, 0, "订单编号")); sheet.addCell(new Label(2, 0, "会员编号")); sheet.addCell(new Label(3, 0, "总金额")); sheet.addCell(new Label(4, 0, "实付款")); sheet.addCell(new Label(5, 0, "获取积分")); sheet.addCell(new Label(6, 0, "使用积分")); sheet.addCell(new Label(7, 0, "交易状态")); sheet.addCell(new Label(8, 0, "审核状态")); sheet.addCell(new Label(9, 0, "成交时间")); if (hotelOrderList!= null) { IHotelManager hotelManager =SpringContextHolder.getBean("hotelManager",IHotelManager.class); for (int i = 0; i < hotelOrderList.size(); i++) { HotelOrder hotelOrder = hotelOrderList.get(i); String status = ""; if (hotelOrder.getOrderStatus() == 0) { status = "未支付"; } else if (hotelOrder.getOrderStatus() == 1) { status = "交易成功"; } else { status = "交易取消"; } String auditStatus = ""; if (hotelOrder.getAuditStatus().equals("0")) { auditStatus = "未审核"; } else { auditStatus = "已审核"; } sheet.addCell(new Label(0, i+1, hotelManager.getById(hotelOrder.getHotelId()).getHotelName())); sheet.addCell(new Label(1, i+1, hotelOrder.getId())); sheet.addCell(new Label(2, i+1, hotelOrder.getUserId())); sheet.addCell(new Label(3, i+1, hotelOrder.getOrderAmount().toString())); sheet.addCell(new Label(4, i+1, hotelOrder.getPayAmount().toString())); sheet.addCell(new Label(5, i+1, hotelOrder.getGetCredits().toString())); sheet.addCell(new Label(6, i+1, hotelOrder.getUsedCredits().toString())); sheet.addCell(new Label(7, i+1, status)); sheet.addCell(new Label(8, i+1, auditStatus)); if(hotelOrder.getDealTime()==null){ sheet.addCell(new Label(9, i+1, "暂无时间")); }else{ sheet.addCell(new Label(9, i+1, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(hotelOrder.getDealTime()))); } } book.write(); book.close(); // 关闭输出流,同时也关闭response里的OutputStream,所以无需再调用response.getOutputStream().close()了 } } catch (Exception e) { e.printStackTrace(); } }