特色 列出List中要導入到Excel的列名#中文名,而後就完事了。具體的看看main()你就知道有多簡單了。java
利用POI,本工具類能夠實現Excel導出和下載,話很少說,放碼過來。 git
package com.fantong.ois.wms.utils; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.util.HSSFColor; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 導出或下載Excel, * Title: ExportXls<br> * Description: 用#分割列與轉義標題 ;標題包含*表示須要過濾該列<br> * @author xiaour@github.com * @createDate 2016年5月31日 * @version v1.0 */ public class ExportXls { private OutputStream out;//傳入OutputStream表示寫入到服務器,傳入HttpServletResponse則直接下載 private static String tag="#";//標題與字段分割符 private static String hidden="*";//隱藏標識符 public ExportXls(OutputStream out) { super(); this.out = out; } /** * *@category *@throws IOException *@createDate 2016年5月31日 * @param sheetName * @param titles 字段名#中文列名 * @param list * */ @SuppressWarnings({"deprecation" }) public void exportExcel(String sheetName, String[] titles, List<Map<String, Object>> list) throws IOException { // 聲明一個工做薄 @SuppressWarnings("resource") HSSFWorkbook workbook = new HSSFWorkbook(); // 生成一個表格 HSSFSheet sheet = workbook.createSheet(sheetName); // 設置表格默認列寬度爲15個字節 sheet.setDefaultColumnWidth((short) 20); // 生成一個樣式 HSSFCellStyle style = workbook.createCellStyle(); // 設置這些樣式 style.setFillForegroundColor(HSSFColor.TEAL.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 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); // 生成一個字體 HSSFFont font = workbook.createFont(); font.setColor(HSSFColor.WHITE.index); font.setFontHeightInPoints((short) 14); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 把字體應用到當前的樣式 style.setFont(font); //產生表格標題行 HSSFRow row = sheet.createRow(0); sheet.createFreezePane( 0, 1, 0, 1 ); for (int i = 0; i < titles.length; i++) { if(titles[i].indexOf(hidden)<0){ HSSFCell cell = row.createCell(i); cell.setCellStyle(style); HSSFRichTextString text=null; if(titles[i].indexOf(tag)>0){ text = new HSSFRichTextString(titles[i].split(tag)[1]); }else { text = new HSSFRichTextString(titles[i]); } cell.setCellValue(text); } } String value=""; for(int j=1;j<list.size()+1;j++){ Map<String, Object> map=list.get(j-1); row = sheet.createRow(j); for(int s=0;s<titles.length;s++){ if(titles[s].indexOf(hidden)<0){ HSSFCell txtCell = row.createCell(s); if(map!=null){ Object obj=null; if(titles[s].indexOf(tag)>0){ obj=map.get(titles[s].split(tag)[0]); }else { obj=map.get(titles[s]); } String temp=obj!=null?obj.toString():""; value= temp.toString(); } HSSFRichTextString text2 = new HSSFRichTextString(value); txtCell.setCellValue(text2); } } } workbook.write(out); out.flush(); out.close(); } public static void main(String[] args) throws Exception { //寫入本地 OutputStream out = new FileOutputStream("d://1432026362899.xls"); ExportXls ex = new ExportXls(out); String [] headers={"A#標題1","B#標題2","C#標題3*"}; List<Map<String,Object>> dataList= new ArrayList<Map<String,Object>>(); for(int j=0;j<headers.length;j++){ Map<String,Object> map= new HashMap<String,Object>(); map.put("A",j+1); map.put("B",new BigDecimal("580.63")); map.put("C",null); dataList.add(map); } ex.exportExcel("TEST",headers,dataList); out.close(); System.out.println("excel導出成功!"); } }
上面有測試類能夠直接導出到本地,下面看看怎麼直接打開鏈接就下載吧!github
@ResponseBody @RequestMapping(value = "/download", method = { RequestMethod.GET, RequestMethod.POST }) public void download(HttpServletResponse response){ try { response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition", "attachment; filename='xiaour@github.com.xls'"); response.setCharacterEncoding("utf-8"); //能夠將output輸出到服務器 OutputStream outputStream=response.getOutputStream(); ExportXls ex = new ExportXls(outputStream); String [] headers={"id#編號","name#名稱","operation_time#最近更新時間"}; ex.exportExcel("報表",headers,dataList); } } catch (Exception e) { logger.error(e); } }