Java生成和操作Excel文件

http://blog.csdn.net/softwave/article/details/38071825

Excel是我们平时工作中比较常用的用于存储二维表数据的,JAVA也可以直接对Excel进行操作,在这篇博客中将为大家介绍两种操作Excel的方式,分别为:jxl和poi。

对于两者的区别网上有测试如下:

测试结果 

类型   数据量(行)   执行时间(ms)   执行时间(ms)   执行时间(ms)   平均时间(ms) 
POI   1000       579       562       532       558 
JXL   1000       500       469       484       484 
POI   5000       984       984       969       979 
JXL   5000       922       860       890       891 
POI   10000      1609      1594      1641       1615 
JXL   10000        1437      1453      1406       1432 
POI   30000      3782      3765      3828       3792 
JXL   30000      3922      3906      3922       3917 
POI   50000      5953      6484      5859       6099 
JXL   50000      6765      7421      6984       7057 


JAVA EXCEL API  不支持excell2007以上 不介绍了

(二)poi

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>


HSSF和XSSF两种方式  excel2007 只能使用XSSF读写

  1. package com.tools.poi.lesson1;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.io.OutputStream;  
  10. import java.text.ParseException;  
  11. import java.text.SimpleDateFormat;  
  12. import java.util.ArrayList;  
  13. import java.util.List;  
  14.   
  15. import org.apache.poi.hssf.usermodel.HSSFCell;  
  16. import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
  17. import org.apache.poi.hssf.usermodel.HSSFRow;  
  18. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  19. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  20. import org.apache.poi.hssf.util.HSSFColor;  
  21. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;  
  22. import org.apache.poi.poifs.filesystem.POIFSFileSystem;  
  23. import org.apache.poi.ss.usermodel.Cell;  
  24. import org.apache.poi.ss.usermodel.CellStyle;  
  25. import org.apache.poi.ss.usermodel.Row;  
  26. import org.apache.poi.ss.usermodel.Sheet;  
  27. import org.apache.poi.ss.usermodel.Workbook;  
  28. import org.apache.poi.ss.usermodel.WorkbookFactory;  
  29. import org.apache.poi.ss.util.WorkbookUtil;  
  30.   
  31. import com.tools.poi.bean.Student;  
  32.   
  33. public class ExcelUtilWithXSSF {  
  34.     public static void main(String[] args) {  
  35.         try {  
  36.             getExcelAsFile("d:/FTP/系统报表.xls");  
  37.         } catch (FileNotFoundException e) {  
  38.             e.printStackTrace();  
  39.         } catch (IOException e) {  
  40.             e.printStackTrace();  
  41.         } catch (InvalidFormatException e) {  
  42.             e.printStackTrace();  
  43.         }  
  44.           
  45.           
  46. //      try {  
  47. //          CreateExcelDemo1();  
  48. //      } catch (ParseException e) {  
  49. //          e.printStackTrace();  
  50. //      }  
  51.           
  52.           
  53.     }  
  54.       
  55.     /** 
  56.      * 得到Excel,并解析内容  对2007及以上版本 使用XSSF解析 
  57.      * @param file 
  58.      * @throws FileNotFoundException 
  59.      * @throws IOException 
  60.      * @throws InvalidFormatException  
  61.      */  
  62.     public static void getExcelAsFile(String file) throws FileNotFoundException, IOException, InvalidFormatException{  
  63. //      //1.得到Excel常用对象  
  64. //      POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("d:/FTP/new1.xls"));  
  65. //      //2.得到Excel工作簿对象  
  66. //      HSSFWorkbook wb = new HSSFWorkbook(fs);  
  67.   
  68.           
  69.           
  70.         InputStream ins = null;     
  71.         Workbook wb = null;     
  72.             ins=new FileInputStream(new File(file));     
  73.             //ins= ExcelService.class.getClassLoader().getResourceAsStream(filePath);     
  74.             wb = WorkbookFactory.create(ins);     
  75.             ins.close();     
  76.           
  77.           
  78.         //3.得到Excel工作表对象  
  79.         Sheet sheet = wb.getSheetAt(0);  
  80.         //总行数  
  81.         int trLength = sheet.getLastRowNum();  
  82.         //4.得到Excel工作表的行  
  83.         Row row = sheet.getRow(0);  
  84.         //总列数  
  85.         int tdLength = row.getLastCellNum();  
  86.         //5.得到Excel工作表指定行的单元格  
  87.         Cell cell = row.getCell((short)1);  
  88.         //6.得到单元格样式  
  89.         CellStyle cellStyle = cell.getCellStyle();  
  90.   
  91.         for(int i=5;i<trLength;i++){  
  92.             //得到Excel工作表的行  
  93.             Row row1 = sheet.getRow(i);  
  94.             for(int j=0;j<tdLength;j++){  
  95.             //得到Excel工作表指定行的单元格  
  96.             Cell cell1 = row1.getCell(j);  
  97.             /** 
  98.              * 为了处理:Excel异常Cannot get a text value from a numeric cell 
  99.              * 将所有列中的内容都设置成String类型格式 
  100.              */  
  101.             if(cell1!=null){  
  102.                   cell1.setCellType(Cell.CELL_TYPE_STRING);  
  103.              }  
  104.               
  105.             if(j==5&&i<=10){  
  106.                 cell1.setCellValue("1000");  
  107.             }  
  108.               
  109.             //获得每一列中的值  
  110.             System.out.print(cell1+"                   ");  
  111.             }  
  112.             System.out.println();  
  113.         }  
  114.           
  115.         //将修改后的数据保存  
  116.         OutputStream out = new FileOutputStream(file);  
  117.                 wb.write(out);  
  118.     }  
  119.       
  120.       
  121.     /** 
  122.      * 创建Excel,并写入内容 
  123.      */  
  124.     public static void CreateExcel(){  
  125.           
  126.         //1.创建Excel工作薄对象  
  127.         HSSFWorkbook wb = new HSSFWorkbook();  
  128.         //2.创建Excel工作表对象       
  129.         HSSFSheet sheet = wb.createSheet("new Sheet");  
  130.         //3.创建Excel工作表的行     
  131.         HSSFRow row = sheet.createRow(6);  
  132.         //4.创建单元格样式  
  133.         CellStyle cellStyle =wb.createCellStyle();  
  134.           // 设置这些样式  
  135.         cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);  
  136.         cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
  137.         cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
  138.         cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
  139.         cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);  
  140.         cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);  
  141.         cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
  142.             
  143.             
  144.             
  145.         //5.创建Excel工作表指定行的单元格  
  146.         row.createCell(0).setCellStyle(cellStyle);  
  147.         //6.设置Excel工作表的值  
  148.         row.createCell(0).setCellValue("aaaa");  
  149.           
  150.         row.createCell(1).setCellStyle(cellStyle);  
  151.         row.createCell(1).setCellValue("bbbb");  
  152.           
  153.           
  154.         //设置sheet名称和单元格内容  
  155.         wb.setSheetName(0,"第一张工作表");  
  156.         //设置单元格内容   cell.setCellValue("单元格内容");  
  157.           
  158.         // 最后一步,将文件存到指定位置  
  159.                 try  
  160.                 {  
  161.                     FileOutputStream fout = new FileOutputStream("E:/students.xls");  
  162.                     wb.write(fout);  
  163.                     fout.close();  
  164.                 }  
  165.                 catch (Exception e)  
  166.                 {  
  167.                     e.printStackTrace();  
  168.                 }  
  169.     }  
  170.       
  171.     /** 
  172.      * 创建Excel的实例 
  173.      * @throws ParseException  
  174.      */  
  175.     public static void CreateExcelDemo1() throws ParseException{  
  176.         List list = new ArrayList();  
  177.         SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");  
  178.         Student user1 = new Student(1"张三"16,true, df.parse("1997-03-12"));  
  179.         Student user2 = new Student(2"李四"17,true, df.parse("1996-08-12"));  
  180.         Student user3 = new Student(3"王五"26,false, df.parse("1985-11-12"));  
  181.         list.add(user1);  
  182.         list.add(user2);  
  183.         list.add(user3);  
  184.           
  185.           
  186.         // 第一步,创建一个webbook,对应一个Excel文件  
  187.                 HSSFWorkbook wb = new HSSFWorkbook();  
  188.                 // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet  
  189.                 HSSFSheet sheet = wb.createSheet("学生表一");  
  190.                 // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
  191.                 HSSFRow row = sheet.createRow((int0);  
  192.                 // 第四步,创建单元格,并设置值表头 设置表头居中  
  193.                 HSSFCellStyle style = wb.createCellStyle();  
  194.                 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
  195.   
  196.                 HSSFCell cell = row.createCell((short0);  
  197.                 cell.setCellValue("学号");  
  198.                 cell.setCellStyle(style);  
  199.                 cell = row.createCell((short1);  
  200.                 cell.setCellValue("姓名");  
  201.                 cell.setCellStyle(style);  
  202.                 cell = row.createCell((short2);  
  203.                 cell.setCellValue("年龄");  
  204.                 cell.setCellStyle(style);  
  205.                 cell = row.createCell((short3);  
  206.                 cell.setCellValue("性别");  
  207.                 cell.setCellStyle(style);  
  208.                 cell = row.createCell((short4);  
  209.                 cell.setCellValue("生日");  
  210.                 cell.setCellStyle(style);  
  211.   
  212.                 // 第五步,写入实体数据 实际应用中这些数据从数据库得到,  
  213.   
  214.                 for (int i = 0; i < list.size(); i++)  
  215.                 {  
  216.                     row = sheet.createRow((int) i + 1);  
  217.                     Student stu = (Student) list.get(i);  
  218.                     // 第四步,创建单元格,并设置值  
  219.                     row.createCell((short0).setCellValue((double) stu.getId());  
  220.                     row.createCell((short1).setCellValue(stu.getName());  
  221.                     row.createCell((short2).setCellValue((double) stu.getAge());  
  222.                     row.createCell((short)3).setCellValue(stu.getSex()==true?"男":"女");  
  223.                     cell = row.createCell((short4);  
  224.                     cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu  
  225.                             .getBirthday()));  
  226.                 }  
  227.                 // 第六步,将文件存到指定位置  
  228.                 try  
  229.                 {  
  230.                     FileOutputStream fout = new FileOutputStream("E:/students.xls");  
  231.                     wb.write(fout);  
  232.                     fout.close();  
  233.                 }  
  234.                 catch (Exception e)  
  235.                 {  
  236.                     e.printStackTrace();  
  237.                 }  
  238.           
  239.           
  240.           
  241.     }  
  242. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值