前几天因为项目需求,要在几个模块加导出excle方法,大概在网上搜索了一下,大多不太符合实际情况,于是就自己写了一个通用的导出工具类.
使用方法之前呢,需要注意几点:
1.为了方便打印取值,实体类toString方法中'='全部改为了','
2.在数据库中 数据库字段需要写全注释 不然表头全为空
那么开始贴方法吧:
方法实现:
public <T extends AbstractDO> void exportToExcle(HttpServletResponse resp, List<T> DataList, String tablename) throws UnsupportedEncodingException, IOException, SQLException, IllegalArgumentException, IllegalAccessException { resp.setContentType("application/vnd.ms-excel"); resp.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("导出的工作簿", "utf-8") + new SimpleDateFormat("yyyy-MM-dd HH_mm_ss").format(new Date()) + ".xls"); ServletOutputStream out = resp.getOutputStream(); HSSFWorkbook newsheet = new HSSFWorkbook(); // 建立第一个sheet HSSFCellStyle columnTopStyle = this.getColumnTopStyle(newsheet);// 获取列头样式对象 HSSFCellStyle style = this.getStyle(newsheet); // 单元格样式对象 // 建立第一个sheet HSSFSheet sheet = newsheet.createSheet("sheet1"); sheet.setDefaultColumnWidth(20); HSSFRow row = sheet.createRow(0); // 获取表头 List<String> tableTittleList = UtilMapper.getColumnCommentByTableName(tablename); for (int i = 0; i < tableTittleList.size(); i++) { row.createCell(i).setCellStyle(columnTopStyle); row.createCell(i).setCellValue(tableTittleList.get(i)); } // 获取表数据 for (int i = 0; i < DataList.size(); i++) { T tempEntity = DataList.get(i); row = sheet.createRow(i + 1); for (int j = 0; j < tableTittleList.size(); j++) { row.createCell(j).setCellStyle(style); row.createCell(j).setCellValue(tempEntity.toString().split(",")[j*2+1]==null?"":tempEntity.toString().split(",")[j*2+1]); } } newsheet.write(out); out.flush(); out.close(); } /* * 列头单元格样式 */ 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); // 设置单元格背景颜色 style.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex()); 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; }
Mapper.java:
List<String> getColumnCommentByTableName(@Param("tb") String tablename);
mapper.xml
<!-- 查询表注释 --> <select id="getColumnCommentByTableName" resultType="String" parameterType="String"> <![CDATA[ select column_comment from INFORMATION_SCHEMA.Columns where table_name=#{tb}]]> </select>
好了至此就可以愉悦的调用
exportToExcle(resp, Datalist, "tablename"); //HttpServletResponse resp //Datalist 需要打印的list //"tablename"表名咯
就好啦;