java spring maven excel 匯出
阿新 • • 發佈:2018-12-07
html
<a href="/visitor/export">匯出excel</a>
controller
/** * excel 匯出 */ @RequestMapping(value ="/export") public ModelAndView exportExcel(HttpServletRequest request, HttpServletResponse response){ try { // 查出使用者資料 List<Map<String, Object>> vistors = visitorService.selectMaps(null); String title ="訪客管理"; String[] rowsName=new String[]{"ID","園區id","訪客姓名","訪客電話","預約時間"}; List<Object[]> dataList = new ArrayList<Object[]>(); Object[] objs = null; for (int i = 0; i < vistors.size(); i++) { Map<String, Object> vistor = vistors.get(i); objs = new Object[rowsName.length]; objs[0] = vistor.get("id"); objs[1] = vistor.get("parkId"); objs[2] = vistor.get("visitorName"); objs[3] = vistor.get("visitorMobile"); objs[4] = vistor.get("appointTime"); dataList.add(objs); } // ExportExcelUtils ex =new ExportExcelUtils(title, rowsName, dataList,response); ex.exportData(); } catch (Exception e) { e.printStackTrace(); } return null; }
utils
package com.stylefeng.guns.modular.utils; import java.io.IOException; import java.io.OutputStream; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; 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 org.apache.poi.ss.util.CellRangeAddress; public class ExportExcelUtils { private String title; // 匯出表格的表名 private String[] rowName;// 匯出表格的列名 private List<Object[]> dataList = new ArrayList<Object[]>(); // 物件陣列的List集合 private HttpServletResponse response; // 傳入要匯入的資料 public ExportExcelUtils(String title,String[] rowName,List<Object[]> dataList,HttpServletResponse response){ this.title=title; this.rowName=rowName; this.dataList=dataList; this.response = response; } // 匯出資料 public void exportData(){ try { HSSFWorkbook workbook =new HSSFWorkbook(); // 建立一個excel物件 HSSFSheet sheet =workbook.createSheet(title); // 建立表格 // 產生表格標題行 HSSFRow rowm =sheet.createRow(0); // 行 HSSFCell cellTiltle =rowm.createCell(0); // 單元格 // sheet樣式定義 HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook); // 頭樣式 HSSFCellStyle style = this.getStyle(workbook); // 單元格樣式 /** * 引數說明 * 從0開始 第一行 第一列 都是從角標0開始 * 行 列 行列 (0,0,0,5) 合併第一行 第一列 到第一行 第六列 * 起始行,起始列,結束行,結束列 * * new Region() 這個方法使過時的 */ // 合併第一行的所有列 sheet.addMergedRegion(new CellRangeAddress(0, (short) 0, 0, (short) (rowName.length-1))); cellTiltle.setCellStyle(columnTopStyle); cellTiltle.setCellValue(title); int columnNum = rowName.length; // 表格列的長度 HSSFRow rowRowName = sheet.createRow(1); // 在第二行建立行 HSSFCellStyle cells =workbook.createCellStyle(); cells.setBottomBorderColor(HSSFColor.BLACK.index); rowRowName.setRowStyle(cells); // 迴圈 將列名放進去 for (int i = 0; i < columnNum; i++) { HSSFCell cellRowName = rowRowName.createCell((int)i); cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); // 單元格型別 HSSFRichTextString text = new HSSFRichTextString(rowName[i]); // 得到列的值 cellRowName.setCellValue(text); // 設定列的值 cellRowName.setCellStyle(columnTopStyle); // 樣式 } // 將查詢到的資料設定到對應的單元格中 for (int i = 0; i < dataList.size(); i++) { Object[] obj = dataList.get(i);//遍歷每個物件 HSSFRow row = sheet.createRow(i+2);//建立所需的行數 for (int j = 0; j < obj.length; j++) { HSSFCell cell = null; //設定單元格的資料型別 if(j==0){ // 第一列設定為序號 cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(i+1); }else{ cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING); if(!"".equals(obj[j]) && obj[j] != null){ cell.setCellValue(obj[j].toString()); //設定單元格的值 }else{ cell.setCellValue(" "); } } cell.setCellStyle(style); // 樣式 } } // 讓列寬隨著匯出的列長自動適應 sheet.autoSizeColumn((short)0); //調整第一列寬度 sheet.autoSizeColumn((short)1); //調整第二列寬度 sheet.autoSizeColumn((short)2); //調整第三列寬度 sheet.autoSizeColumn((short)3); //調整第四列寬度 sheet.autoSizeColumn((short)4); //調整第五列寬度 sheet.autoSizeColumn((short)5); //調整第六列寬度 if(workbook !=null){ try { // excel 表文件名 String fileName = title + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls"; String fileName11 = URLEncoder.encode(fileName,"UTF-8"); String headStr = "attachment; filename=\"" + fileName11 + "\""; response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition", headStr); OutputStream out = response.getOutputStream(); workbook.write(out); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }catch(Exception e){ e.printStackTrace(); } } public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) { // 設定字型 HSSFFont font = workbook.createFont(); //設定字型大小 font.setFontHeightInPoints((short)11); //字型加粗 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //設定字型名字 font.setFontName("Courier New"); //設定樣式; HSSFCellStyle style = workbook.createCellStyle(); //設定底邊框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //設定底邊框顏色; style.setBottomBorderColor(HSSFColor.BLACK.index); //設定左邊框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //設定左邊框顏色; style.setLeftBorderColor(HSSFColor.BLACK.index); //設定右邊框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); //設定右邊框顏色; style.setRightBorderColor(HSSFColor.BLACK.index); //設定頂邊框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); //設定頂邊框顏色; style.setTopBorderColor(HSSFColor.BLACK.index); //在樣式用應用設定的字型; style.setFont(font); //設定自動換行; style.setWrapText(false); //設定水平對齊的樣式為居中對齊; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //設定垂直對齊的樣式為居中對齊; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } public HSSFCellStyle getStyle(HSSFWorkbook workbook) { // 設定字型 HSSFFont font = workbook.createFont(); //設定字型大小 //font.setFontHeightInPoints((short)10); //字型加粗 //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //設定字型名字 font.setFontName("Courier New"); //設定樣式; HSSFCellStyle style = workbook.createCellStyle(); //設定底邊框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //設定底邊框顏色; style.setBottomBorderColor(HSSFColor.BLACK.index); //設定左邊框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //設定左邊框顏色; style.setLeftBorderColor(HSSFColor.BLACK.index); //設定右邊框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); //設定右邊框顏色; style.setRightBorderColor(HSSFColor.BLACK.index); //設定頂邊框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); //設定頂邊框顏色; style.setTopBorderColor(HSSFColor.BLACK.index); //在樣式用應用設定的字型; style.setFont(font); //設定自動換行; style.setWrapText(false); //設定水平對齊的樣式為居中對齊; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //設定垂直對齊的樣式為居中對齊; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } }
參考網站 : https://blog.csdn.net/qq_23191147/article/details/50588581