<span style="font-size:14px;">java通用的Excel檔案建立方法,支援同檔案多tab頁建立。只需要調用靜態方法,傳遞List<String>表頭和List<Map>資料集合等,即可產生Excel檔案。package com.matols.utils; import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; import javax.servlet.http.HttpServletResponse; 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 com.google.common.collect.Lists; /** * Excel工具類 */public class ExcelUtils { public static void main(String[] args) throws Throwable { String path = "D:/tj/統計報表.xls"; //表頭 List<String> headers = Lists.newArrayList(); for (int i = 1; i < 10; i++) { headers.add("表頭"+i); } //資料行 List<Map> datas = new ArrayList<Map>(); Map m = null; for(int i=1;i<10;i++){ m = new HashMap(); //一行資料集 for(int j=0;j<headers.size();j++){ m.put(j, "第"+i+" 行資料:"+j); } datas.add(m); } ExpExs(path,"","統計報表",headers,datas); } /* * 通用的Excel檔案建立方法 * title:首列名: 2015年度統計報表 * sheets:sheet的tab標籤頁說明: 15年度報表 * headers:表頭:List存放表頭 編號、姓名、備忘 * datas:資料行:list存放實體資料,map存放具體每一行資料,和headers對應。 * rs:HttpServletResponse響應範圍,如果不為null,會直接將檔案流輸出到用戶端,下載檔案 */ public static void ExpExs(String title,String sheets,List headers,List<Map> datas,HttpServletResponse rs){ try { if(sheets== null || "".equals(sheets)){ sheets = "sheet"; } HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(sheets); //+workbook.getNumberOfSheets() HSSFRow row; HSSFCell cell; // 設定這些樣式 HSSFFont font = workbook.createFont(); font.setFontName(HSSFFont.FONT_ARIAL);//字型 font.setFontHeightInPoints((short) 16);//字型大小 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗 //font.setColor(HSSFColor.BLUE.index);//顏色 HSSFCellStyle cellStyle= workbook.createCellStyle(); //設定儲存格樣式 cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index); cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER ); cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); cellStyle.setFont(font); //產生表格標題列 row = sheet.createRow(0); row.setHeightInPoints(20); for (int i = 0; i < headers.size(); i++) { HSSFRichTextString text = new HSSFRichTextString(headers.get(i).toString()); cell = row.createCell(i); cell.setCellValue(text); cell.setCellStyle(cellStyle); } cellStyle= workbook.createCellStyle(); cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT); cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); cellStyle.setDataFormat((short)0x31);//設定顯示格式,避免點擊後變成科學計數法了 //cellStyle.setWrapText(true);//設定自動換行 Map map; //遍曆集合資料,產生資料行 for (int i=0; i <datas.size(); i++) { row=sheet.createRow((i+1)); row.setHeightInPoints(20); map = datas.get(i); for(int j=0;j<map.size();j++) { cell = row.createCell(j); cell.setCellStyle(cellStyle); cell.setCellType(HSSFCell.CELL_TYPE_STRING); if(map.get(j) != null) { cell.setCellValue(new HSSFRichTextString(map.get(j).toString())); }else{ cell.setCellValue(new HSSFRichTextString("")); } } } for (int i = 0; i < headers.size(); i++) { sheet.autoSizeColumn((short)i); } rs.reset(); rs.setContentType("multipart/form-data"); //自動識別 rs.setHeader("Content-Disposition","attachment;filename=data.xls"); //檔案流輸出到rs裡 workbook.write(rs.getOutputStream()); rs.getOutputStream().flush(); rs.getOutputStream().close(); } catch (Exception e) { System.out.println("#Error ["+e.getMessage()+"] "); } System.out.println("["+sheets+"] 建立成功..."); System.out.println(""); } /* * 通用的Excel檔案建立方法 * path:儲存路徑: C:/xls/統計報表.xls * title:首列名: 2015年度統計報表 * sheets:sheet的tab標籤頁說明: 15年度報表 * headers:表頭:List存放表頭 編號、姓名、備忘 * datas:資料行:list存放實體資料,map存放具體每一行資料,和headers對應。 */ public static void ExpExs(String path,String title,String sheets,List headers,List<Map> datas){ try { if(sheets== null || "".equals(sheets)){ sheets = "sheet"; } boolean isExist = new File(path).exists(); if(!isExist){ HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(sheets); FileOutputStream out = new FileOutputStream(new File(path)); workbook.write(out); out.flush(); out.close(); } FileInputStream file = new FileInputStream(new File(path)); HSSFWorkbook workbook = new HSSFWorkbook(file); HSSFSheet sheet = null; if(!isExist){ sheet = workbook.getSheetAt(0); }else{ if(workbook.getSheet(sheets) == null){ sheet = workbook.createSheet(sheets); //+workbook.getNumberOfSheets() }else{ System.out.println("檔案:["+path+"] ["+sheets+"] 已經存在..."); System.out.println(""); return; } } HSSFRow row; HSSFCell cell; // 設定這些樣式 HSSFFont font = workbook.createFont(); font.setFontName(HSSFFont.FONT_ARIAL);//字型 font.setFontHeightInPoints((short) 16);//字型大小 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗 //font.setColor(HSSFColor.BLUE.index);//顏色 HSSFCellStyle cellStyle= workbook.createCellStyle(); //設定儲存格樣式 cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index); cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER ); cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); cellStyle.setFont(font); //產生表格標題列 row = sheet.createRow(0); row.setHeightInPoints(20); for (int i = 0; i < headers.size(); i++) { HSSFRichTextString text = new HSSFRichTextString(headers.get(i).toString()); cell = row.createCell(i); cell.setCellValue(text); cell.setCellStyle(cellStyle); } cellStyle= workbook.createCellStyle(); cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT); cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); cellStyle.setDataFormat((short)0x31);//設定顯示格式,避免點擊後變成科學計數法了 //cellStyle.setWrapText(true);//設定自動換行 Map map; //遍曆集合資料,產生資料行 for (int i=0; i <datas.size(); i++) { row=sheet.createRow((i+1)); row.setHeightInPoints(20); map = datas.get(i); for(int j=0;j<map.size();j++) { cell = row.createCell(j); cell.setCellStyle(cellStyle); cell.setCellType(HSSFCell.CELL_TYPE_STRING); if(map.get(j) != null) { cell.setCellValue(new HSSFRichTextString(map.get(j).toString())); }else{ cell.setCellValue(new HSSFRichTextString("")); } } } for (int i = 0; i < headers.size(); i++) { sheet.autoSizeColumn((short)i); } FileOutputStream out = new FileOutputStream(new File(path)); workbook.write(out); out.flush(); out.close(); /* HSSFRow row = sheet.createRow(sheets); HSSFCell cell = null; cell=row.createCell(sheets); cell.setCellValue(new HSSFRichTextString("-["+sheets+"]-")); sheets=sheets+2;//中間空一行 row=sheet.createRow(sheets); */ } catch (Exception e) { System.out.println("#Error ["+e.getMessage()+"] "); } System.out.println("檔案:["+path+"] ["+sheets+"] 建立成功..."); System.out.println(""); }}</span>