struts2实现excel导出_第1页
struts2实现excel导出_第2页
struts2实现excel导出_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、用到的是poi的包。在apache的网站上去下poi-bin-3.2-FINAL-20081019.zip,目前最新的版本。直接上代码了:action:import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.ps.model.UserBizInfo;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.Inpu

2、tStream;import java.io.OutputStream;import java.util.List;import java.util.Map;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFRichTextString;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.

3、usermodel.HSSFWorkbook;public class ExcelDownloadAction extends ActionSupport public InputStream getInputStream() Map session=ActionContext.getContext().getSession(); List<UserBizInfo> list=(List<UserBizInfo>)session.get("ecxelUserBizInfolist"); HSSFWorkbook wb = new HSSFWorkbo

4、ok(); HSSFSheet sheet = wb.createSheet("sheet1"); int rowNum = 0; / 标题开始行 int colNum = 0; / 标题开始列 / HSSFRow excell的头 HSSFRow row = sheet.createRow(rowNum+); / HSSFCell excell的格子单元 HSSFCell cell = row.createCell(short)colNum+); cell.setCellValue("Id"); cell = row.createCell(short)

5、colNum+); cell.setCellValue("用户名"); cell = row.createCell(short)colNum+); cell.setCellValue("类型"); for(UserBizInfo user : list) row = sheet.createRow(rowNum+); colNum=0; cell = row.createCell(short) colNum+); /cell.setCellStyle(cellStyle); String userid=String.valueOf(user.getUse

6、rId(); cell.setCellValue(new HSSFRichTextString(userid); cell = row.createCell(short) colNum+); /cell.setCellStyle(cellStyle); cell.setCellValue(new HSSFRichTextString(user.getUserName(); cell = row.createCell(short) colNum+); /cell.setCellStyle(cellStyle); int type=user.getUserType(); String userty

7、pe="不限" if(type=1) usertype="一般用户" else if(type=2) usertype="摄影师" cell.setCellValue(new HSSFRichTextString(usertype); File file = new File("abc.xls"); /创建一个File 拿来当缓存用.也就是先将内存中的excel写入File中.然后再将File转换成输出流 try OutputStream out = new FileOutputStream(file); wb.w

8、rite(out);/写入File out.close(); catch (Exception e) e.printStackTrace(); InputStream in = null; try in = new FileInputStream(file);/将file转换成输入流 /in.close(); catch (Exception e) / TODO Auto-generated catch block e.printStackTrace(); return in; public String execute() throws Exception try return SUCCES

9、S; catch (Exception e1) e1.printStackTrace(); return ERROR; struts2配置<action name="excel" class="com.ps.action.adminaction.ExcelDownloadAction"> <result name="success" type="stream"> <param name="contentType">application/vnd.ms-excel</param> <param name="contentDisposition">attachment;filename="AllUsers.xls"</param> <p

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论