參數解釋: title:導出excel標題、headers 導出到excel顯示的列頭、
columns 對應數據庫字段 、list 導出數據
1、pox中添加依賴
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15-beta2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
2、添加工具類
public void expoortExcelx(String title, String[] headers, String[] columns, List<T> list, OutputStream out, String pattern) throws NoSuchMethodException, Exception{ //創建工作薄 XSSFWorkbook workbook=new XSSFWorkbook(); //創建表格 Sheet sheet=workbook.createSheet(title); //設置默認寬度 sheet.setDefaultColumnWidth(25); //創建樣式 XSSFCellStyle style=workbook.createCellStyle(); //設置樣式 style.setFillForegroundColor(IndexedColors.GOLD.index); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBorderLeft(CellStyle.BORDER_THIN); style.setBorderRight(CellStyle.BORDER_THIN); style.setBorderTop(CellStyle.BORDER_THIN); //生成字體 XSSFFont font=workbook.createFont(); font.setColor(IndexedColors.VIOLET.index); font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); //應用字體 style.setFont(font); //自動換行 style.setWrapText(true); //聲明一個畫圖的頂級管理器 Drawing drawing=(XSSFDrawing) sheet.createDrawingPatriarch(); //表頭的樣式 XSSFCellStyle titleStyle=workbook.createCellStyle();//樣式對象 titleStyle.setAlignment(CellStyle.ALIGN_CENTER_SELECTION);//水平居中 titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //設置字體 XSSFFont titleFont=workbook.createFont(); titleFont.setFontHeightInPoints((short)15); titleFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);//粗體 titleStyle.setFont(titleFont); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headers.length-1)); //指定合並區域 Row rowHeader = sheet.createRow(0); //XSSFRow rowHeader=sheet.createRow(0); Cell cellHeader=rowHeader.createCell(0); XSSFRichTextString textHeader=new XSSFRichTextString(title); cellHeader.setCellStyle(titleStyle); cellHeader.setCellValue(textHeader); Row row=sheet.createRow(1); for(int i=0;i<headers.length;i++){ Cell cell=row.createCell(i); cell.setCellStyle(style); XSSFRichTextString text=new XSSFRichTextString(headers[i]); cell.setCellValue(text); } //遍歷集合數據,產生數據行 if(list!=null&&list.size()>0){ int index=2; for(T t:list){ row=sheet.createRow(index); index++; for(short i=0;i<columns.length;i++){ Cell cell=row.createCell(i); String filedName=columns[i]; String getMethodName="get"+filedName.substring(0,1).toUpperCase() +filedName.substring(1); Class tCls=t.getClass(); Method getMethod=tCls.getMethod(getMethodName,new Class[]{}); Object value=getMethod.invoke(t, new Class[]{}); String textValue=null; if(value==null){ textValue=""; }else if(value instanceof Date){ Date date=(Date)value; SimpleDateFormat sdf = new SimpleDateFormat(pattern); textValue = sdf.format(date); }else if(value instanceof byte[]){ row.setHeightInPoints(80); sheet.setColumnWidth(i, 35*100); byte[] bsValue=(byte[])value; XSSFClientAnchor anchor=new XSSFClientAnchor(0,0,1023,255,6,index,6,index); anchor.setAnchorType(2); drawing.createPicture(anchor, workbook.addPicture(bsValue, XSSFWorkbook.PICTURE_TYPE_JPEG)); }else{ // 其它數據類型都當作字符串簡單處理 textValue=value.toString(); } if(textValue!=null){ Pattern p = Pattern.compile("^//d+(//.//d+)?$"); Matcher matcher = p.matcher(textValue); if (matcher.matches()) { // 是數字當作double處理 cell.setCellValue(Double.parseDouble(textValue)); } else { XSSFRichTextString richString = new XSSFRichTextString( textValue); // HSSFFont font3 = workbook.createFont(); // font3.setColor(HSSFColor.BLUE.index); // richString.applyFont(font3); cell.setCellValue(richString); } } } } } workbook.write(out); }
3、
response.setHeader("Content-Disposition", "attachment; filename="+DateUtil.DateToString()+".xlsx");
OutputStream out =response.getOutputStream();
ExportExcelUtil.expoortExcelx("表頭",headers,clouns,list,out,"yyyy-MM-dd HH:mm:ss");