springmvc 中使用poi导出excel

整理了一些关于Spring 中如何用poi导出excel的文章,如下:


导出excel 工具类的写法

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.fuiou.MyProject.unit;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.OutputStream;  
  5. import java.net.URLEncoder;  
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8.   
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. import org.apache.poi.hssf.usermodel.HSSFCell;  
  12. import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
  13. import org.apache.poi.hssf.usermodel.HSSFFont;  
  14. import org.apache.poi.hssf.usermodel.HSSFRichTextString;  
  15. import org.apache.poi.hssf.usermodel.HSSFRow;  
  16. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  17. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  18. import org.apache.poi.hssf.util.HSSFColor;  
  19. import org.apache.poi.ss.util.CellRangeAddress;  
  20.   
  21. public class ExportExcelUtils {  
  22.       
  23.     private String title; // 导出表格的表名  
  24.       
  25.     private String[] rowName;// 导出表格的列名  
  26.       
  27.     private List<Object[]>  dataList = new ArrayList<Object[]>(); // 对象数组的List集合  
  28.       
  29.     private HttpServletResponse  response;  
  30.       
  31.   
  32.     // 传入要导入的数据  
  33.     public ExportExcelUtils(String title,String[] rowName,List<Object[]>  dataList,HttpServletResponse  response){  
  34.         this.title=title;  
  35.         this.rowName=rowName;  
  36.         this.dataList=dataList;  
  37.         this.response = response;  
  38.     }  
  39.       
  40.     // 导出数据  
  41.     public void exportData(){  
  42.         try {  
  43.             HSSFWorkbook workbook =new HSSFWorkbook(); // 创建一个excel对象  
  44.             HSSFSheet sheet =workbook.createSheet(title); // 创建表格  
  45.             // 产生表格标题行  
  46.             HSSFRow rowm  =sheet.createRow(0);  // 行  
  47.             HSSFCell cellTiltle =rowm.createCell(0);  // 单元格  
  48.               
  49.             // sheet样式定义  
  50.             HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook); // 头样式  
  51.             HSSFCellStyle style = this.getStyle(workbook);  // 单元格样式  
  52.             /**  
  53.              * 参数说明  
  54.              * 从0开始   第一行 第一列 都是从角标0开始  
  55.              * 行 列 行列    (0,0,0,5)  合并第一行 第一列  到第一行 第六列  
  56.              * 起始行,起始列,结束行,结束列  
  57.              *   
  58.              * new Region()  这个方法使过时的  
  59.              */  
  60.             // 合并第一行的所有列  
  61.             sheet.addMergedRegion(new CellRangeAddress(0, (short) 0, 0, (short) (rowName.length-1)));  
  62.             cellTiltle.setCellStyle(columnTopStyle);  
  63.             cellTiltle.setCellValue(title);   
  64.               
  65.             int columnNum = rowName.length;  // 表格列的长度  
  66.             HSSFRow rowRowName = sheet.createRow(1);  // 在第二行创建行  
  67.             HSSFCellStyle cells =workbook.createCellStyle();  
  68.             cells.setBottomBorderColor(HSSFColor.BLACK.index);    
  69.             rowRowName.setRowStyle(cells);  
  70.               
  71.             // 循环 将列名放进去  
  72.             for (int i = 0; i < columnNum; i++) {  
  73.                 HSSFCell  cellRowName = rowRowName.createCell((int)i);  
  74.                 cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); // 单元格类型  
  75.                   
  76.                 HSSFRichTextString text = new HSSFRichTextString(rowName[i]);  // 得到列的值  
  77.                 cellRowName.setCellValue(text); // 设置列的值  
  78.                 cellRowName.setCellStyle(columnTopStyle); // 样式  
  79.             }  
  80.               
  81.             // 将查询到的数据设置到对应的单元格中  
  82.             for (int i = 0; i < dataList.size(); i++) {  
  83.                 Object[] obj = dataList.get(i);//遍历每个对象  
  84.                 HSSFRow row = sheet.createRow(i+2);//创建所需的行数  
  85.                 for (int j = 0; j < obj.length; j++) {  
  86.                      HSSFCell  cell = null;   //设置单元格的数据类型   
  87.                      if(j==0){  
  88.                          // 第一列设置为序号  
  89.                          cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC);  
  90.                          cell.setCellValue(i+1);  
  91.                      }else{  
  92.                          cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);  
  93.                          if(!"".equals(obj[j]) && obj[j] != null){    
  94.                                 cell.setCellValue(obj[j].toString());                       //设置单元格的值    
  95.                             }else{  
  96.                                 cell.setCellValue("  ");  
  97.                             }    
  98.                      }  
  99.                      cell.setCellStyle(style); // 样式  
  100.                 }  
  101.             }  
  102.             //  让列宽随着导出的列长自动适应  
  103.              sheet.autoSizeColumn((short)0); //调整第一列宽度  
  104.              sheet.autoSizeColumn((short)1); //调整第二列宽度  
  105.              sheet.autoSizeColumn((short)2); //调整第三列宽度  
  106.              sheet.autoSizeColumn((short)3); //调整第四列宽度  
  107.              sheet.autoSizeColumn((short)4); //调整第五列宽度  
  108.              sheet.autoSizeColumn((short)5); //调整第六列宽度  
  109.                
  110.              if(workbook !=null){    
  111.                     try    
  112.                     {    
  113.                         // excel 表文件名  
  114.                         String fileName = title + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";    
  115.                         String fileName11 = URLEncoder.encode(fileName,"UTF-8");  
  116.                         String headStr = "attachment; filename=\"" + fileName11 + "\"";    
  117.                         response.setContentType("APPLICATION/OCTET-STREAM");    
  118.                         response.setHeader("Content-Disposition", headStr);    
  119.                         OutputStream out = response.getOutputStream();    
  120.                         workbook.write(out);  
  121.                         out.flush();  
  122.                         out.close();  
  123.                     }    
  124.                     catch (IOException e)    
  125.                     {    
  126.                         e.printStackTrace();    
  127.                     }   
  128.                       
  129.                 }    
  130.         
  131.             }catch(Exception e){    
  132.                 e.printStackTrace();    
  133.             }    
  134.                 
  135.         }    
  136.               
  137.     public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {    
  138.           
  139.         // 设置字体    
  140.         HSSFFont font = workbook.createFont();    
  141.         //设置字体大小    
  142.         font.setFontHeightInPoints((short)11);    
  143.         //字体加粗    
  144.         font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);    
  145.         //设置字体名字     
  146.         font.setFontName("Courier New");    
  147.         //设置样式;     
  148.         HSSFCellStyle style = workbook.createCellStyle();    
  149.         //设置底边框;     
  150.         style.setBorderBottom(HSSFCellStyle.BORDER_THIN);    
  151.         //设置底边框颜色;      
  152.         style.setBottomBorderColor(HSSFColor.BLACK.index);    
  153.         //设置左边框;       
  154.         style.setBorderLeft(HSSFCellStyle.BORDER_THIN);    
  155.         //设置左边框颜色;     
  156.         style.setLeftBorderColor(HSSFColor.BLACK.index);    
  157.         //设置右边框;     
  158.         style.setBorderRight(HSSFCellStyle.BORDER_THIN);    
  159.         //设置右边框颜色;     
  160.         style.setRightBorderColor(HSSFColor.BLACK.index);    
  161.         //设置顶边框;     
  162.         style.setBorderTop(HSSFCellStyle.BORDER_THIN);    
  163.         //设置顶边框颜色;      
  164.         style.setTopBorderColor(HSSFColor.BLACK.index);    
  165.         //在样式用应用设置的字体;      
  166.         style.setFont(font);    
  167.         //设置自动换行;     
  168.         style.setWrapText(false);    
  169.         //设置水平对齐的样式为居中对齐;      
  170.         style.setAlignment(HSSFCellStyle.ALIGN_CENTER);    
  171.         //设置垂直对齐的样式为居中对齐;     
  172.         style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);    
  173.             
  174.         return style;    
  175.             
  176.   }    
  177.       
  178.     public HSSFCellStyle getStyle(HSSFWorkbook workbook) {    
  179.         // 设置字体    
  180.         HSSFFont font = workbook.createFont();    
  181.         //设置字体大小    
  182.         //font.setFontHeightInPoints((short)10);    
  183.         //字体加粗    
  184.         //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);    
  185.         //设置字体名字     
  186.         font.setFontName("Courier New");    
  187.         //设置样式;     
  188.         HSSFCellStyle style = workbook.createCellStyle();    
  189.         //设置底边框;     
  190.         style.setBorderBottom(HSSFCellStyle.BORDER_THIN);    
  191.         //设置底边框颜色;      
  192.         style.setBottomBorderColor(HSSFColor.BLACK.index);    
  193.         //设置左边框;       
  194.         style.setBorderLeft(HSSFCellStyle.BORDER_THIN);    
  195.         //设置左边框颜色;     
  196.         style.setLeftBorderColor(HSSFColor.BLACK.index);    
  197.         //设置右边框;     
  198.         style.setBorderRight(HSSFCellStyle.BORDER_THIN);    
  199.         //设置右边框颜色;     
  200.         style.setRightBorderColor(HSSFColor.BLACK.index);    
  201.         //设置顶边框;     
  202.         style.setBorderTop(HSSFCellStyle.BORDER_THIN);    
  203.         //设置顶边框颜色;      
  204.         style.setTopBorderColor(HSSFColor.BLACK.index);    
  205.         //在样式用应用设置的字体;      
  206.         style.setFont(font);    
  207.         //设置自动换行;     
  208.         style.setWrapText(false);    
  209.         //设置水平对齐的样式为居中对齐;      
  210.         style.setAlignment(HSSFCellStyle.ALIGN_CENTER);    
  211.         //设置垂直对齐的样式为居中对齐;     
  212.         style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);    
  213.            
  214.         return style;    
  215.   }    
  216. }    
  217.       
  218.       

