struts2 利用 POI 导出 excel
2016-04-12 19:16
435 查看
第一种方式:在action中设置header等
1. 根据数据生成 HSSFWorkbook, 并将 HSSFWorkbook 写入到输出流
private void createExcel(List<String[]> data) throws Exception{ HttpServletResponse response = ServletActionContext.getResponse(); //excel 文件的 MIME 类型 response.setContentType("application/vnd.ms-excel"); String fileName = "导出数据";//需要使用<span style="font-family: Arial, Helvetica, sans-serif;">URLEncoder编码一下, 否则中文名字不会显示</span> response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName, "utf-8") +".xls"); HSSFWorkbook wb = new HSSFWorkbook(); if(data == null || data.size() < 1){ return; } String sheetTitle = "sheet1"; HSSFSheet sheet = wb.createSheet(); wb.setSheetName(0,sheetTitle); HSSFRow row=null; HSSFCell cell = null; for(int i=0; i<data.size(); i++){ row = sheet.createRow(i); for(int j=0; j<data.get(i).length; j++){ cell = row.createCell(j); cell.setCellValue(data.get(i)[j]); } } OutputStream os = ServletActionContext.getResponse().getOutputStream(); wb.write(os); if(os != null){ os.flush(); os.close(); } }
2. 在 action 中调用该方法即可
public String export() throws Exception{ oauthInfoService.getPageInfoData(startDate, endDate, clientId, pageInfo, true); List<String[]> data = new ArrayList<String[]>(); List<OauthInfoBean> result = pageInfo.getSearchResult(); String[] rowName = new String[]{"请求来源","授权时间","请求时间","手机号"}; data.add(rowName); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); for(OauthInfoBean bean : result){ rowName = new String[4]; rowName[0] = bean.getReqSource() == null?"":bean.getReqSource(); if(bean.getAuthTime() != null){ rowName[1] = sdf.format(bean.getAuthTime()); }else{ rowName[1] = ""; } rowName[2] = bean.getReqTime()==null?"":bean.getReqTime(); rowName[3] = bean.getPhone()==null?"":bean.getPhone(); data.add(rowName); } this.createExcel(data); return NONE; }
3. struts2的配置文件中的配置方式
<action name="export" class="oauthInfoJsonAction" method="export"> </action>
第二种方式,在struts2的配置文件中配置
代码/** * * @return * @throws Exception */ public String export() throws Exception{ HttpServletResponse response = ServletActionContext.getResponse(); //excel 文件的 MIME 类型 response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=授权信息数据.xls"); exportFileName = "导出数据"; oauthInfoService.getPageInfoData(startDate, endDate, clientId, pageInfo, true); List<String[]> data = new ArrayList<String[]>(); List<OauthInfoBean> result = pageInfo.getSearchResult(); String[] rowName = new String[]{"请求来源","授权时间","请求时间","手机号"}; data.add(rowName); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); for(OauthInfoBean bean : result){ rowName = new String[4]; rowName[0] = bean.getReqSource() == null?"":bean.getReqSource(); if(bean.getAuthTime() != null){ //rowName[1] = sdf.format(bean.getAuthTime()); }else{ rowName[1] = ""; } rowName[2] = bean.getReqTime()==null?"":bean.getReqTime(); rowName[3] = bean.getPhone()==null?"":bean.getPhone(); data.add(rowName); } this.createExcel(data); return SUCCESS; } private void createExcel(List<String[]> data) throws Exception{ HSSFWorkbook wb = new HSSFWorkbook(); if(data == null || data.size() < 1){ return; } String sheetTitle = "sheet1"; HSSFSheet sheet = wb.createSheet(); wb.setSheetName(0,sheetTitle); HSSFRow row=null; HSSFCell cell = null; for(int i=0; i<data.size(); i++){ row = sheet.createRow(i); for(int j=0; j<data.get(i).length; j++){ cell = row.createCell(j); cell.setCellValue(data.get(i)[j]); } } this.exportFileName="导出数据";//设置fileName ByteArrayOutputStream baos = new ByteArrayOutputStream(); wb.write(baos); baos.flush(); byte[]aa=baos.toByteArray(); this.excelFileStream = new ByteArrayInputStream(aa,0,aa.length); baos.close(); }
xml配置文件
<action name="export" class="oauthInfoJsonAction" method="export"> <result type="stream"> <param name="contentType">application/vnd.ms-excel</param> <param name="contentDisposition">attachment;filename=123.xls</param> <param name="bufferSize">1024</param> <param name="inputName">excelFileStream</param> </result> </action>当然在action中必须要有excelFileStream这个属性和set,get方法
相关文章推荐
- 多线程中线程执行器java5
- java中堆和栈的区别
- 分享某公司JAVA实习生招聘代码面的一道题
- 20145238-荆玉茗 《Java程序设计》第7周学习总结
- Spring与Mybatis三种常用整合方法
- Java学习——运算顺序的详细挖掘(下)
- java集合框架04——LinkedList和源码分析
- Java NIO Scatter / Gather
- Java并发编程系列之三十:多线程的代价
- javax.servlet获取
- Qconf配置管理工具-Java使用文档
- Ubuntu14.04 java环境配置
- eclipse_深入理解查找(Search)功能及其扩展点
- Spring Bean的生命周期
- java robots协议检测工具
- java使用IO读写文件总结
- struts2框架校验时布尔表达式无效 警告: Got result of null when trying to get Boolean.
- java RMI入门指南
- java笔记6→java接口(interface)
- 编译andriod源码出错:java.lang.UnsupportedClassVersionError: com/google/doclava/Doclava : Unsupported