Java XSSF 导出excel 工具类

参数解释: 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");


 

posted @ 2017-03-17 11:28  咸蛋超人、  阅读(10113)  评论(0编辑  收藏  举报