jsp 页面

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <td><a href="${pageContext.request.contextPath}/user/exportExcel.action">导出数据</a></td>  

controller 层中的代码

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @RequestMapping(value ="/exportExcel.action")  
  2.     public ModelAndView exportExcel(HttpServletRequest request,HttpServletResponse response){  
  3.         try {  
  4.             UserInfoPO user =new UserInfoPO();  
  5.             // 查出用户数据  
  6.             List<UserInfoPO> userlist = userInfoService.queryList(user);  
  7.             String title ="用户信息表";  
  8.             String[] rowsName=new String[]{"序号","ID","用户名","性别","登录id","登录密码"};  
  9.             List<Object[]>  dataList = new ArrayList<Object[]>();  
  10.             Object[] objs = null;  
  11.             for (int i = 0; i < userlist.size(); i++) {  
  12.                 UserInfoPO po =userlist.get(i);  
  13.                 objs = new Object[rowsName.length];  
  14.                 objs[0] = i;  
  15.                 objs[1] = po.getId();  
  16.                 objs[2] = po.getUserName();  
  17.                 objs[3] = po.getSex();  
  18.                 objs[4] = po.getLoginId();  
  19.                 objs[5] = po.getLoginPassword();  
  20.                 dataList.add(objs);  
  21.             }  
  22.             //   
  23.             ExportExcelUtils ex =new ExportExcelUtils(title, rowsName, dataList,response);  
  24.             ex.exportData();  
  25.               
  26.               
  27.         } catch (Exception e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.           
  31.         return null;  
  32.     }   


原文地址         http://blog.csdn .NET /wangchangpen62/article/details/44410967




原文地址         http://blog.csdn .NET /wangchangpen62/article/details/44410967
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值