JavaWeb中如何导出Excel

如何在JavaWeb中导出Excel

代码自解释

1,依赖包

[html]  view plain  copy
  1. <dependency>  
  2.     <groupId>org.apache.poi</groupId>  
  3.     <artifactId>poi</artifactId>  
  4.     <version>3.16</version>  
  5. </dependency>  


2,编写Controller ,参考 点击打开链接

[java]  view plain  copy
  1. @RequestMapping("/export")  
  2.     public void export(HttpServletRequest request, HttpServletResponse response) {  
  3.         // @See http://www.cnblogs.com/dingjiaoyang/p/5831049.html  
  4.         response.setContentType("application/vnd.ms-excel");  
  5.         response.setHeader("Content-disposition""attachment;filename=myExcel.xls");  
  6.         OutputStream ouputStream = null;  
  7.         HSSFWorkbook wb = exportData();  
  8.         try {  
  9.             ouputStream = response.getOutputStream();  
  10.             wb.write(ouputStream);  
  11.         } catch (Exception e) {  
  12.             throw new RuntimeException("系统异常");  
  13.         } finally {  
  14.             try {  
  15.                 ouputStream.flush();  
  16.                 ouputStream.close();  
  17.             } catch (Exception e) {  
  18.                 throw new RuntimeException("系统异常");  
  19.             }  
  20.         }  
  21.     }  

3,Excel文件数据

[java]  view plain  copy
  1. private HSSFWorkbook exportData() {  
  2.         // 创建工作空间  
  3.         HSSFWorkbook wb = new HSSFWorkbook();  
  4.         // 创建表  
  5.         HSSFSheet sheet = wb.createSheet("mySheet");  
  6.         sheet.setDefaultColumnWidth(20);  
  7.         sheet.setDefaultRowHeightInPoints(20);  
  8.   
  9.         // 创建行  
  10.         HSSFRow row = sheet.createRow((int0);  
  11.   
  12.         // 生成一个样式  
  13.         HSSFCellStyle style = wb.createCellStyle();  
  14.         style.setAlignment(HorizontalAlignment.CENTER);// 水平居中  
  15.         style.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中  
  16.   
  17.         // 背景色  
  18.         style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.YELLOW.getIndex());  
  19.         style.setFillPattern(FillPatternType.SOLID_FOREGROUND);  
  20.         style.setFillBackgroundColor(HSSFColor.HSSFColorPredefined.YELLOW.getIndex());  
  21.   
  22.         // 设置边框  
  23.         style.setBorderBottom(BorderStyle.THIN);  
  24.         style.setBorderLeft(BorderStyle.THIN);  
  25.         style.setBorderRight(BorderStyle.THIN);  
  26.         style.setBorderTop(BorderStyle.THIN);  
  27.   
  28.         // 生成一个字体  
  29.         HSSFFont font = wb.createFont();  
  30.         font.setFontHeightInPoints((short10);  
  31.         font.setColor(HSSFColor.HSSFColorPredefined.RED.getIndex());  
  32.         font.setBold(true);  
  33.         font.setFontName("宋体");  
  34.   
  35.         // 把字体 应用到当前样式  
  36.         style.setFont(font);  
  37.           
  38.         // 添加表头数据  
  39.         String[] excelHeader = { "row1""row2""row3" };  
  40.         for (int i = 0; i < excelHeader.length; i++) {  
  41.             HSSFCell cell = row.createCell(i);  
  42.             cell.setCellValue(excelHeader[i]);  
  43.             cell.setCellStyle(style);  
  44.         }  
  45.           
  46.         // 添加单元格数据  
  47.         for (int i = 0; i < 3; i++) {  
  48.             row = sheet.createRow(i + 1);  
  49.             row.createCell(0).setCellValue("line" + i + ":row" + 1);  
  50.             row.createCell(1).setCellValue("line" + i + ":row" + 2);  
  51.             row.createCell(2).setCellValue("line" + i + ":row" + 3);  
  52.         }  
  53.         return wb;  
  54.     }  



转自:https://blog.csdn.net/tidu2chengfo/article/details/75577112

猜你喜欢

转载自blog.csdn.net/zhufengyan521521/article/details/80262125