JXL和POI导出数据方式的比较

POI支持excel2003和2007,而jxl只支持excel2003。

下面为测试代码:

  1. public class TestCondition {
  2. /**
  3. * 生成的记录条数
  4. */
  5. public static final int RECORD_COUNT = 21000;
  6. /**
  7. * 模板文件
  8. */
  9. public static final String TEMPLATE_FILE = "E:/MyKernelPlatformWorkspace/Template/query_order.xls";
  10. /**
  11. * JXL生成文件位置
  12. */
  13. public static final String JXL_TARGET_FILE_NAME = "E:/MyKernelPlatformWorkspace/Template/target/jxl_order.xls";
  14. /**
  15. * POI生成文件位置
  16. */
  17. public static final String POI_TARGET_FILE_NAME = "E:/MyKernelPlatformWorkspace/Template/target/poi_order.xls";
  18. /**
  19. * JXL临时文件位置
  20. */
  21. public static final String JXL_TEMP_DIR = "E:/MyKernelPlatformWorkspace/Template/temp";
  22. }

然后在此测试条件下编写JXL和POI的测试类,首先是JXL的

  1. public class JXLExcel {
  2. /**
  3. * 起始行
  4. */
  5. private static final int start_row = 3;
  6. private WorkbookSettings settings;
  7. private File target;
  8. public JXLExcel() {
  9. this.settings = new WorkbookSettings();
  10. //设定JXL在生成excel文件时使用临时文件
  11. settings.setUseTemporaryFileDuringWrite(true);
  12. settings.setTemporaryFileDuringWriteDirectory(new File(TestCondition.JXL_TEMP_DIR));
  13. this.target = new File(TestCondition.JXL_TARGET_FILE_NAME);
  14. }
  15. public void execute() throws Exception {
  16. // 读入模板文件
  17. Workbook template = Workbook.getWorkbook(new File(TestCondition.TEMPLATE_FILE));
  18. WritableWorkbook worbook = Workbook.createWorkbook(target, template, settings);
  19. // 获取第一个sheet
  20. WritableSheet sheet = worbook.getSheet(0);
  21. Random random = new Random();
  22. // 循环写入数据
  23. for(int i = 0;i < TestCondition.RECORD_COUNT;i++) {
  24. int row = i + start_row;
  25. sheet.insertRow(row);
  26. Label col1 = new Label(0, row, String.valueOf(i + 1));
  27. Label col2 = new Label(1, row, String.valueOf(random.nextLong()));
  28. Label col3 = new Label(2, row, String.valueOf(random.nextLong()));
  29. Label col4 = new Label(3, row, "merchant" + (i +1));
  30. jxl.write.Number col5 = new Number(4, row, random.nextDouble());
  31. jxl.write.Number col6 = new Number(5, row, random.nextDouble());
  32. jxl.write.Number col7 = new Number(6, row, random.nextDouble());
  33. jxl.write.Number col8 = new Number(7, row, random.nextDouble());
  34. Label col9 = new Label(8, row, String.valueOf(random.nextLong()));
  35. Label col10 = new Label(9, row, "PAY");
  36. Label col11 = new Label(10, row, "POS");
  37. Label col12 = new Label(11, row, "2010-09-03 12:45:13");
  38. Label col13 = new Label(12, row, "2010-09-09 12:45:13");
  39. Label col14 = new Label(13, row, "interface" + (i + 1));
  40. Label col15 = new Label(14, row, "18701001830");
  41. Label col16 = new Label(15, row, "ccbc");
  42. Label col17 = new Label(16, row, String.valueOf(random.nextLong()));
  43. Label col18 = new Label(17, row, String.valueOf(random.nextLong()));
  44. jxl.write.Number col19 = new Number(18, row, random.nextDouble());
  45. jxl.write.Number col20 = new Number(19, row, random.nextDouble());
  46. Label col21 = new Label(20, row, "payer" + (i + 1));
  47. Label col22 = new Label(21, row, String.valueOf(random.nextLong()));
  48. Label col23 = new Label(22, row, "192.168.1.1");
  49. Label col24 = new Label(23, row, "192.168.1.1");
  50. sheet.addCell(col1);
  51. sheet.addCell(col2);
  52. sheet.addCell(col3);
  53. sheet.addCell(col4);
  54. sheet.addCell(col5);
  55. sheet.addCell(col6);
  56. sheet.addCell(col7);
  57. sheet.addCell(col8);
  58. sheet.addCell(col9);
  59. sheet.addCell(col10);
  60. sheet.addCell(col11);
  61. sheet.addCell(col12);
  62. sheet.addCell(col13);
  63. sheet.addCell(col14);
  64. sheet.addCell(col15);
  65. sheet.addCell(col16);
  66. sheet.addCell(col17);
  67. sheet.addCell(col18);
  68. sheet.addCell(col19);
  69. sheet.addCell(col20);
  70. sheet.addCell(col21);
  71. sheet.addCell(col22);
  72. sheet.addCell(col23);
  73. sheet.addCell(col24);
  74. }
  75. worbook.write();
  76. worbook.close();
  77. }
  78. }

