博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Java】Java 实现导出excel表 POI
阅读量:6879 次
发布时间:2019-06-26

本文共 3746 字,大约阅读时间需要 12 分钟。

hot3.png

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

2.Student.java

 

[java] view plaincopyprint?

  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. }  

import java.util.Date; public class Student { private int id; private String name; private int age; private Date birth; public Student() { } public Student(int id, String name, int age, Date birth) { this.id = id; this.name = name; this.age = age; this.birth = birth; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } } 3.CreateSimpleExcelToDisk.java

 

 

[java] view plaincopyprint?

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

转载于:https://my.oschina.net/whitejavadog/blog/714282

你可能感兴趣的文章
Code Coverage API plugin 一个新的代码覆盖率插件
查看>>
DTCC 2019 | 深度解码阿里数据库实现 数据库内核——基于HLC的分布式事务实现深度剖析...
查看>>
【思维导图】NoSQL分布式模型
查看>>
Angular4的依赖注入
查看>>
如果人工智能“圈养”了人类会怎么样?
查看>>
小程序如何生成海报分享朋友圈
查看>>
检测后台错误
查看>>
微信小程序自定义组件
查看>>
Android Studio 和 Gradle 优化配置总结
查看>>
java8 stream实现列表去重,java8的stream 和lambda的使用实例
查看>>
iOS中通知的添加和移除
查看>>
企业分布式微服务云SpringCloud SpringBoot mybatis (一)服务的注册与发现(Eureka)...
查看>>
批量下载图片
查看>>
Java内存模型(Memory Model)
查看>>
某大型网站迁移纪实(一)
查看>>
C#进行Socket 连接发送和接收数据
查看>>
即时编辑插件-jeditable|已迁移
查看>>
Linux下CA证书服务配置
查看>>
《JMeter从入门到精通》之一——开始你的第一个JMeter脚本
查看>>
从技术到管理,艰难的转型
查看>>