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. linux截图工具scrot

    SCROT截图工具 安装命令 sudo apt-get install scrot 截图命令使用说明: 1.抓取整个桌面:    scrot   ~/Pictures/pic1.jpg2.抓取窗口:  ...

  2. C++11的新类型转换方法

    转载自 http://blog.csdn.net/luoweifu/article/details/20493177 基于C++11标准 如果你用的编译器是基于最新的C++11标准,那么这个问题就变的 ...

  3. 黄聪:如何删除wordpress登录之后wp_footer、wp_head自行加载的Open Sans字体、fonts.googleapis.com连接导致卡死的问题

    有时候在浏览自己的WordPress网站时,发现网页长时间无响应, 卡在正在连接到fonts.googleapis.com ,如下图所示: 查看网页源码时,发现Head里面有如下一段代码: <l ...

  4. cf 605A Sorting Railway Cars 贪心 简单题

    其实就是求总长度 - 一个最长“连续”自序列的长度 最长“连续”自序列即一个最长的lis,并且这个lis的值刚好是连续的,比如4,5,6... 遍历一遍,贪心就是了 遍历到第i个时,此时值为a[i], ...

  5. NeHe OpenGL教程 第三十六课:从渲染到纹理

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  6. ci模板布局方式

    1.修改Loader链式加载header和footer方式 参考:http://stackoverflow.com/questions/9540576/header-and-footer-in-cod ...

  7. vim使用快捷键

    vim使用快捷键 索引 1. 关于Vim 1.1 Vim的几种模式 2. 启动Vim 3. 文档操作 4. 光标的移动 4.1 基本移动 4.2 翻屏 4.3 标记 5. 插入文本 5.1 基本插入 ...

  8. windows上传文件到linux

    1.在putty的网站上下载putty跟pscp 2.安装ssh跟putty sudo apt-get install openssh-server sudo apt-get install putt ...

  9. android实现 服务器功能

    package com.weijia.tests; import java.io.IOException; import java.net.InetSocketAddress; import java ...

  10. Java注解教程及自定义注解

    Java注解提供了关于代码的一些信息,但并不直接作用于它所注解的代码内容.在这个教程当中,我们将学习Java的注解,如何定制注解,注解的使用以及如何通过反射解析注解. Java1.5引入了注解,当前许 ...