Java 实现导出excel表 POI

来源:互联网 发布:知乎城市冷漠 编辑:程序博客网 时间:2024/06/07 09:02

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.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((int0);  
  40.         // 第四步,创建单元格,并设置值表头 设置表头居中  
  41.         HSSFCellStyle style = wb.createCellStyle();  
  42.         style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
  43.   
  44.         HSSFCell cell = row.createCell((short0);  
  45.         cell.setCellValue("学号");  
  46.         cell.setCellStyle(style);  
  47.         cell = row.createCell((short1);  
  48.         cell.setCellValue("姓名");  
  49.         cell.setCellStyle(style);  
  50.         cell = row.createCell((short2);  
  51.         cell.setCellValue("年龄");  
  52.         cell.setCellStyle(style);  
  53.         cell = row.createCell((short3);  
  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((short0).setCellValue((double) stu.getId());  
  66.             row.createCell((short1).setCellValue(stu.getName());  
  67.             row.createCell((short2).setCellValue((double) stu.getAge());  
  68.             cell = row.createCell((short3);  
  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.     }  

1. 数值类型处理

       通过POI取出的数值默认都是double,即使excel单元格中存的是1,取出来的值也是1.0,这就造成了一些问题,如果数据库字段是int,那么就会wrong data type,所以需要对数值类型处理。

 

  1. Cell cell = null;// 单元格  
  2. Object inputValue = null;// 单元格值  
  3. if(!isEmpty(cell) && cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {  
  4.     long longVal = Math.round(cell.getNumericCellValue());  
  5.     if(Double.parseDouble(longVal + ".0") == doubleVal)  
  6.         inputValue = longVal;  
  7.     else  
  8.         inputValue = doubleVal;  
  9. }  

       这么处理后,单元格中的小数没有变化,如果是整数,也会取到整数。

 

       2. 日期类型处理

       很遗憾,POI对单元格日期处理很弱,没有针对的类型,日期类型取出来的也是一个double值,所以同样作为数值类型。

 

  1. Cell cell = null;// 单元格  
  2. Object inputValue = null;// 单元格值  
  3. if(!isEmpty(cell) && cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {  
  4.     if(DateUtil.isCellDateFormatted(c))// 判断单元格是否属于日期格式  
  5.         inputValue = cell.getDateCellValue();//java.util.Date类型  
  6. }  

       可以判断得到的Date是日期时间、日期还是时间,可以通过cell.getCellStyle().getDataFormat()来判断,这个返回值没有一个常量值来对应,我本机是excel2013,测试结果是日期时间(yyyy-MM-dd HH:mm:ss) - 22,日期(yyyy-MM-dd) - 14,时间(HH:mm:ss) - 21,年月(yyyy-MM) - 17,时分(HH:mm) - 20,月日(MM-dd) - 58,有了这个,可以根据数据库字段类型,处理之后再入库,相当不方便。

       另外,如果单元格数据格式是自定义的日期格式,那么通过DateUtil.isCellDateFormatted(cell)判断不出来,而且该单元格还是一个数值单元格,返回一个double值,这里比较2。针对这种方式,有两种解决方案,第一种,重写DateUtil.isCellDateFormatted(cell)方法,开源的都有源码;第二种,cell.getCellStyle().getDataFormatString()来判断,这个方法会返回格式字符串,通过这个字符串去匹配,再处理。

       3. 数据有效性

       很奇怪,POI能生成数据有效性(下拉列表),却得不到,或者说我没找到方法去得到,蛋疼。

       附单元格数据类型:

 

 

常量说明取值Cell.CELL_TYPE_NUMERIC数值类型cell.getNumericCellValue()
或cell.getDateCellValue()Cell.CELL_TYPE_STRING字符串类型cell.getStringCellValue()
或cell.toString()Cell.CELL_TYPE_BOOLEAN布尔类型cell.getBooleanCellValue()Cell.CELL_TYPE_FORMULA表达式类型cell.getCellFormula()Cell.CELL_TYPE_ERROR异常类型
不知道何时算异常cell.getErrorCellValue()Cell.CELL_TYPE_BLANK空,不知道何时算空空就不要取值了吧



POI操作Excel时偶尔会出现Cannot get a text value from a numeric cell的异常错误。

异常原因:Excel数据Cell有不同的类型,当我们试图从一个数字类型的Cell读取出一个字符串并写入数据库时,就会出现Cannot get a text value from a numeric cell的异常错误。

此异常常见于类似如下代码中:row.getCell(0).getStringCellValue();

解决办法:先设置Cell的类型,然后就可以把纯数字作为String类型读进来了:

if(row.getCell(0)!=null){
          row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
          stuUser.setPhone(row.getCell(0).getStringCellValue());
     }


http://blog.csdn.net/huazhangena/article/details/7587731

参考文章 http://www.open-open.com/lib/view/open1429847388213.html
http://webook-java.iteye.com/blog/1699621
0 0
原创粉丝点击