zoukankan      html  css  js  c++  java
  • JAVA 导出Excel2003格式文件实现代码

     1     public String exportExcel(String name, String name2, String name3,
     2             String name4, String name5, HttpServletRequest request,
     3             HttpServletResponse response){
     4         
     5         String path = request.getRealPath("/");
     6         List list = this.findVsendList(name,name2,name3,name4,name5);
     7         WritableWorkbook book = null;
     8         int pagernum,mus=65535,count,num;
     9         WritableSheet sheet = null;
    10     
    11         try {
    12 
    13             // 在位置0的前方插入表格页
    14             OutputStream os = response.getOutputStream();// 取得输出流
    15             response.reset();// 清空输出流
    16             response.setHeader("Content-disposition", "attachment; filename=" + new String("文件名".getBytes("GB2312"), "8859_1") + ".xls");// 设定输出文件头
    17             response.setContentType("application/msexcel");// 定义输出类型
    18 
    19             book = Workbook.createWorkbook(os);
    20             pagernum=list.size()/mus;
    21             for(int i=0;i<pagernum+1;i++){
    22                 sheet = book.createSheet("sheet"+(i+1), i);
    23 
    24                 // 删除页
    25                 sheet.addCell(new Label(0, 0, "列一")); // 位置从0开始
    26 
    27                 sheet.addCell(new Label(1, 0, "列二"));
    28                 
    29                 sheet.addCell(new Label(2, 0, "列三"));
    30                 
    31                 sheet.addCell(new Label(3, 0, "列四"));
    32                 
    33                 sheet.addCell(new Label(4, 0, "列五"));
    34                 
    35                 sheet.addCell(new Label(5, 0, "列六"));
    36                 
    37                 sheet.addCell(new Label(6, 0, "列七"));
    38                 
    39                 sheet.addCell(new Label(7, 0, "列八"));
    40                 
    41                 if(i==pagernum){
    42                     count=list.size()%mus;
    43                 }else{
    44                     count=mus;
    45                 }
    46                 num=i*mus;
    47                 
    48                 for (int j = num; j < num+count; j++) {
    49                     VSendList sendlist = (VSendList) list.get(j);                
    50                     if (sendlist != null) {
    51                         String Type = "";
    52                         if (1 == sendlist.getXx()) {
    53                             Type = "XX";
    54                         } else if (2 == sendlist.getXx()) {
    55                             Type = "XX";                        
    56                         } else if (3 == sendlist.getXx()){
    57                             Type = "XX";
    58                         } else {
    59                             Type = "XX";
    60                         }
    61                         sheet.addCell(new Label(0, j + 1 - num, sendlist.getXxxx())); // 位置从0开始
    62                         sheet.addCell(new Label(1, j + 1 - num, sendlist.getXx()));
    63                         sheet.addCell(new Label(2, j + 1 - num, sendlist.getXxxx()));
    64                         sheet.addCell(new Label(3, j + 1 - num, Type));
    65                         sheet.addCell(new Label(4, j + 1 - num, sendlist.getXxxxx().substring(0, 19)));
    66                         sheet.addCell(new Label(5, j + 1 - num, sendlist.getXxxxxx()));
    67                         sheet.addCell(new Label(6, j + 1 - num, sendlist.getXxxxxx().toString()));
    68                         sheet.addCell(new Label(7, j + 1 - num, sendlist.getXxxxx()));
    69                     }
    70                 }
    71             }
    72             book.write();
    73         } catch (Exception e) {
    74             e.printStackTrace();
    75         } finally {
    76             if (book != null) {
    77                 try {
    78                     book.close();
    79                 } catch (Exception e) {
    80                     e.printStackTrace();
    81                 }
    82             }
    83         }
    84         return null;
    85     }
  • 相关阅读:
    [原创]在使用SDK 23(6.0)版本后org.apache.http相关的类找不到的解决办法
    [原创] Gradle DSL method not found: 'android()' 和 buildToolsVersion is not specified 的解决办法。
    [原创]Android Lollipop (5.0) 原生代码 Settings 首页加载逻辑分析
    访问https接口报错 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系
    js for循环中延迟 setTimeout
    ABP框架扩展AbpSession
    web在线查看PDF文件
    web在线预览office文件
    sql server游标读取excel文件数据,更新到指定表中
    echarts图形销毁重新绘制
  • 原文地址:https://www.cnblogs.com/sky-of-chuanqingchen/p/4164950.html
Copyright © 2011-2022 走看看