Java 实现导出excel表 POI

来源:互联网 发布:篮球队logo设计软件 编辑:程序博客网 时间:2024/06/14 02:37

Java 实现导出excel表 POI

1.首先下载poi-3.6-20091214.jar,下载地址如下:

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

2.Student.java

  1. import java.util.Date;  
  2.   
  3. public class Student  
  4. {  
  5.     private int id;  
  6.     private String name;  
  7.     private int age;  
  8.     private Date birth;  
  9.   
  10.     public Student()  
  11.     {  
  12.     }  
  13.   
  14.     public Student(int id, String name, int age, Date birth)  
  15.     {  
  16.         this.id = id;  
  17.         this.name = name;  
  18.         this.age = age;  
  19.         this.birth = birth;  
  20.     }  
  21.   
  22.     public int getId()  
  23.     {  
  24.         return id;  
  25.     }  
  26.   
  27.     public void setId(int id)  
  28.     {  
  29.         this.id = id;  
  30.     }  
  31.   
  32.     public String getName()  
  33.     {  
  34.         return name;  
  35.     }  
  36.   
  37.     public void setName(String name)  
  38.     {  
  39.         this.name = name;  
  40.     }  
  41.   
  42.     public int getAge()  
  43.     {  
  44.         return age;  
  45.     }  
  46.   
  47.     public void setAge(int age)  
  48.     {  
  49.         this.age = age;  
  50.     }  
  51.   
  52.     public Date getBirth()  
  53.     {  
  54.         return birth;  
  55.     }  
  56.   
  57.     public void setBirth(Date birth)  
  58.     {  
  59.         this.birth = birth;  
  60.     }  
  61.   
  62. }  
3.CreateSimpleExcelToDisk.java

  1. import java.io.FileOutputStream;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.apache.poi.hssf.usermode���WSFCell��3 `H���D3���?fcQ�X�`���d�!��6���NV�W$P/����$ < ��.e�����|e�����|�K*/�ݧ�����Z��;���4��FE��d:^Ȍ82�r�Xe��ѱg�NV�{镴T�4B b_�ger-����7F�#�6g�X���?��kRsd53��T!$��=�f��^rgb(108,226,108); list-style:decimal outside; color:inherit; line-height:1.6; margin:0px!important; padding:0px 3px 0px 10px!important">import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
  7. import org.apache.poi.hssf.usermodel.HSSFRow;  
  8. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  10.   
  11. public class CreateSimpleExcelToDisk  
  12. {  
  13.     /** 
  14.      * @功能:手工构建一个简单格式的Excel 
  15.      */  
  16.     private static List<Student> getStudent() throws Exception  
  17.     {  
  18.         List list = new ArrayList();  
  19.         SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");  
  20.   
  21.         Student user1 = new Student(1"张三"16, df.parse("1997-03-12"));  
  22.         Student user2 = new Student(2"李四"17, df.parse("1996-08-12"));  
  23.         Student user3 = new Student(3"王五"26, df.parse("1985-11-12"));  
  24.         list.add(user1);  
  25.         list.add(user2);  
  26.         list.add(user3);  
  27.   
  28.         return list;  
  29.     }  
  30.   
  31.     public static void main(String[] args) throws Exception  
  32.     {  
  33.         // 第一步,创建一个webbook,对应一个Excel文件  
  34.         HSSFWorkbook wb = new HSSFWorkbook();  
  35.         // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet  
  36.         HSSFSheet sheet = wb.createSheet("学生表一");  
  37.         // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
  38.         HSSFRow row = sheet.createRow((int0);  
  39.         // 第四步,创建单元格,并设置值表头 设置表头居中  
  40.         HSSFCellStyle style = wb.createCellStyle();  
  41.         style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
  42.   
  43.         HSSFCell cell = row.createCell((short0);  
  44.         cell.setCellValue("学号");  
  45.         cell.setCellStyle(style);  
  46.         cell = row.createCell((short1);  
  47.         cell.setCellValue("姓名");  
  48.         cell.setCellStyle(style);  
  49.         cell = row.createCell((short2);  
  50.         cell.setCellValue("年龄");  
  51.         cell.setCellStyle(style);  
  52.         cell = row.createCell((short3);  
  53.         cell.setCellValue("生日");  
  54.         cell.setCellStyle(style);  
  55.   
  56.         // 第五步,写入实体数据 实际应用中这些数据从数据库得到,  
  57.         List list = CreateSimpleExcelToDisk.getStudent();  
  58.   
  59.         for (int i = 0; i < list.size(); i++)  
  60.         {  
  61.             row = sheet.createRow((int) i + 1);  
  62.             Student stu = (Student) list.get(i);  
  63.             // 第四步,创建单元格,并设置值  
  64.             row.createCell((short0).setCellValue((double) stu.getId());  
  65.             row.createCell((short1).setCellValue(stu.getName());  
  66.             row.createCell((short2).setCellValue((double) stu.getAge());  
  67.             cell = row.createCell((short3);  
  68.             cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu  
  69.                     .getBirth()));  
  70.         }  
  71.         // 第六步,将文件存到指定位置  
  72.         try  
  73.         {  
  74.             FileOutputStream fout = new FileOutputStream("E:/students.xls");  
  75.             wb.write(fout);  
  76.             fout.close();  
  77.         }  
  78.         catch (Exception e)  
  79.         {  
  80.             e.printStackTrace();  
  81.         }  
  82.     }  
  83. }  
0 0
原创粉丝点击