• 用jxl导出excel报表


    1.在项目中加入jxl.jar包

    2.页面是一个from表单,也可以是一个a标签...,示例如下

     1 <s:form action="claimVoucherStatistics_createDetailExcel.action" name="queryForm">
     2       <label for="time">年份:</label>
     3       <s:property value="year"/>
     4       <label for="end-time">月份:</label>
     5       <s:property value="selectMonth"/>
     6      <s:hidden name="year" value="%{year}"/>
     7      <s:hidden name="selectMonth" value="%{selectMonth}"/>
     8      <s:hidden name="departmentId" value="%{departmentId}"/>
     9      <input type="submit" class="submit_01" value="导出报表"/>
    10 </s:form>

    3.后台代码,示例如下

     1     /**
     2      * 
     3      */
     4     private static final long serialVersionUID = 1L;
     5     
     6     //claimVoucherStatistics_getDeptStatisticsByMonth.action
     7     private PageInfo pageInfo;
     8     private BizClaimVoucherStatisticsService voucherStatisticsService;
     9     private Short year;//
    10     private Short startMonth;//开始月份
    11     private Short endMonth;//结束月份
    12     private Integer pageIndex;//当前页码
    13     
    14     private Short selectMonth ;//选择查询的月份
    15     private Long departmentId;//选择查询的部门编号
    16     private List<Detail> detailList;//详情集合 
    17     private Double detailCount=0.0;
    18     
    19     private JFreeChart chart;//图形工具
    20     private String fileName; //导出报表时显示的文件名
    21     /**
    22      * 导出报表
    23      * @return
    24      * @throws Exception
    25      */
    26     public String createDetailExcel() throws Exception {
    27         //fileName=year+"年"+selectMonth+"月"+detailList.get(0).getDeptName()+"月度报销统计表";
    28         return "detailExcel";
    29     }
    30     public InputStream getInputStream() {
    31         //查询到的list集合数据
    32         detailList=voucherStatisticsService.getDeptVoucherDetailByMonth(year,selectMonth,departmentId);
    33         Object[][] objectArray=new Object[detailList.size()][];
    34     
    35         //把集合循环在次封装到一个二维数组
    36         for(int i=0;i<detailList.size();i++){
    37             Object ob[]={detailList.get(i).getName(),detailList.get(i).getTotalAccount(),detailList.get(i).getYear()
    38                     ,detailList.get(i).getMonth(),detailList.get(i).getDeptName()};
    39             objectArray[i]=ob;
    40             ob=null;
    41         }
    42         String headTitle=year+"年"+selectMonth+"月"+detailList.get(0).getDeptName()+"月度报销统计表";
    43         fileName=headTitle;
    44         String []titles={"报销人","报销总额","年份","月份","部门"};
    45         ByteArrayOutputStream out = new ByteArrayOutputStream();
    46         //调用工具类
    47         new ToExcelUtil().printToExcel(objectArray, out, headTitle, titles);
    48         return new ByteArrayInputStream(out.toByteArray());
    49     }
    50 
    51     public String getFileName() throws Exception {
    52         //return fileName;
    53         /**
    54          * 解决中文乱码
    55          */
    56         return java.net.URLEncoder.encode(fileName, "UTF-8");//??????????????????
    57     }
    58 
    59 
    60     public void setFileName(String fileName) {
    61         this.fileName = fileName;
    62     }

    4.工具类,示例如下

     1 package cn.bd.jboa.util;
     2 
     3 import java.io.OutputStream;
     4 import jxl.Workbook;
     5 import jxl.write.Label;
     6 import jxl.write.WritableCellFormat;
     7 import jxl.write.WritableSheet;
     8 import jxl.write.WritableWorkbook;
     9 
    10 
    11 public class ToExcelUtil {
    12 
    13     /**
    14      * 
    15      * @param objectArray 二维数组
    16      * @param os 输出流
    17      * @param headTitle 头部标题
    18      * @param titles 标题
    19      */
    20     
    21     // 输出集合信息到Excel中
    22         public void printToExcel(Object[][] objectArray,OutputStream os,String headTitle,String[] titles) {
    23             try {
    24                 // 打开Excel文件
    25                 WritableWorkbook workBook = Workbook.createWorkbook(os);
    26                 // 生成名为“图书信息汇总”的工作表,参数0表示这是第一页
    27                 //WritableSheet sheet = workBook.createSheet("图书信息借阅汇总",0);
    28                 WritableSheet sheet = workBook.createSheet(headTitle,0);
    29                 /*String[] titles = { "图书编号", "图书分类", "图书名称", "作者", "出版社", "借阅状态","" };*/
    30                 // 填充标题信息
    31                 
    32                 
    33                 //*******************************合并单元格 **********************************//
    34                 //sheet.mergeCells(int m,int n,int p,int q); 
    35                 //作用是从 (m,n)到(p,q)的单元格全部合并,比如: 
    36                 
    37                 //合并第一列第一行到第五列第一行的所有单元格 
    38                 sheet.mergeCells(0,0,4,0); 
    39                 //给合并后(第0列第0行)的单元格赋值
    40                 Label label3 = new Label(0, 0, headTitle);
    41                 //单元格居中
    42                 WritableCellFormat cellFormat = new WritableCellFormat();  
    43                 cellFormat.setAlignment(jxl.format.Alignment.CENTRE);  
    44                 label3.setCellFormat(cellFormat); 
    45                 //将定义好的单元格添加到工作表中
    46                 sheet.addCell(label3);
    47                 //合并既可以是横向的,也可以是纵向的。合并后的单元格不能再次进行合并,否则会触发异常。
    48                 //**************************** 合并单元格 ********************************//
    49                   
    50                  
    51                 
    52                 
    53                 for (int i = 0; i < titles.length; i++) {
    54                     // 在Label对象的构造子中指名单元格位置是第i列第0行(i,1),单元格内容为titles[i]
    55                     Label label = new Label(i, 1, titles[i]);
    56                     // 将定义好的单元格添加到工作表中
    57                     sheet.addCell(label);
    58                 }
    59                 //准备填充单元格的数据源
    60                 for (int j = 0; j < objectArray.length; j++) {
    61                     Object[] cellContents = objectArray[j];
    62                     
    63                     //将数据填充到单元中
    64                     for (int i = 0; i < cellContents.length; i++) {
    65                         Object content = cellContents[i];
    66                             // 在Label对象的构造子中指名单元格位置是第i列第0行(i,0),单元格内容为titles[i]
    67                             Label label = new Label(i, j + 2, cellContents[i].toString());///
    68                             // 将定义好的单元格添加到工作表中
    69                             sheet.addCell(label);
    70                     }
    71                 }
    72                 // 写入数据并关闭文件
    73                 workBook.write();
    74                 workBook.close();
    75 
    76             } catch (Exception e) {
    77                 e.printStackTrace();
    78                 System.out.println(e);
    79             }
    80         }
    81 }

    5.struts.xml的配置文件

     1         <!-- 月度报销单 -->
     2         <action name="claimVoucherStatistics_*" class="VoucherStatisticsAction" method="{1}">
    13             <result name="detailExcel" type="stream">
    14                 <param name="contentType">
    15                     application/vnd.ms-excel
    16                 </param>
    17                 <param name="inputStream">inputStream</param>
    18                 <param name="contentDisposition">
    19                     filename="${fileName}.xls"
    20                 </param>
    21                 <param name="bufferSize">1024</param>
    22             </result>
    26         </action>

    6.导出的excel文件如图

  • 相关阅读:
    剑指offer-树的子结构
    剑指offer-二叉搜索树的后序遍历序列
    剑指offer-调整数组顺序使奇数位于偶数前面
    剑指offer-包含min函数的栈
    剑指offer-从上往下打印二叉树
    剑指offer-链表中倒数第k个结点
    剑指offer-合并两个排列的链接
    剑指offer-替换空格
    剑指offer-旋转数组的最小数字
    剑指offer-数字在排序数组中出现的次数
  • 原文地址:https://www.cnblogs.com/taobd/p/6759452.html
Copyright © 2020-2023  润新知