struts2 使用注解 导出excel

来源:互联网 发布:自动发卡程序源码 编辑:程序博客网 时间:2024/05/20 11:22

做项目中需要用到导出excel的功能,记录一下。

1、使用jxl导出。

这个是从网上找的。这个不需要params inputName参数。

[java] view plaincopy
  1. @Action(value = "vsealFileDepts.export1", results = { @Result(name = "success", type = "stream") })  
  2.     // , params = {  
  3.     // "contentType", "text/html;charset=UTF-8" }  
  4.     // ,params={  
  5.     // "contentType","application/octet-stream",  
  6.     // "inputName","fileInputStream",  
  7.     // "contentDisposition","attachment;filename=${fileName}.xls",  
  8.     // "bufferSize","1024"  
  9.     // }  
  10.     public String export1() throws Exception {  
  11.   
  12.         HttpServletResponse response = ServletActionContext.getResponse();  
  13.         // 定义request ,response.  
  14.         // 查询下载附件.  
  15.         // 设置下载头信息.begin  
  16.         response.setCharacterEncoding("UTF-8");  
  17.         response.setContentType("application/vnd.ms-excel");  
  18.         response.setHeader("Content-Disposition""attachment; filename="  
  19.                 + new String("用户通讯录.xls".getBytes("GB2312"), "iso8859-1"));  
  20.         response.setHeader("Pragma""No-cache");  
  21.         response.setHeader("Cache-Control""No-cache");  
  22.         response.setDateHeader("Expires"0);  
  23.         // 这个地方一定要进行编码的转换要不然中文字符会出现乱码.  
  24.         // 设置下载头信息.end,  
  25.         OutputStream output = null;  
  26.         InputStream fis = null;  
  27.         try {  
  28.             output = response.getOutputStream();  
  29.             jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(output);  
  30.             jxl.write.WritableSheet ws = wwb.createSheet("用户通讯录"0);  
  31.             // 设置标题.ws.addCell(new jxl.write.Label(列, 行, 内容.));  
  32.             ws.addCell(new Label(00"编号"));  
  33.             ws.addCell(new Label(10"登录名称"));  
  34.             ws.addCell(new Label(20"联系人"));  
  35.             ws.addCell(new Label(30"电话"));  
  36.             ws.addCell(new Label(40"email"));  
  37.             ws.addCell(new Label(50"单位名称"));  
  38.             ws.addCell(new Label(60"单位邮编"));  
  39.             ws.addCell(new Label(70"单位地址"));  
  40.             // 设置显示长度.  
  41.             ws.setColumnView(115);  
  42.             // 登录名长度  
  43.             ws.setColumnView(215);  
  44.             ws.setColumnView(315);  
  45.             ws.setColumnView(420);  
  46.             ws.setColumnView(520);  
  47.             ws.setColumnView(620);  
  48.             ws.setColumnView(720);  
  49.             ws.setColumnView(840);  
  50.             int i = 1;  
  51.             List list1 = new ArrayList();  
  52.             for (int j = 0; j < list1.size(); j++) {  
  53.                 User user = (User) list1.get(j);  
  54.                 ws.addCell(new jxl.write.Number(0, i + 1, i));  
  55.                 // 这里设置是自增的序号而不是ID号.也可以改成ID号.  
  56.                 // ws.addCell(new jxl.write.Label(1, i + 1, ""  
  57.                 // + user.getUserId()));  
  58.                 ws.addCell(new Label(1, i + 1"" + user.getAddress()));  
  59.                 // 登录名  
  60.                 ws.addCell(new Label(2, i + 1"" + user.getAddress()));  
  61.                 // 联系人  
  62.                 ws.addCell(new Label(3, i + 1"" + user.getPhone()));  
  63.                 // 联系电话.  
  64.                 ws.addCell(new Label(4, i + 1"" + user.getEmail()));  
  65.                 // email.  
  66.                 if (null != user.getAddress()) {  
  67.                     ws.addCell(new Label(5, i + 1"" + user.getAddress()));  
  68.                     if (user.getAddress() != null) {  
  69.                         ws.addCell(new Label(6, i + 1"" + user.getAddress()));  
  70.                     } else {  
  71.                         ws.addCell(new Label(6, i + 1""));  
  72.                         // 增加邮编为""的判断.因为这个是Integer的类型.  
  73.                     }  
  74.                     ws.addCell(new Label(7, i + 1"" + user.getAddress()));  
  75.                 } else {  
  76.                     ws.addCell(new Label(5, i + 1""));  
  77.                     ws.addCell(new Label(6, i + 1""));  
  78.                     ws.addCell(new Label(7, i + 1""));  
  79.                 }  
  80.                 i++;  
  81.             }  
  82.             wwb.write();  
  83.             wwb.close();  
  84.         } catch (Exception e) {  
  85.             System.out.println("Error!");  
  86.             e.printStackTrace();  
  87.         } finally {// 正常关闭输入输出流.  
  88.             try {  
  89.                 if (fis != null) {  
  90.                     fis.close();  
  91.                     fis = null;  
  92.                 }  
  93.             } catch (Exception e) {  
  94.                 e.printStackTrace();  
  95.             }  
  96.             try {  
  97.                 if (output != null) {  
  98.                     output.close();  
  99.                     output = null;  
  100.                 }  
  101.             } catch (Exception e) {  
  102.                 e.printStackTrace();  
  103.             }  
  104.         }  
  105.         return null;  
  106.     }  