执行Main函数

  1. public class JXLMain {
  2. /**
  3. * 描述:
  4. * @param args
  5. * @throws Exception
  6. */
  7. public static void main(String[] args) throws Exception {
  8. long jxlStart = System.currentTimeMillis();
  9. JXLExcel jxl = new JXLExcel();
  10. jxl.execute();
  11. long jxlStop = System.currentTimeMillis();
  12. System.out.println("jxl takes : " + (jxlStop - jxlStart)/1000 + " seconds.");
  13. }

然后是POI的

public class POIExcel {

  1. /**
  2. * 起始行
  3. */
  4. private static final int start_row = 3;
  5. public void execute() throws Exception {
  6. // 读入模板文件
  7. InputStream is = new FileInputStream(TestCondition.TEMPLATE_FILE);
  8. POIFSFileSystem poifsFileSystem = new POIFSFileSystem(is);
  9. HSSFWorkbook workbook = new HSSFWorkbook(poifsFileSystem);
  10. // 获取第一个sheet
  11. HSSFSheet sheet = workbook.getSheetAt(0);
  12. Random random = new Random();
  13. // 将模板的最后两行移动
  14. sheet.shiftRows(3, 4, TestCondition.RECORD_COUNT);
  15. OutputStream os = new FileOutputStream(
  16. TestCondition.POI_TARGET_FILE_NAME);
  17. // 循环写入数据
  18. for (int i = 0; i < TestCondition.RECORD_COUNT; i++) {
  19. int rowNum = i + start_row;
  20. HSSFRow row = sheet.createRow(rowNum);
  21. HSSFCell cell1 = row.createCell(0);
  22. cell1.setCellValue(i + 1);
  23. HSSFCell cell2 = row.createCell(1);
  24. cell2.setCellValue(String.valueOf(random.nextLong()));
  25. HSSFCell cell3 = row.createCell(2);
  26. cell3.setCellValue(String.valueOf(random.nextLong()));
  27. HSSFCell cell4 = row.createCell(3);
  28. cell4.setCellValue("merchant" + (i +1));
  29. HSSFCell cell5 = row.createCell(4);
  30. cell5.setCellValue(random.nextDouble());
  31. HSSFCell cell6 = row.createCell(5);
  32. cell6.setCellValue(random.nextDouble());
  33. HSSFCell cell7 = row.createCell(6);
  34. cell7.setCellValue(random.nextDouble());
  35. HSSFCell cell8 = row.createCell(7);
  36. cell8.setCellValue(random.nextDouble());
  37. HSSFCell cell9 = row.createCell(8);
  38. cell9.setCellValue(String.valueOf(random.nextLong()));
  39. HSSFCell cell10 = row.createCell(9);
  40. cell10.setCellValue("PAY");
  41. HSSFCell cell11 = row.createCell(10);
  42. cell11.setCellValue("POS");
  43. HSSFCell cell12 = row.createCell(11);
  44. cell12.setCellValue(new Date());
  45. HSSFCell cell13 = row.createCell(12);
  46. cell13.setCellValue(new Date());
  47. HSSFCell cell14 = row.createCell(13);
  48. cell14.setCellValue("interface" + (i + 1));
  49. HSSFCell cell15 = row.createCell(14);
  50. cell15.setCellValue("18701001830");
  51. HSSFCell cell16 = row.createCell(15);
  52. cell16.setCellValue("ccbc");
  53. HSSFCell cell17 = row.createCell(16);
  54. cell17.setCellValue(String.valueOf(random.nextLong()));
  55. HSSFCell cell18 = row.createCell(17);
  56. cell18.setCellValue(String.valueOf(random.nextLong()));
  57. HSSFCell cell19 = row.createCell(18);
  58. cell19.setCellValue(random.nextDouble());
  59. HSSFCell cell20 = row.createCell(19);
  60. cell20.setCellValue(random.nextDouble());
  61. HSSFCell cell21 = row.createCell(20);
  62. cell21.setCellValue("payer" + (i + 1));
  63. HSSFCell cell22 = row.createCell(21);
  64. cell22.setCellValue(String.valueOf(random.nextLong()));
  65. HSSFCell cell23 = row.createCell(22);
  66. cell23.setCellValue("192.168.1.1");
  67. HSSFCell cell24 = row.createCell(23);
  68. cell24.setCellValue("192.168.1.1");
  69. }
  70. workbook.write(os);
  71. os.close();
  72. }

执行Main函数

  1. public class POIMain {
  2. /**
  3. * 描述:
  4. * @param args
  5. * @throws Exception
  6. */
  7. public static void main(String[] args) throws Exception {
  8. long jxlStart = System.currentTimeMillis();
  9. POIExcel poi = new POIExcel();
  10. poi.execute();
  11. long jxlStop = System.currentTimeMillis();
  12. System.out.println("poi takes : " + (jxlStop - jxlStart)/1000 + " seconds.");
  13. }
  14. }

转:http://tianwenbo.iteye.com/blog/1485654

Java导出数据为EXCEL的两种方式JXL和POI的更多相关文章

  1. mysql导出数据到excel的两种方式

    使用第一种方式如果数据中有换行符的话会自动换行,但使用第二种方式就不会出现这种效果了.两种方式自己选择哈 1:select * from into outfile 'c:/Users/a.xls' t ...

  2. Java EXCEL导入的两种方式JXL和POI

    Excel导入有两个方法:JXL 和POI 1.JXL解析Excel public class JxlReadExcel { /**     * JXL解析Excel     * @author Da ...

  3. java分段加载数据,循环和递归两种方式

    package org.jimmy.autosearch2019.test; import java.util.ArrayList; public class Test20190328 { priva ...

  4. Delphi 导出数据至Excel的7种方法【转】

    一; delphi 快速导出excel   uses ComObj,clipbrd;   function ToExcel(sfilename:string; ADOQuery:TADOQuery): ...

  5. Delphi 导出数据至Excel的7种方法

    一; delphi 快速导出excel uses ComObj,clipbrd; function ToExcel(sfilename:string; ADOQuery:TADOQuery):bool ...

  6. Java并发--线程间协作的两种方式:wait、notify、notifyAll和Condition

    在前面我们将了很多关于同步的问题,然而在现实中,需要线程之间的协作.比如说最经典的生产者-消费者模型:当队列满时,生产者需要等待队列有空间才能继续往里面放入商品,而在等待的期间内,生产者必须释放对临界 ...

  7. 遍历Map集合:java.util.Map.Entry、KeySet两种方式

    遍历Map集合的两种方式: 1.用KeySet Map.keySet(),返回一个存放所有key的set集合,通过遍历集合,根据key值取出所有的value值. Map<String,Strin ...

  8. Android提交数据到服务器的两种方式四种方法

    本帖最后由 yanghe123 于 2012-6-7 09:58 编辑 Android应用开发中,会经常要提交数据到服务器和从服务器得到数据,本文主要是给出了利用http协议采用HttpClient方 ...

  9. python利用mongodb上传图片数据 : GridFS 与 bson两种方式

    利用mongodb保存图片通常有两种方法,一种是将图片数据转化为二进制作为字典的键值对进行保存,另一种是利用mongodb提供的GridFS进行保存,两者各有利弊.性能方面的优劣未曾测试,无法进行评价 ...

随机推荐

  1. RESTful API 简书

    RESTful API 概述 参考地址 RESTful架构是一种流行的互联网软件架构,它结构清晰,符合标准,易于理解,扩展方便.REST是Representational State Transfer ...

  2. 【Linux】之系统工具top

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来不断刷新 ...

  3. 黄聪:MYSQL提交一批ID,查询数据库中不存在的ID并返回

    假设你数据库有个A表: ID NAME    1 aaa    2 bbb    3 ccc    4 ddd 需求:给你几个ID,返回A表中不存在的ID? 例如提交1,2,8,9 返回8,9 sel ...

  4. Cpk

    CPK:Complex Process Capability index 的缩写,是现代企业用于表示制程能力的指标.制程能力是过程性能的允许最大变化范围与过程的正常偏差的比值.制程能力研究在於确认这些 ...

  5. jetty-run运行报错的原因

  6. JAVA 处理程序异常,(try、catch、finally),(thorws)

    一.try语句: try{//这里写可能出现异常的程序} catch(Exception e){//这里写如果出现异常怎么处理的程序} 二.throws语句 语法:函数方法()  throws Exc ...

  7. js实现的新闻列表垂直滚动实现详解

    js实现的新闻列表垂直滚动实现详解:新闻列表垂直滚动效果在大量的网站都有应用,有点自然是不言而喻的,首先由于网页的空间有限,使用滚动代码可以使用最小的空间提供更多的信息量,还有让网页有了动态的效果,更 ...

  8. Ubuntu编写开机自启动脚本(转载)

    From:http://blog.csdn.net/marujunyy/article/details/8466255 1.首先编写一个简单的shell脚本test.sh #! /bin/bash e ...

  9. 要件审判九步法及其基本价值 z

    要件审判九步法及其基本价值 发布时间:2014-12-24 14:29:05 作者介绍 邹碧华,男,1967年出生于江西奉新,毕业于北京大学法学院,获法学博士学位.上海市高级人民法院副院长.2006年 ...

  10. 程序员遇到Bug时的30个反应

    开发应用程序是一个非常有压力的工作.没有人是完美的,因此在这个行业中,代码中出现bug是相当普遍的现象.面对bug,一些程序员会生气,会沮丧,会心烦意乱,甚至会灰心丧气,而另一些程序员会依然保持冷静沉 ...