java操作Excel--jxl与poj的比较

[java]  view plain  copy
 print ?
  1. package com.nexusy.excel.jxl;  
  2. import java.io.File;  
  3. import java.io.IOException;  
  4. import java.util.List;  
  5. import jxl.Cell;  
  6. import jxl.CellType;  
  7. import jxl.NumberCell;  
  8. import jxl.Sheet;  
  9. import jxl.Workbook;  
  10. import jxl.read.biff.BiffException;  
  11. import jxl.write.Label;  
  12. import jxl.write.Number;  
  13. import jxl.write.WritableSheet;  
  14. import jxl.write.WritableWorkbook;  
  15. import jxl.write.WriteException;  
  16. import jxl.write.biff.RowsExceededException;  
  17. import com.nexusy.excel.Record;  
  18. import com.nexusy.excel.TestUtil;  
  19. public class JXLTestMain {  
  20.       
  21.     private final static String filename = "jxltest.xls";  
  22.     private final static String[] headers = {"ID""标题""价格""数量""描述"};  
  23.       
  24.     private final static int rows = 65535;  
  25.     public static void main(String[] args) {  
  26.         writeExcel();  
  27.         readExcel();  
  28.     }  
  29.       
  30.     public static void writeExcel() {  
  31.         try {  
  32.             Thread.sleep(1000*20);  
  33.         } catch (InterruptedException e1) {  
  34.             e1.printStackTrace();  
  35.         }  
  36.         try {  
  37.             WritableWorkbook workbook = Workbook.createWorkbook(new File(filename));  
  38.               
  39.             WritableSheet  sheet = workbook.createSheet("jxl测试"0);  
  40.               
  41.             for (int i = 0; i < headers.length; i++) {  
  42.                 Label label = new Label(i, 0 , headers[i]);  
  43.                 sheet.addCell(label);  
  44.             }  
  45.               
  46.             List<Record> records = TestUtil.getRecords(rows);  
  47.             long s1 = System.nanoTime();  
  48.             int c = 1;  
  49.             for (Record record : records) {  
  50.                 sheet.addCell(new Number(0, c, record.getId()));  
  51.                 sheet.addCell(new Label(1, c, record.getTitle()));  
  52.                 sheet.addCell(new Number(2, c, record.getPrice()));  
  53.                 sheet.addCell(new Number(3, c, record.getQuantity()));  
  54.                 sheet.addCell(new Label(4, c, record.getDesc()));  
  55.                 c++;  
  56.             }  
  57.               
  58.             workbook.write();  
  59.             workbook.close();  
  60.             long s2 = System.nanoTime();  
  61.             System.out.println("jxl write " + rows + " rows to excel:" + (s2-s1));  
  62.         } catch (IOException e) {  
  63.             e.printStackTrace();  
  64.         } catch (RowsExceededException e) {  
  65.             e.printStackTrace();  
  66.         } catch (WriteException e) {  
  67.             e.printStackTrace();  
  68.         }  
  69.     }  
  70.     public static void readExcel() {  
  71.         try {  
  72.             Thread.sleep(1000*20);  
  73.         } catch (InterruptedException e1) {  
  74.             e1.printStackTrace();  
  75.         }  
  76.         try {  
  77.             long s1 = System.nanoTime();  
  78.             Workbook workbook = Workbook.getWorkbook(new File(filename));  
  79.             Sheet sheet = workbook.getSheet(0);  
  80.             System.out.println(sheet.getName());  
  81.             for(int i = 0; i < sheet.getRows(); i++){  
  82.                 Cell[] cells = sheet.getRow(i);  
  83.                 for (Cell cell : cells) {  
  84.                     if(cell.getType() == CellType.NUMBER){  
  85.                         System.out.print(((NumberCell)cell).getValue()+"  ");  
  86.                     } else if(cell.getType() == CellType.LABEL){  
  87.                         System.out.print(cell.getContents()+"  ");  
  88.                     }  
  89.                 }  
  90.                 System.out.println();  
  91.             }  
  92.             workbook.close();  
  93.             long s2 = System.nanoTime();  
  94.             System.out.println("jxl read " + rows + " rows from excel:" + (s2-s1));  
  95.         } catch (BiffException e) {  
  96.             e.printStackTrace();  
  97.         } catch (IOException e) {  
  98.             e.printStackTrace();  
  99.         }  
  100.     }  
  101. }  
  102.    
  103. package com.nexusy.excel.poi;  
  104. import java.io.FileInputStream;  
  105. import java.io.FileNotFoundException;  
  106. import java.io.FileOutputStream;  
  107. import java.io.IOException;  
  108. import java.io.InputStream;  
  109. import java.util.List;  
  110. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  111. import org.apache.poi.ss.usermodel.Cell;  
  112. import org.apache.poi.ss.usermodel.Row;  
  113. import org.apache.poi.ss.usermodel.Sheet;  
  114. import org.apache.poi.ss.usermodel.Workbook;  
  115. import com.nexusy.excel.Record;  
  116. import com.nexusy.excel.TestUtil;  
  117. public class POITestMain {  
  118.       
  119.     private final static String filename = "poitest.xls";  
  120.     private final static String[] headers = {"ID""标题""价格""数量""描述"};  
  121.       
  122.     private final static int rows = 65535;  
  123.     public static void main(String[] args) {  
  124.         writeExcel();  
  125.         readExcel();  
  126.     }  
  127.     public static void writeExcel() {  
  128.         try {  
  129.             Thread.sleep(1000*20);  
  130.         } catch (InterruptedException e1) {  
  131.             e1.printStackTrace();  
  132.         }  
  133.         Workbook wb = new HSSFWorkbook();  
  134.         try {  
  135.             FileOutputStream fileOut = new FileOutputStream(filename);  
  136.               
  137.             Sheet sheet = wb.createSheet("poi测试");  
  138.             Row row = sheet.createRow(0);  
  139.             for (int i = 0; i < headers.length; i++) {  
  140.                 row.createCell(i).setCellValue(headers[i]);  
  141.             }  
  142.               
  143.             List<Record> records = TestUtil.getRecords(rows);  
  144.             long s1 = System.nanoTime();  
  145.             int r = 1;  
  146.             for (Record record : records) {  
  147.                 row = sheet.createRow(r);  
  148.                 row.createCell(0).setCellValue(record.getId());  
  149.                 row.createCell(1).setCellValue(record.getTitle());  
  150.                 row.createCell(2).setCellValue(record.getPrice());  
  151.                 row.createCell(3).setCellValue(record.getQuantity());  
  152.                 row.createCell(4).setCellValue(record.getDesc());  
  153.                 r++;  
  154.             }  
  155.               
  156.             wb.write(fileOut);  
  157.             fileOut.close();  
  158.             long s2 = System.nanoTime();  
  159.             System.out.println("poi write " + rows + " rows to excel:" + (s2-s1));  
  160.         } catch (FileNotFoundException e) {  
  161.             e.printStackTrace();  
  162.         } catch (IOException e) {  
  163.             e.printStackTrace();  
  164.         }  
  165.     }  
  166.     public static void readExcel() {  
  167.         try {  
  168.             Thread.sleep(1000*20);  
  169.         } catch (InterruptedException e1) {  
  170.             e1.printStackTrace();  
  171.         }  
  172.         try {  
  173.             long s1 = System.nanoTime();  
  174.             InputStream inp = new FileInputStream(filename);  
  175.             Workbook wb = new HSSFWorkbook(inp);  
  176.             Sheet sheet = wb.getSheetAt(0);  
  177.             System.out.println(sheet.getSheetName());  
  178.               
  179.             for(Row row : sheet){  
  180.                 for(Cell cell : row){  
  181.                     switch (cell.getCellType()) {  
  182.                     case Cell.CELL_TYPE_NUMERIC:  
  183.                         System.out.print(cell.getNumericCellValue() + "  ");  
  184.                         break;  
  185.                     case Cell.CELL_TYPE_STRING:  
  186.                         System.out.print(cell.getStringCellValue() + "  ");  
  187.                         break;  
  188.                     default:  
  189.                         break;  
  190.                     }  
  191.                 }  
  192.                 System.out.println();  
  193.             }  
  194.             inp.close();  
  195.             long s2 = System.nanoTime();  
  196.             System.out.println("poi read " + rows + " rows from excel:" + (s2-s1));  
  197.         } catch (FileNotFoundException e) {  
  198.             e.printStackTrace();  
  199.         } catch (IOException e) {  
  200.             e.printStackTrace();  
  201.         }  
  202.   
  203.     }  
  204. }  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值