前端jsp页面能调用这个action即可。

2、使用poi导出。

这种方法必须要使用struts2的注解中的params  inputName 参数。

首先在action中声明变量,并写明get/set方法

[java] view plaincopy
  1. private InputStream excelFile;  
  2. private String downloadFileName;  
  3.     public String getDownloadFileName() {  
  4.         SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd ");  
  5.   
  6.         String downloadFileName = (sf.format(new Date()).toString())  
  7.                 + "用印文件统计.xls";  
  8.         try {  
  9.             downloadFileName = new String(downloadFileName.getBytes(),  
  10.                     "ISO8859-1");  
  11.         } catch (UnsupportedEncodingException e) {  
  12.             e.printStackTrace();  
  13.         }  
  14.         return downloadFileName;  
  15.     }  
  16.   
  17.     public void setDownloadFileName(String downloadFileName) {  
  18.         this.downloadFileName = downloadFileName;  
  19.     }  
  20.   
  21.     public InputStream getExcelFile() {  
  22.         return excelFile;  
  23.     }  
  24.   
  25.     public void setExcelFile(InputStream excelFile) {  
  26.         this.excelFile = excelFile;  
  27.     }  
前端可执行调用的方法,注意:inputName对应的必须为前面声明过的变量。

[java] view plaincopy
  1. @Action(value = "vsealFileDepts.exportExcel", results = { @Result(name = "success", type = "stream", params = {  
  2.             "contentType""application/vnd.ms-excel""inputName",  
  3.             "excelFile""contentDisposition",  
  4.             "attachment;filename=${downloadFileName}.xls""bufferSize""1024" }) })  
  5.     public String export2() throws Exception {  
  6.         ExcelUtil eu = new ExcelUtil();  
  7.         HSSFWorkbook workbook = eu.exportExcel(titleSBSub.toString(), dataList,  
  8.                 titleSB.toString());  
  9.         ByteArrayOutputStream output = new ByteArrayOutputStream();  
  10.         workbook.write(output);  
  11.   
  12.         byte[] ba = output.toByteArray();  
  13.         excelFile = new ByteArrayInputStream(ba);  
  14.         output.flush();  
  15.         output.close();  
  16.         return "success";  
  17.     }  

ExcelUtil.java  生成excel的类

[java] view plaincopy
  1. public class ExcelUtil {  
  2.     public HSSFWorkbook exportExcel(String tmpContentCn,List dataList) throws Exception {  
  3.         HSSFWorkbook workbook = null;  
  4.         String[] titles_CN = tmpContentCn.split(",");  
  5.         try {  
  6.             // 这里的数据即时你要从后台取得的数据  
  7.   
  8.             // 创建工作簿实例  
  9.             workbook = new HSSFWorkbook();  
  10.             // 创建工作表实例  
  11.             HSSFSheet sheet = workbook.createSheet("TscExcel");  
  12.              //设置列宽   
  13.             this.setSheetColumnWidth(titles_CN,sheet);  
  14.           //获取样式   
  15.             HSSFCellStyle style = this.createTitleStyle(workbook);   
  16.             if (dataList != null && dataList.size() > 0) {  
  17.                 // 创建第一行标题  
  18.                 HSSFRow row = sheet.createRow((short0);// 建立新行  
  19.   
  20.                 for(int i=0;i<titles_CN.length;i++){  
  21.                     this.createCell(row, i, null, HSSFCell.CELL_TYPE_STRING,   
  22.                            titles_CN[i]);  
  23.                     }  
  24.                 // 给excel填充数据  
  25.                 for (int i = 0; i < dataList.size(); i++) {  
  26.                     // 将dataList里面的数据取出来  
  27.                     String[] model= (String[]) dataList.get(i);  
  28.                     HSSFRow row1 = sheet.createRow((short) (i + 1));// 建立新行  
  29. //                  this.createCell(row1, 0, style, HSSFCell.CELL_TYPE_STRING,  
  30. //                          i + 1);  
  31.                     for(int j=0;j<model.length;j++)  
  32.                         this.createCell(row1, j, style,  
  33.                                 HSSFCell.CELL_TYPE_STRING, model[j]);  
  34.   
  35.                 }  
  36.             } else {  
  37.                 this.createCell(sheet.createRow(0), 0, style,  
  38.                         HSSFCell.CELL_TYPE_STRING, "查无资料");  
  39.             }  
  40.         } catch (Exception e) {  
  41.             e.printStackTrace();  
  42.         }  
  43.         return workbook;  
  44.   
  45.     }  

在研究研究以前同事写的可以利用反射来实现可以统一调用的方法。
原创粉丝点击