1 package projectUtil; 2 3 import org.apache.commons.lang3.StringUtils; 4 import org.apache.poi.hssf.usermodel.*; 5 import org.apache.poi.ss.usermodel.HorizontalAlignment; 6 import org.apache.poi.ss.usermodel.VerticalAlignment; 7 8 import java.util.List; 9 import java.util.Map; 10 11 /** 12 * 建立Excel表 13 * @author tian 14 * @date 2019/3/2315:46 15 */ 16 public class WorkbookUtil { 17 private HSSFWorkbook workbook = new HSSFWorkbook();// 建立一個Excel文件 18 19 20 public HSSFWorkbook getWorkbook() { 21 return this.workbook; 22 } 23 24 public HSSFSheet writeExcel(List<Map<String, Object>> list, List<String> head, String sheetName) { 25 HSSFSheet sheet;// 建立一個Excel的Sheet 26 if (!StringUtils.isBlank(sheetName)) { 27 sheet = workbook.createSheet(sheetName);// 建立一個Excel的Sheet 28 } else { 29 sheet = workbook.createSheet("Sheet");// 建立一個Excel的Sheet 30 } 31 HSSFRow row4 = sheet.createRow(0); 32 33 HSSFCellStyle style4 = workbook.createCellStyle(); 34 style4.setAlignment(HorizontalAlignment.CENTER);//水平居中 35 style4.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中 36 style4.setWrapText(true);//自動換行 37 style4.setAlignment(HorizontalAlignment.CENTER);//水平居中 38 style4.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中 39 HSSFFont font4 = workbook.createFont(); 40 // font.setFontName("華文行楷");//設置字體名稱 41 font4.setFontHeightInPoints((short) 12);//設置字號 42 // font4.setBold( true); 43 style4.setFont(font4); 44 for (int i = 0; i < head.size(); i++ 45 ) { 46 sheet.setColumnWidth(i, 20 * 256); 47 HSSFCell cell4_1 = row4.createCell(i); 48 cell4_1.setCellValue(head.get(i)); 49 cell4_1.setCellStyle(style4); 50 } 51 for (int i = 0; i < list.size(); i++) { 52 HSSFRow row5 = sheet.createRow(i + 1); 53 for (int y = 0; y < head.size(); y++ 54 ) { 55 HSSFCell cell5_1 = row5.createCell(y); 56 cell5_1.setCellValue(list.get(i).get(head.get(y)) == null ? "" : list.get(i).get(head.get(y)) + ""); 57 cell5_1.setCellStyle(style4); 58 } 59 60 } 61 62 return sheet; 63 } 64 65 66 }
本身寫的工具類,能夠參考一下,若是有bug但願各位大俠指教。java
1 List<Map<String, Object>> rows = new ArrayList<>(); 2 List<Map<String, Object>> zifu = new ArrayList<>(); 3 List<Map<String, Object>> yinhang = new ArrayList<>(); 4 for (Map one : list) { 5 Integer state = (Integer) one.get("state"); 6 Map<String, Object> reMap = new LinkedHashMap<>(); 7 reMap.put("提現編號", one.get("id")); 8 reMap.put("用戶ID", one.get("userId")); 9 reMap.put("用戶名稱", one.get("name")); 10 reMap.put("用戶手機號", one.get("phone")); 11 reMap.put("銀行卡戶主名稱", one.get("userName")); 12 reMap.put("銀行", one.get("bankName")); 13 reMap.put("所屬支行", one.get("subBranchName")); 14 reMap.put("銀行卡號", one.get("cardNumber")); 15 reMap.put("提現金額", one.get("arrivalAmountMoney")); 16 reMap.put("提現狀態", state == -1 ? "處理中" : state == 1 ? "已處理" : state == 0 ? "待處理" : state == 2 ? "信息錯誤" : "千萬不要打款"); 17 reMap.put("支付寶帳號", one.get("withdraw")); 18 reMap.put("提現方式", (Integer) one.get("withdrawType") == 0 ? "支付寶" : "銀行卡"); 19 reMap.put("備註", one.get("remark")); 20 reMap.put("打款進度", ""); 21 rows.add(reMap); 22 if ((Integer) one.get("withdrawType") == 0) {//支付寶提現 23 Map<String, Object> ss = reMap; 24 ss.remove("銀行卡戶主名稱", one.get("userName")); 25 ss.remove("銀行", one.get("bankName")); 26 ss.remove("銀行卡號", one.get("cardNumber")); 27 ss.remove("所屬支行", one.get("subBranchName")); 28 zifu.add(ss); 29 } 30 if ((Integer) one.get("withdrawType") == 1) {//銀行卡提現 31 Map<String, Object> ss = reMap; 32 ss.remove("支付寶帳號", one.get("withdraw")); 33 yinhang.add(ss); 34 } 35 } 36 List<String> head = new ArrayList<>(); 37 head.add("提現編號"); 38 head.add("用戶ID"); 39 head.add("用戶名稱"); 40 head.add("用戶手機號"); 41 head.add("銀行卡戶主名稱"); 42 head.add("銀行"); 43 head.add("所屬支行"); 44 head.add("銀行卡號"); 45 head.add("提現金額"); 46 head.add("提現狀態"); 47 head.add("支付寶帳號"); 48 head.add("提現方式"); 49 head.add("備註"); 50 head.add("打款進度"); 51 WorkbookUtil workbookUtil = new WorkbookUtil(); 52 HSSFSheet sheet1 = workbookUtil.writeExcel(rows, head, "用戶總提現"); 53 sheet1.setColumnWidth(5,30 * 256); 54 sheet1.setColumnWidth(6,30 * 256); 55 sheet1.setColumnWidth(7,30 * 256); 56 List<String> head1= (List<String>) ((ArrayList<String>) head).clone(); 57 head1.remove(10); 58 HSSFSheet sheet2 = workbookUtil.writeExcel(yinhang, head1, "銀行卡提現"); 59 sheet2.setColumnWidth(5,30 * 256); 60 sheet2.setColumnWidth(6,30 * 256); 61 sheet2.setColumnWidth(7,30 * 256); 62 List<String> head2=(List<String>) ((ArrayList<String>) head).clone(); 63 head2.remove(4); 64 head2.remove(4); 65 head2.remove(4); 66 head2.remove(4); 67 HSSFSheet sheet3 = workbookUtil.writeExcel(zifu, head2, "支付寶提現"); 68 // 第六步,將文件存到指定位置 69 ServletOutputStream out = response.getOutputStream(); 70 try { 71 response.setContentType("application/vnd.ms-excel;charset=ISO8859-1"); 72 response.setHeader("Content-Disposition", "attachment;filename=" + new String("用戶提現".getBytes("UTF-8"), "ISO8859-1") + new SimpleDateFormat("yyyy-MM-dd->HH").format(new Date()) + ".xls"); 73 workbookUtil.getWorkbook().write(out); 74 } catch (Exception e) { 75 e.printStackTrace(); 76 } finally { 77 out.close(); 78 } 79 return;
使用的方法片斷。記錄一下apache