1、异常java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException
解决方法:使用的poi的相关jar包一定版本一定要相同!!!!!
2.引入jar包
<!-- 导出excel -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
3.设置工作簿
//创建工作簿 HSSFWorkbook workbook = new HSSFWorkbook() //添加工作簿名称 HSSFSheet sheet = workbook.createSheet("考勤统计"); //创建单元格 HSSFRow row = sheet.createRow(0); //创建字体 HSSFFont font = workbook.createFont(); font.setFontHeight((short) 200); //创建样式 HSSFCellStyle style = workbook.createCellStyle(); //强制换行 style.setWrapText(true); //添加字体 style.setFont(font); //合并单元格(起始行,结束行,起始列,结束列) CellRangeAddress timeRegion = new CellRangeAddress(1, 2,0,0); sheet.addMergedRegion(timeRegion); //设置单元格内容 timeCell.setCellValue("时间");
4.下载表格
HttpServletResponse response = servletRequestAttributes.getResponse(); try { OutputStream out = response.getOutputStream(); //设置格式 response.setCharacterEncoding("UTF-8"); response.setContentType("application/ms-excel;charset=UTF-8"); //设置请求头 response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(DateTimeUtil.getStartAndLastDayNumber(startDay, endDay) + ".xls", "UTF-8")))); response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); //写入 workbook.write(out); out.close(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }