zoukankan      html  css  js  c++  java
  • java 实现导出Excel文件

    java 实现导出Excel(java生成 excel 并导出文件)

    经常有有一些数据需要导出成   excel  格式 ,所以就需要实现啦

    开始:

    1.加入jar

       poi-3.6-20091214.jar

       commons-logging-1.1.jar

        junit-3.8.1.jar

        log4j-1.2.13.jar

    2. 实现代码 :

     1 /**
     2      * 创建excel
     3      * 
     4      * @param list
     5      * @param request
     6      * @param response
     7      */
     8     private void excel(List<MyBrowselog> list, HttpServletRequest request, HttpServletResponse response) {
     9         OutputStream outputStream = null;
    10         response.setCharacterEncoding("utf-8");
    11         response.setContentType("application/x-msdownload");
    12         try {
    13             response.setHeader("Content-disposition",
    14                     "attachment;filename=" + new String("个人用户统计".getBytes("utf-8"), "ISO8859-1") + ".xls");
    15         } catch (UnsupportedEncodingException e2) {
    16             e2.printStackTrace();
    17         }
    18         try {
    19             outputStream = response.getOutputStream();
    20         } catch (IOException e1) {
    21             e1.printStackTrace();
    22         }
    23 
    24         SXSSFWorkbook workbook = new SXSSFWorkbook(list.size() + 1);
    25         Sheet sheet = workbook.createSheet("个人用户统计");
    26         Row headRow = sheet.createRow(0);
    27         Cell cell0 = headRow.createCell(0);
    28         cell0.setCellValue("用户名");
    29         Cell cell1 = headRow.createCell(1);
    30         cell1.setCellValue("浏览量");
    31         Cell cell2 = headRow.createCell(2);
    32         cell2.setCellValue("下载量");
    33         // 创建行
    34         for (int i = 0; i < list.size(); i++) {
    35             Row dataRow = sheet.createRow(i + 1);
    36             Cell cell_0 = dataRow.createCell(0);
    37             cell_0.setCellValue(list.get(i) == null ? "" : list.get(i).getUserLoginName());
    38             Cell cell_1 = dataRow.createCell(1);
    39             cell_1.setCellValue(list.get(i) == null ? "" : list.get(i).getBrowseCount());
    40             Cell cell_2 = dataRow.createCell(2);
    41             cell_2.setCellValue(list.get(i) == null ? "" : list.get(i).getDownloadCount());
    42         }
    43         try {
    44             workbook.write(outputStream);
    45         } catch (IOException e1) {
    46             e1.printStackTrace();
    47         }
    48         try {
    49             outputStream.flush();
    50             outputStream.close();
    51         } catch (IOException e) {
    52             e.printStackTrace();
    53         }
    54     }

    3.各个参数详解

    HSSF(用于操作Excel的组件)提供给用户使用的对象在rg.apache.poi.hssf.usermodel包中,主要部分包括Excel对象,样式和格式,还有辅助操作。有以下几种对象:

     

        常用组件:
    
        HSSFWorkbook                      excel的文档对象
    
        HSSFSheet                         excel的表单
    
        HSSFRow                           excel的行
    
        HSSFCell                          excel的格子单元
    
        HSSFFont                          excel字体
    
        HSSFDataFormat                    日期格式
    
        HSSFHeader                        sheet头
    
        HSSFFooter                        sheet尾(只有打印的时候才能看到效果)
    
        样式:
    
        HSSFCellStyle                       cell样式
    
        辅助操作包括:
    
        HSSFDateUtil                        日期
    
        HSSFPrintSetup                      打印
    
        HSSFErrorConstants                  错误信息表
    

     

     4.基本操作步骤

        1、用HSSFWorkbook打开或者创建“Excel文件对象”
    
        2、用HSSFWorkbook对象返回或者创建Sheet对象
    
        3、用Sheet对象返回行对象,用行对象得到Cell对象
    
        4、对Cell对象读写。
    

      个人参考他人博客 及 自己项目:

      https://blog.csdn.net/xunwei0303/article/details/53213130

         

     

  • 相关阅读:
    操作系统第一天学习
    进制之间的转换
    git的使用
    Python 第二天学习(文件的处理)
    下载博客首页的博客列表
    获取所有的列表
    抓取指定博客的内容
    进程简介
    python 内置函数range和xrange
    关于read的例子和条件测试
  • 原文地址:https://www.cnblogs.com/zhukaixin/p/9167123.html
Copyright © 2011-2022 走看看