java导出EXCEL表格小demo

我自己在网上找的例子,地址:http://blog.csdn.net/u014621859/article/details/54944059

自己的代码: 

  1 package com.ym.admin.controller; 
  2 
  3 import java.io.FileOutputStream;
  4 import java.text.SimpleDateFormat;
  5 import java.util.ArrayList;
  6 import java.util.Date;
  7 import java.util.List;
  8 
  9 import org.apache.poi.hssf.usermodel.HSSFCell;
 10 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
 11 import org.apache.poi.hssf.usermodel.HSSFRow;
 12 import org.apache.poi.hssf.usermodel.HSSFSheet;
 13 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 14 
 15 /** 
 16  * @author Anear: 
 17  * @version 创建时间:2017年12月23日 下午3:52:54 
 18  */
 19 public class TestExcelMain {
 20 
 21     /**  
 22      * @功能:手工构建一个简单格式的Excel  
 23      */    
 24     private static List<Member> getMember() throws Exception    
 25     {    
 26         List list = new ArrayList<Member>();    
 27         SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");    
 28     
 29         Member user1 = new Member(1, "anpei", 16,df.parse("1994-08-19"));   
 30         Member user2 = new Member(2, "熊二", 23, df.parse("1994-08-19"));    
 31         Member user3 = new Member(3, "熊熊", 24, df.parse("1983-11-22"));    
 32         list.add(user1);    
 33         list.add(user2);    
 34         list.add(user3);    
 35     
 36         return list;    
 37     }    
 38     
 39     public static void main(String[] args) throws Exception    
 40     {    
 41         // 第一步,创建一个webbook,对应一个Excel文件    
 42         HSSFWorkbook wb = new HSSFWorkbook();    
 43         // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet    
 44         HSSFSheet sheet = wb.createSheet("学生表一");    
 45         // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short    
 46         HSSFRow row = sheet.createRow((int) 0);    
 47         // 第四步,创建单元格,并设置值表头 设置表头居中    
 48         HSSFCellStyle style = wb.createCellStyle();    
 49         style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式    
 50     
 51         HSSFCell cell = row.createCell((short) 0);    
 52         cell.setCellValue("学号");    
 53         cell.setCellStyle(style);    
 54         cell = row.createCell((short) 1);    
 55         cell.setCellValue("姓名");    
 56         cell.setCellStyle(style);    
 57         cell = row.createCell((short) 2);    
 58         cell.setCellValue("年龄");    
 59         cell.setCellStyle(style);    
 60         cell = row.createCell((short) 3);    
 61         cell.setCellValue("生日");    
 62         cell.setCellStyle(style);    
 63     
 64         // 第五步,写入实体数据 实际应用中这些数据从数据库得到,    
 65         List list = TestExcelMain.getMember();    
 66     
 67         for (int i = 0; i < list.size(); i++)    
 68         {    
 69             row = sheet.createRow((int) i + 1);    
 70             Member stu = (Member) list.get(i);    
 71             // 第四步,创建单元格,并设置值    
 72             row.createCell((short) 0).setCellValue((double) stu.getCode());    
 73             row.createCell((short) 1).setCellValue(stu.getName());    
 74             row.createCell((short) 2).setCellValue((double) stu.getAge());    
 75             cell = row.createCell((short) 3);    
 76             cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu    
 77                     .getBirth()));    
 78         }    
 79         // 第六步,将文件存到指定位置    
 80         try    
 81         {    
 82             FileOutputStream fout = new FileOutputStream("E:/Members.xls");    
 83             wb.write(fout);    
 84             fout.close();    
 85         }    
 86         catch (Exception e)    
 87         {    
 88             e.printStackTrace();    
 89         }    
 90     }  
 91     
 92     public static class Member{  
 93            private Integer code;  
 94               
 95            private String name;  
 96              
 97            private Integer age;  
 98              
 99            private Date birth;  
100           
101             public Member(Integer code, String name, Integer age, Date birth) {  
102             super();  
103             this.code = code;  
104             this.name = name;  
105             this.age = age;  
106             this.birth = birth;  
107         }  
108           
109             public Integer getCode() {  
110                 return code;  
111             }  
112               
113             public void setCode(Integer code) {  
114                 this.code = code;  
115             }  
116               
117             public String getName() {  
118                 return name;  
119             }  
120               
121             public void setName(String name) {  
122                 this.name = name;  
123             }  
124               
125             public Integer getAge() {  
126                 return age;  
127             }  
128               
129             public void setAge(Integer age) {  
130                 this.age = age;  
131             }  
132               
133             public Date getBirth() {  
134                 return birth;  
135             }  
136               
137             public void setBirth(Date birth) {  
138                 this.birth = birth;  
139             }  
140              
141           
142               
143         }  
144 }

pom文件:

1 <dependency>
2            <groupId>org.apache.poi</groupId>
3            <artifactId>poi</artifactId>
4            <version>3.15</version>
5         </dependency>

效果:

posted @ 2017-12-23 16:13  根目录97  阅读(945)  评论(1编辑  收藏  举报