以前的導出都很亂,直接寫在代碼中,等到下回還使用導出功能時又不知如何下手,今天用模板模式重寫了一個導出功能,方便之後使用:java
package com.sf.addrCheck.util.export.poi; import com.sf.addrCheck.ebo.QbUserAdmin; import com.sf.addrCheck.util.export.ExcelUtil; import org.apache.poi.hssf.usermodel.*; import org.osgi.resource.Requirement; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.*; /** * poi-excel導出模板 */ public abstract class PoiExport { /** * 導出的數據 */ private List list; /** * 標題 */ private String[] title; /** * 文件名 */ private String fileName = "export_" + System.currentTimeMillis()+".xlsx"; /** * 表格名 */ private String sheetName = "sheet1"; public PoiExport(List list, String[] title) { this.list = list; this.title = title; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName + ".xlsx"; } public String getSheetName() { return sheetName; } public void setSheetName(String sheetName) { this.sheetName = sheetName; } private HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb) { // 第一步,建立一個HSSFWorkbook,對應一個Excel文件 if (wb == null) { wb = new HSSFWorkbook(); } // 第二步,在workbook中添加一個sheet,對應Excel文件中的sheet HSSFSheet sheet = wb.createSheet(sheetName); // 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數列數有限制 HSSFRow row = sheet.createRow(0); // 第四步,建立單元格,並設置值表頭 設置表頭居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 建立一個居中格式 // 聲明列對象 HSSFCell cell = null; // 建立標題 for (int i = 0; i < title.length; i++) { cell = row.createCell(i); cell.setCellValue(title[i]); cell.setCellStyle(style); } // 建立內容 for (int i = 0; i < values.length; i++) { row = sheet.createRow(i + 1); for (int j = 0; j < values[i].length; j++) { // 將內容按順序賦給對應的列對象 row.createCell(j).setCellValue(values[i][j]); } } return wb; } /** * 須要重寫的方法:用來填充二元數組dataTable * * <p>這裏主要實現把list中的每一個數據設置到dataTable中,list中Obj的屬性對應dataTable中的列,list的index對應dataTable中的行</p> * * @param dataTable 表明數據表,第一位表示行數,第二位表示列數 * @param i 行數 * @return */ public abstract void fillingData(String[][] dataTable, int i, List list); public void export(HttpServletResponse response) throws Exception { String[][] dataTable = new String[list.size()+1][title.length]; for (int i = 0; i < list.size(); i++) { this.fillingData(dataTable,i, this.list); } //建立HSSFWorkbook HSSFWorkbook wb = this.getHSSFWorkbook(sheetName, title, dataTable, null); //響應到客戶端 try { this.setResponseHeader(response, fileName); OutputStream os = response.getOutputStream(); wb.write(os); os.flush(); os.close(); } catch (Exception e) { e.printStackTrace(); } } //發送響應流方法 private void setResponseHeader(HttpServletResponse response, String fileName) { try { try { fileName = new String(fileName.getBytes(),"ISO8859-1"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } response.setContentType("application/octet-stream;charset=ISO8859-1"); response.setHeader("Content-Disposition", "attachment;filename="+ fileName); response.addHeader("Pargam", "no-cache"); response.addHeader("Cache-Control", "no-cache"); } catch (Exception ex) { ex.printStackTrace(); } } /** * 使用例子 */ public static void main(String[] args) { List<QbUserAdmin> list = new ArrayList<>(); QbUserAdmin qbUserAdmin = new QbUserAdmin(); qbUserAdmin.setLoginName("admin"); qbUserAdmin.setRole("1"); String[] titles = {"loginName", "role"}; new PoiExport(Collections.singletonList(list), titles) { @Override public void fillingData(String[][] content, int i, List list) { QbUserAdmin qbUserAdmin1 = (QbUserAdmin)list.get(i); content[i][0] = qbUserAdmin1.getLoginName(); content[i][1] = qbUserAdmin1.getRole(); } }.export(response); //這裏的main沒有response,須要在web應用中使用 } }