Java使用POI导出到Excel

来源:互联网 发布:部落冲突男王数据 编辑:程序博客网 时间:2024/06/06 00:48

转载:http://blog.csdn.net/itchiang/article/details/8219842


使用POI。谷歌输入POI 下一个poi的jar包。

[java] view plain copy

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