- 浏览: 223514 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
itlang01:
推荐一款动态添加日期的插件https://github.com ...
利用mysql触发器记录数据的插入和更新时间 -
daydayupzzc:
好,不错,清楚
Spring MVC的拦截器 -
zi_wu_xian:
不管用POI还是JXL都是第三的操作excel的组件,都有破坏 ...
java操作excel——jxl和poi比较 -
小头猎豹:
支持无私的分享,很有用,十分感谢!!
Maven之使用SVN版本号 -
superich2008:
写的很详细,正在找这个资料,多谢博主分享。
Maven之使用SVN版本号
最近需要需要项目开发需要从excel导入数据到数据库,于是就开始找开源的java操作excel的框架。貌似比较流行的有jxl和poi两个框架。网上有些对这两个框架比较的文章,但都不是最近的。根据项目需要,下面对jxl和poi读写excel的性能做个比较。
jxl:jxl-2.6.12.jar
poi:poi-3.8.jar
JXLTestMain.java
package com.nexusy.excel.jxl; import java.io.File; import java.io.IOException; import java.util.List; import jxl.Cell; import jxl.CellType; import jxl.NumberCell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import jxl.write.Label; import jxl.write.Number; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; import com.nexusy.excel.Record; import com.nexusy.excel.TestUtil; public class JXLTestMain { private final static String filename = "jxltest.xls"; private final static String[] headers = {"ID", "标题", "价格", "数量", "描述"}; private final static int rows = 65535; public static void main(String[] args) { writeExcel(); readExcel(); } public static void writeExcel() { try { Thread.sleep(1000*20); } catch (InterruptedException e1) { e1.printStackTrace(); } try { WritableWorkbook workbook = Workbook.createWorkbook(new File(filename)); WritableSheet sheet = workbook.createSheet("jxl测试", 0); for (int i = 0; i < headers.length; i++) { Label label = new Label(i, 0 , headers[i]); sheet.addCell(label); } List<Record> records = TestUtil.getRecords(rows); long s1 = System.nanoTime(); int c = 1; for (Record record : records) { sheet.addCell(new Number(0, c, record.getId())); sheet.addCell(new Label(1, c, record.getTitle())); sheet.addCell(new Number(2, c, record.getPrice())); sheet.addCell(new Number(3, c, record.getQuantity())); sheet.addCell(new Label(4, c, record.getDesc())); c++; } workbook.write(); workbook.close(); long s2 = System.nanoTime(); System.out.println("jxl write " + rows + " rows to excel:" + (s2-s1)); } catch (IOException e) { e.printStackTrace(); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } public static void readExcel() { try { Thread.sleep(1000*20); } catch (InterruptedException e1) { e1.printStackTrace(); } try { long s1 = System.nanoTime(); Workbook workbook = Workbook.getWorkbook(new File(filename)); Sheet sheet = workbook.getSheet(0); System.out.println(sheet.getName()); for(int i = 0; i < sheet.getRows(); i++){ Cell[] cells = sheet.getRow(i); for (Cell cell : cells) { if(cell.getType() == CellType.NUMBER){ System.out.print(((NumberCell)cell).getValue()+" "); } else if(cell.getType() == CellType.LABEL){ System.out.print(cell.getContents()+" "); } } System.out.println(); } workbook.close(); long s2 = System.nanoTime(); System.out.println("jxl read " + rows + " rows from excel:" + (s2-s1)); } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
POITestMain.java
package com.nexusy.excel.poi; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import com.nexusy.excel.Record; import com.nexusy.excel.TestUtil; public class POITestMain { private final static String filename = "poitest.xls"; private final static String[] headers = {"ID", "标题", "价格", "数量", "描述"}; private final static int rows = 65535; public static void main(String[] args) { writeExcel(); readExcel(); } public static void writeExcel() { try { Thread.sleep(1000*20); } catch (InterruptedException e1) { e1.printStackTrace(); } Workbook wb = new HSSFWorkbook(); try { FileOutputStream fileOut = new FileOutputStream(filename); Sheet sheet = wb.createSheet("poi测试"); Row row = sheet.createRow(0); for (int i = 0; i < headers.length; i++) { row.createCell(i).setCellValue(headers[i]); } List<Record> records = TestUtil.getRecords(rows); long s1 = System.nanoTime(); int r = 1; for (Record record : records) { row = sheet.createRow(r); row.createCell(0).setCellValue(record.getId()); row.createCell(1).setCellValue(record.getTitle()); row.createCell(2).setCellValue(record.getPrice()); row.createCell(3).setCellValue(record.getQuantity()); row.createCell(4).setCellValue(record.getDesc()); r++; } wb.write(fileOut); fileOut.close(); long s2 = System.nanoTime(); System.out.println("poi write " + rows + " rows to excel:" + (s2-s1)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void readExcel() { try { Thread.sleep(1000*20); } catch (InterruptedException e1) { e1.printStackTrace(); } try { long s1 = System.nanoTime(); InputStream inp = new FileInputStream(filename); Workbook wb = new HSSFWorkbook(inp); Sheet sheet = wb.getSheetAt(0); System.out.println(sheet.getSheetName()); for(Row row : sheet){ for(Cell cell : row){ switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: System.out.print(cell.getNumericCellValue() + " "); break; case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue() + " "); break; default: break; } } System.out.println(); } inp.close(); long s2 = System.nanoTime(); System.out.println("poi read " + rows + " rows from excel:" + (s2-s1)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
测试结果如下:
jxl写入65535行数据
jxl读取65535行数据:
poi写入65535行数据:
poi读取65535行数据:
评论
11 楼
zi_wu_xian
2016-09-02
不管用POI还是JXL都是第三的操作excel的组件,都有破坏excel文件格式的可能,还是用PageOffice动态生成excel文件,操作excel文件中的数据比较好,因为是调用的Office自己的VBA接口,所以不会有格式问题。
10 楼
lanhuidong
2014-03-29
weimeili8211 写道
我用poi,jxl都 会内存溢出
9 楼
weimeili8211
2014-03-28
我用poi,jxl都 会内存溢出
8 楼
tony_jingzhou
2013-10-16
tangzililiang 写道
这样比较太片面了哦,如果尝试用POI的事件驱动模式读取我相信内存占用会大大降低,JXL将文件一次性加载到内存,读取大文件是短板(原理可参照xml文件读取方式dom和sax的区别)
采用事件模型,poi读取10M以上的Excel 还是会内存溢出?
7 楼
tangzililiang
2013-07-01
这样比较太片面了哦,如果尝试用POI的事件驱动模式读取我相信内存占用会大大降低,JXL将文件一次性加载到内存,读取大文件是短板(原理可参照xml文件读取方式dom和sax的区别)
6 楼
lanhuidong
2013-05-09
bamboo_prince 写道
好像效率差不多嘛,你推荐用哪种啊?
我自己选择了poi
5 楼
bamboo_prince
2013-05-08
好像效率差不多嘛,你推荐用哪种啊?
4 楼
lanhuidong
2013-05-02
winerlinwen1 写道
我怎么读到2760行就爆了!我用poi操作的!
什么爆了?
3 楼
winerlinwen1
2013-04-28
我怎么读到2760行就爆了!我用poi操作的!
2 楼
lanhuidong
2012-09-10
zhangjian420 写道
这个是什么工具监视的!
jvisualvm,jdk自带的工具,在bin目录中。兄弟可有更好的监视工具推荐?
1 楼
zhangjian420
2012-07-24
这个是什么工具监视的!
发表评论
-
Java日志系统-logback配置
2013-10-30 19:52 4739logback配置文件:logback.xml < ... -
关于ThreadPoolExecutor的理解
2013-09-03 16:07 845做Java也有4年了,原来对于ThreadPoolExec ... -
Garbage-First Collector
2013-01-30 21:51 1176从JDK7 update4开始完全支持G1垃圾收集器,这是一 ... -
JDK自带工具
2013-01-30 10:21 01.Java Virtual Machine Process ... -
JAVA定时器
2013-01-20 23:14 5463最近有同事提出在项目中使用JDK自带的定时器,之前的任务调度 ... -
开发环境配置
2012-09-11 11:50 53将自己的开发环境安装及配置过程写成文件,供自己或他人参考。 -
异常没有打印栈信息
2012-07-04 21:36 1568最近上的项目bug很多,服务器经常爆java.lang.Nul ... -
SecurityManager使用
2011-06-15 14:49 2017因为只允许指定IP的服务器访问RMI,因此研究了Securit ... -
Java远程方法调用(RMI)
2011-05-28 12:42 3251Java远程方法调用(Java Remote Method I ... -
关于String连接(+运算符)
2011-03-08 20:54 26081.代码 package lan.jdk.study; ... -
HotSpot VM 内存管理
2011-02-22 21:18 11611.HotSpot VM的内存有三个g ... -
The Java HotSpot VM's GC
2011-02-18 15:04 1064GC的主要特点: 一、精 ... -
Java HotSpot
2011-02-17 17:16 0JDK中包含两种风格的虚拟机:Client VM and Se ... -
maven在eclipse中报错问题解决
2010-07-17 10:03 1074Eclipse is running in a J ... -
Java中的for和for-each效率问题
2010-06-22 11:59 1322今天看到Effective Java中说foreach比for ... -
Integer之==
2010-05-12 20:41 1003package lan; public class T ...
相关推荐
jxl.jar和poi3.8,还有common-io.jar等等,全部亲测使用,最全jar包集合
Excel生成导出JXL和POI两种方式小demo
NULL 博文链接:https://yuanyuan7891.iteye.com/blog/656388
操作Excel文件(读取和生成)jxl和poi 详细实例
java 操作Excel jxl poi 有例子
JAVA操作Excel(jxl使用介绍).pdf
java实现excel的导入导出(poi详解),经过测试poi效率要比jxl要高很多,特别是数据量大的时候jxl根本无法用肉眼入目,本资源是个可运行项目demo,很有参考价值!
POI与JXL的实战性能对比
java报表JXL和POI打印设置 设置打印,非常好用 一般文档没有 绝版
jxl excel java excel导入数据库 java操作excel 数据库是mysql
java直接操作excel文件,JXL是韩国的以为开发人员编写,与poi相比功能是简单了点,但是对于普通的一些开发是完全可以满足需求
Java操作Excel(poi、jxl 包介绍)
Java操作Excel之JXL,实际的操作实例。对开发excel很很有用哦
包含Java操作Excel的jxl.jar包
Excel导入导出 jxl及Poi 工具类:jxl 实现 及 Poi实现 非常实用
jxl poi java操作excel
java操作Excel的jar文件及其API——jxl包
ExcelDemo_Excel导出_下载_POI读excel_JXL读写excel_java读写excel_列宽_读取合并或拆分单元格内容
poi是一个Java api组件,用于处理execel和doc文件 jxl可以读取,创建,更新Excel文件,使用该api非windows系统也可以通过纯java来处理Excel。