使用struts2和poi导出excel文档
2023-09-14 09:07:58 时间
poi眼下应该是比較流行的操作excel的工具了。这几天做了个struts2和poi结合使用来实现导出excel的功能。个人认为还是比較有用的。代码阅读起来也非常easy。下来就来分享下我的心得
1 struts2的下载excel文件机制
struts2的action中使用poi和输入输出流把二进制数据通过流的形式响应给client。client浏览器作出响应的处理,如弹出文件下载对话框
2 poi的用法
poi解析或生成excel网上资料特别多,这里我就不给出代码了
3 struts文件的相关配置
<result name="export" type="stream"> <param name="contentType">application/xls;charset=UTF-8</param> <param name="contentDisposition">attachment;filename="${downloadFileName}"</param> <param name="inputName">excelFile</param> </result>
downloadFileName是下载文件的名称,能够使用固定的文件名称也能够使用动态的;excelFile是InputStream的属性名称
4 action的主要内容
(1)相关属性的getter和setter方法
private InputStream excelFile; private String downloadFileName=String.valueOf(Calendar.getInstance().getTimeInMillis())+".xls"; public InputStream getExcelFile() { return excelFile; } public void setExcelFile(InputStream excelFile) { this.excelFile = excelFile; } public String getDownloadFileName() { return downloadFileName; } public void setDownloadFileName(String downloadFileName) { this.downloadFileName = downloadFileName; }
(2)导出excel的主要代码
public void ExcelFile(String startCardNum,String num) throws Exception { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(); HSSFRow row = sheet.createRow(0); //单元格和工作薄名称都要设置下编码。否则有中文的时候就会出现乱码 workbook.setSheetName(0, "卡号信息表" , HSSFWorkbook.ENCODING_UTF_16); HSSFCell cell1=row.createCell((short)0); cell1.setEncoding(HSSFWorkbook.ENCODING_UTF_16); cell1.setCellValue("卡号"); HSSFCell cell2=row.createCell((short)1); cell2.setEncoding(HSSFWorkbook.ENCODING_UTF_16); cell2.setCellValue("password"); HSSFCell cell3=row.createCell((short)2); cell3.setEncoding(HSSFWorkbook.ENCODING_UTF_16); cell3.setCellValue("创建时间"); //取得符合条件的卡号信息 List<Card> all=cardService.queryCardByCondition(startCardNum, num); Iterator<Card> it=all.iterator(); int j=1; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); while(it.hasNext()){ row=sheet.createRow(j); Card c=it.next(); row.createCell((short)0).setCellValue(c.getCardNum()); row.createCell((short)1).setCellValue(c.getPassword()); row.createCell((short)2).setCellValue(sdf.format(c.getCreateDate())); j++; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { workbook.write(baos); } catch (IOException e) { e.printStackTrace(); } byte[] aa = baos.toByteArray(); excelFile = new ByteArrayInputStream(aa,0,aa.length); try { baos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
导出的代码仅仅是给大家一个參照,当然使用jxl导出excel也是全然能够的
(3)导出excel的总方法
public String exportCardInfo() throws Exception { String startNum=this.getRequest().getParameter("startCardNum"); String num=this.getRequest().getParameter("num"); ExcelFile(startNum,num); this.setDownloadFileName(String.valueOf(Calendar.getInstance().getTimeInMillis())+".xls"); return "export"; }
代码是不是特别简单呢。假设有问题给我写评论
相关文章
- NPOI写Excel,Spire.XLS for.NET组件转换Excel为PDF
- Office EXCEL 如何设置最大行高
- asp.net中导出excel数据的方法汇总
- Excel 求差集和并集
- 实时更新Excel文档外部数据源的数据
- 实时更新Excel文档外部数据源的数据
- Excel VLOOKUP函数的使用方法
- 实时更新Excel文档外部数据源的数据
- 实时更新Excel文档外部数据源的数据
- 将 SAP ABAP 内表内容本地导出成 Excel 文件试读版
- Atitit word ppt excel convert txt bp 等文档转换纯文本问题最佳实践.docx Atitit word ppt excel等文档转换txt问题最佳实践 目录 1.
- easyExcel 公用-简洁【一个excel文档多个sheet 循环读取,适用简单表头】-导入
- 革新文档管理:使用多种编程语言从 Word 中提取 Excel 附件和文件名
- poi 操作excel发现 cell.setCellValue 不可输入字符串-解决
- Bartender 使用 Excel xlsx 数据库时出现 0x800A0E7A
- java 读取excel poi 和cell 方法