zoukankan      html  css  js  c++  java
  • JAVA实现创建Excel表并导出(转发)

    最近在做毕设,要求导出word,excel,pdf,这是excel。
    原文是:http://blog.csdn.net/u014621859/article/details/54944059
    [java] view plain copy
     
    1. <span style="font-family:Verdana, Arial, Helvetica, sans-serif;line-height:25.2px;background-color:rgb(255,255,255);">1.首先下载poi-3.6-20091214.jar,下载地址如下:</span>  

    http://download.csdn.net/detail/evangel_z/3895051

    或者使用Maven仓库管理,在pom文件添加坐标

                <dependency>
       <groupId>org.apache.poi</groupId>
       <artifactId>poi</artifactId>
       <version>3.15</version>
               </dependency>

    2.Member.java   创建一个实体对象

    [java] view plain copy
     
    1. package com.leyooe.common.bean.customizedproperty;  
    2. import java.util.Date;  
    3. public class Member{  
    4.    private Integer code;  
    5.       
    6.    private String name;  
    7.      
    8.    private Integer age;  
    9.      
    10.    private Date birth;  
    11.   
    12.     public Student(Integer code, String name, Integer age, Date birth) {  
    13.     super();  
    14.     this.code = code;  
    15.     this.name = name;  
    16.     this.age = age;  
    17.     this.birth = birth;  
    18. }  
    19.   
    20.     public Integer getCode() {  
    21.         return code;  
    22.     }  
    23.       
    24.     public void setCode(Integer code) {  
    25.         this.code = code;  
    26.     }  
    27.       
    28.     public String getName() {  
    29.         return name;  
    30.     }  
    31.       
    32.     public void setName(String name) {  
    33.         this.name = name;  
    34.     }  
    35.       
    36.     public Integer getAge() {  
    37.         return age;  
    38.     }  
    39.       
    40.     public void setAge(Integer age) {  
    41.         this.age = age;  
    42.     }  
    43.       
    44.     public Date getBirth() {  
    45.         return birth;  
    46.     }  
    47.       
    48.     public void setBirth(Date birth) {  
    49.         this.birth = birth;  
    50.     }  
    51.      
    52.   
    53.       
    54. }  

    3.CreateSimpleExcelToDisk.java

    [java] view plain copy
     
    1. package com.leyooe.otw.api.file;  
    2. import java.io.FileOutputStream;  
    3. import java.text.SimpleDateFormat;  
    4. import java.util.ArrayList;  
    5. import java.util.List;  
    6.   
    7. import org.apache.poi.hssf.usermodel.HSSFCell;  
    8. import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
    9. import org.apache.poi.hssf.usermodel.HSSFRow;  
    10. import org.apache.poi.hssf.usermodel.HSSFSheet;  
    11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
    12.   
    13. import com.leyooe.common.bean.customizedproperty.Member;    
    14.     
    15. public class CreateSimpleExcelToDisk    
    16. {    
    17.     /**  
    18.      * @功能:手工构建一个简单格式的Excel  
    19.      */    
    20.     private static List<Member> getMember() throws Exception    
    21.     {    
    22.         List list = new ArrayList();    
    23.         SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");    
    24.     
    25.         Member user1 = new Member(1, "熊大", 24, df.parse("1993-08-28"));    
    26.         Member user2 = new Member(2, "熊二", 23, df.parse("1994-08-19"));    
    27.         Member user3 = new Member(3, "熊熊", 24, df.parse("1983-11-22"));    
    28.         list.add(user1);    
    29.         list.add(user2);    
    30.         list.add(user3);    
    31.     
    32.         return list;    
    33.     }    
    34.     
    35.     public static void main(String[] args) throws Exception    
    36.     {    
    37.         // 第一步,创建一个webbook,对应一个Excel文件    
    38.         HSSFWorkbook wb = new HSSFWorkbook();    
    39.         // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet    
    40.         HSSFSheet sheet = wb.createSheet("学生表一");    
    41.         // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short    
    42.         HSSFRow row = sheet.createRow((int) 0);    
    43.         // 第四步,创建单元格,并设置值表头 设置表头居中    
    44.         HSSFCellStyle style = wb.createCellStyle();    
    45.         style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式    
    46.     
    47.         HSSFCell cell = row.createCell((short) 0);    
    48.         cell.setCellValue("学号");    
    49.         cell.setCellStyle(style);    
    50.         cell = row.createCell((short) 1);    
    51.         cell.setCellValue("姓名");    
    52.         cell.setCellStyle(style);    
    53.         cell = row.createCell((short) 2);    
    54.         cell.setCellValue("年龄");    
    55.         cell.setCellStyle(style);    
    56.         cell = row.createCell((short) 3);    
    57.         cell.setCellValue("生日");    
    58.         cell.setCellStyle(style);    
    59.     
    60.         // 第五步,写入实体数据 实际应用中这些数据从数据库得到,    
    61.         List list = CreateSimpleExcelToDisk.getMember();    
    62.     
    63.         for (int i = 0; i < list.size(); i++)    
    64.         {    
    65.             row = sheet.createRow((int) i + 1);    
    66.             Member stu = (Member) list.get(i);    
    67.             // 第四步,创建单元格,并设置值    
    68.             row.createCell((short) 0).setCellValue((double) stu.getCode());    
    69.             row.createCell((short) 1).setCellValue(stu.getName());    
    70.             row.createCell((short) 2).setCellValue((double) stu.getAge());    
    71.             cell = row.createCell((short) 3);    
    72.             cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu    
    73.                     .getBirth()));    
    74.         }    
    75.         // 第六步,将文件存到指定位置    
    76.         try    
    77.         {    
    78.             FileOutputStream fout = new FileOutputStream("E:/Members.xls");    
    79.             wb.write(fout);    
    80.             fout.close();    
    81.         }    
    82.         catch (Exception e)    
    83.         {    
    84.             e.printStackTrace();    
    85.         }    
    86.     }    
    87. }    

    excel表格就被保存到指定位置了。

    [java] view plain copy
     
    1. 替换第六步可实现下载  
    [java] view plain copy
     
      1. // 第六步,下载excel  
      2.           
      3.         OutputStream out = null;    
      4.         try {        
      5.             out = response.getOutputStream();    
      6.             String fileName = "enroll.xls";// 文件名    
      7.             response.setContentType("application/x-msdownload");    
      8.             response.setHeader("Content-Disposition", "attachment; filename="    
      9.                                                     + URLEncoder.encode(fileName, "UTF-8"));    
      10.             wb.write(out);    
      11.         } catch (Exception e) {    
      12.             e.printStackTrace();    
      13.         } finally {      
      14.             try {       
      15.                 out.close();      
      16.             } catch (IOException e) {      
      17.                 e.printStackTrace();    
      18.             }      
      19.         }     
  • 相关阅读:
    gethostname()和gethostbyname()获取IP地址和计算机名
    struct hostent结构体
    Memcached在Windows 中启动
    Mysql5.7免安装版跳坑测试
    Memcached 开源,支持高性能,高并发以及分布式的内存缓存软件
    关于压力测试使用的工具
    分布式实时同步系统
    oracle 遍历生成树的节点层次号
    支付宝的及时到账接口
    Kubernetes和TensorFlow集群搭建
  • 原文地址:https://www.cnblogs.com/hj1231/p/8067143.html
Copyright © 2011-2022 走看看