根據操蛋需求寫的代碼,新手可以參考,大神勿噴!!!
package com.epipe.plm.pdc; import java.io.IOException; import java.io.OutputStream; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.util.Region; import com.rh.core.base.Bean; /** * 利用開源組件POI3.0.2動態導出EXCEL文檔 * * @author liujunzhe * @param <T> * 應用泛型,代表任意一個符合javabean風格的類 * 注意這里為了簡單起見,boolean型的屬性xxx的get器方式為getXxx(),而不是isXxx() * byte[]表jpg格式的圖片數據 */ public class ExportExcel<T> { public void exportExcel(String title, List<Bean> beans, OutputStream out) { exportExcel(title, beans, out, "yyyy-MM-dd"); } /** * 根據集合Bean動態生成Excel */ @SuppressWarnings("unchecked") public void exportExcel(String title, List<Bean> beans, OutputStream out, String pattern) { // 聲明一個工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); // 生成一個表格 HSSFSheet sheet = workbook.createSheet(title); // 設置表格默認列寬度為20個字節 sheet.setDefaultColumnWidth(20); // 標題樣式 HSSFCellStyle style = workbook.createCellStyle(); // 邊框設置 style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); // 字體居中 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成一個字體 HSSFFont font = workbook.createFont(); font.setFontHeightInPoints((short) 16); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 把字體應用到當前的樣式 style.setFont(font); // 樣式 1 HSSFCellStyle style2 = workbook.createCellStyle(); style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); style2.setBorderLeft(HSSFCellStyle.BORDER_THIN); style2.setBorderRight(HSSFCellStyle.BORDER_THIN); style2.setBorderTop(HSSFCellStyle.BORDER_THIN); style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成另一個字體 HSSFFont font2 = workbook.createFont(); font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 把字體應用到當前的樣式 style2.setFont(font2); // 指定當單元格內容顯示不下時自動換行 style2.setWrapText(true); // 樣式2 HSSFCellStyle style3 = workbook.createCellStyle(); style3.setBorderBottom(HSSFCellStyle.BORDER_THIN); style3.setBorderLeft(HSSFCellStyle.BORDER_THIN); style3.setBorderRight(HSSFCellStyle.BORDER_THIN); style3.setBorderTop(HSSFCellStyle.BORDER_THIN); style3.setAlignment(HSSFCellStyle.ALIGN_CENTER); style3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成另一個字體 HSSFFont font3 = workbook.createFont(); font3.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 把字體應用到當前的樣式 style3.setFont(font3); // 指定當單元格內容顯示不下時自動換行 style3.setWrapText(true); // 標題行 sheet.addMergedRegion(new Region(0, (short) (0), 0, (short) 6)); HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell(0); row.setHeight((short) 800); cell.setCellStyle(style); HSSFRichTextString text = new HSSFRichTextString(title); cell.setCellValue(text); // 數據行 int index = 1; int r = 1; int rf = 3; int j = 1; for (Bean b : beans) { sheet.addMergedRegion(new Region(r, (short) (0), rf, (short) 0)); sheet.addMergedRegion(new Region(r, (short) (2), r, (short) 6)); sheet.addMergedRegion(new Region(rf, (short) (2), rf, (short) 6)); r += 3; rf += 3; row = sheet.createRow(index++); row.setHeight((short) 500); sheet.setColumnWidth(0, 30 * 35); for (short i = 0; i < 7; i++) { cell = row.createCell(i); if (i == 1) { cell.setCellStyle(style3); } else { cell.setCellStyle(style2); } if (i == 1) { cell.setCellValue("工作任務"); } else if (i == 2) { cell.setCellValue(b.getStr("WORK_TASK")); } else if (i == 0) { cell.setCellValue(j++); } } row = sheet.createRow(index++); row.setHeight((short) 500); for (short i = 0; i < 7; i++) { cell = row.createCell(i); if (i == 1 || i == 3 || i == 5) { cell.setCellStyle(style3); } else { cell.setCellStyle(style2); } if (i == 1) { cell.setCellValue("主辦部門"); } else if (i == 2) { cell.setCellValue(b.getStr("RESPON_DEPT")); } else if (i == 3) { cell.setCellValue("布置時間"); } else if (i == 4) { cell.setCellValue(b.getStr("FACT_START")); } else if (i == 5) { cell.setCellValue("落實時間"); } else if (i == 6) { cell.setCellValue(b.getStr("FACT_FINISH")); } } row = sheet.createRow(index++); row.setHeight((short) 1000); for (short i = 0; i < 7; i++) { cell = row.createCell(i); if (i == 1) { cell.setCellStyle(style3); } else { cell.setCellStyle(style2); } if (i == 1) { cell.setCellValue("落實情況"); } else if (i == 2) { cell.setCellValue(b.getStr("CARRY_SITUATION")); } } } try { workbook.write(out); } catch (IOException e) { e.printStackTrace(); } } /** * 導出 Excel * @param param * @throws IOException */ public void export(ParamBean param) { HttpServletResponse response = Context.getResponse(); HttpServletRequest request = Context.getRequest(); String paramStr = param.getStr("param"); Bean paramBean = JsonUtils.toBean(paramStr); String dataId = paramBean.getStr("pkCodes"); String dataIdArrayList = "('" + dataId.replaceAll(",", "','") + "')"; String sql = "select t.type, t.WORK_TASK,t.RESPON_DEPT,t.FACT_START,t.FACT_FINISH,t.CARRY_SITUATION ,m.issue from PDC_WORK_TASK t, PDC_WEEK_MEET m WHERE t.WEEK_MEET_ID = m.ID and t.ID in " + dataIdArrayList; // 任務集合 List<Bean> beans = Context.getExecutor().query(sql); String fileName = "";// 文件名稱 String title = "";// 標題 int issue = beans.get(0).getInt("issue");// 期號 if (beans.get(0).getStr("type").equals("1")) { fileName = "第" + issue + "期任務落實情況"; title = "上周例會落實情況"; } else { fileName = title = "其他工作落實情況"; } response.setContentType("application/x-zip-compressed;charset=utf-8"); RequestUtils.setDownFileName(request, response, fileName + ".xls");// 設置文件名稱 javax.servlet.ServletOutputStream outPutStream = null; try { // 工作表對象 ExportExcel<Bean> ex = new ExportExcel<Bean>(); outPutStream = response.getOutputStream(); ex.exportExcel(title, beans, outPutStream); } catch (IOException e) { e.printStackTrace(); } finally { try { outPutStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }