JAVA实现创建Excel表并导出


  1. <span style="line-height: 25.2px; font-family: Verdana, Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1.首先下载poi-3.6-20091214.jar,下载地址如下:</span>  
1.首先下载poi-3.6-20091214.jar,下载地址如下:

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   创建一个实体对象

  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. }  
package com.leyooe.common.bean.customizedproperty;
import java.util.Date;
public class Member{
   private Integer code;
	
   private String name;
   
   private Integer age;
   
   private Date birth;

	public Student(Integer code, String name, Integer age, Date birth) {
	super();
	this.code = code;
	this.name = name;
	this.age = age;
	this.birth = birth;
}

	public Integer getCode() {
		return code;
	}
	
	public void setCode(Integer code) {
		this.code = code;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public Integer getAge() {
		return age;
	}
	
	public void setAge(Integer age) {
		this.age = age;
	}
	
	public Date getBirth() {
		return birth;
	}
	
	public void setBirth(Date birth) {
		this.birth = birth;
	}
   

    
}
3.CreateSimpleExcelToDisk.java

  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((int0);    
  43.         // 第四步,创建单元格,并设置值表头 设置表头居中    
  44.         HSSFCellStyle style = wb.createCellStyle();    
  45.         style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式    
  46.     
  47.         HSSFCell cell = row.createCell((short0);    
  48.         cell.setCellValue("学号");    
  49.         cell.setCellStyle(style);    
  50.         cell = row.createCell((short1);    
  51.         cell.setCellValue("姓名");    
  52.         cell.setCellStyle(style);    
  53.         cell = row.createCell((short2);    
  54.         cell.setCellValue("年龄");    
  55.         cell.setCellStyle(style);    
  56.         cell = row.createCell((short3);    
  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((short0).setCellValue((double) stu.getCode());    
  69.             row.createCell((short1).setCellValue(stu.getName());    
  70.             row.createCell((short2).setCellValue((double) stu.getAge());    
  71.             cell = row.createCell((short3);    
  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. }    
package com.leyooe.otw.api.file;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.leyooe.common.bean.customizedproperty.Member;  
  
public class CreateSimpleExcelToDisk  
{  
    /** 
     * @功能:手工构建一个简单格式的Excel 
     */  
	private static List<Member> getMember() throws Exception  
    {  
        List list = new ArrayList();  
        SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");  
  
        Member user1 = new Member(1, "熊大", 24, df.parse("1993-08-28"));  
        Member user2 = new Member(2, "熊二", 23, df.parse("1994-08-19"));  
        Member user3 = new Member(3, "熊熊", 24, df.parse("1983-11-22"));  
        list.add(user1);  
        list.add(user2);  
        list.add(user3);  
  
        return list;  
    }  
  
    public static void main(String[] args) throws Exception  
    {  
        // 第一步,创建一个webbook,对应一个Excel文件  
        HSSFWorkbook wb = new HSSFWorkbook();  
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet  
        HSSFSheet sheet = wb.createSheet("学生表一");  
        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
        HSSFRow row = sheet.createRow((int) 0);  
        // 第四步,创建单元格,并设置值表头 设置表头居中  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
  
        HSSFCell cell = row.createCell((short) 0);  
        cell.setCellValue("学号");  
        cell.setCellStyle(style);  
        cell = row.createCell((short) 1);  
        cell.setCellValue("姓名");  
        cell.setCellStyle(style);  
        cell = row.createCell((short) 2);  
        cell.setCellValue("年龄");  
        cell.setCellStyle(style);  
        cell = row.createCell((short) 3);  
        cell.setCellValue("生日");  
        cell.setCellStyle(style);  
  
        // 第五步,写入实体数据 实际应用中这些数据从数据库得到,  
        List list = CreateSimpleExcelToDisk.getMember();  
  
        for (int i = 0; i < list.size(); i++)  
        {  
            row = sheet.createRow((int) i + 1);  
            Member stu = (Member) list.get(i);  
            // 第四步,创建单元格,并设置值  
            row.createCell((short) 0).setCellValue((double) stu.getCode());  
            row.createCell((short) 1).setCellValue(stu.getName());  
            row.createCell((short) 2).setCellValue((double) stu.getAge());  
            cell = row.createCell((short) 3);  
            cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu  
                    .getBirth()));  
        }  
        // 第六步,将文件存到指定位置  
        try  
        {  
            FileOutputStream fout = new FileOutputStream("E:/Members.xls");  
            wb.write(fout);  
            fout.close();  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
    }  
}  


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

  1. 替换第六步可实现下载  
替换第六步可实现下载
  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.         }     
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值