Java导出数据为EXCEL的两种方式JXL和POI
JXL和POI导出数据方式的比较
POI支持excel2003和2007,而jxl只支持excel2003。
下面为测试代码:
- public class TestCondition {
- /**
- * 生成的记录条数
- */
- public static final int RECORD_COUNT = 21000;
- /**
- * 模板文件
- */
- public static final String TEMPLATE_FILE = "E:/MyKernelPlatformWorkspace/Template/query_order.xls";
- /**
- * JXL生成文件位置
- */
- public static final String JXL_TARGET_FILE_NAME = "E:/MyKernelPlatformWorkspace/Template/target/jxl_order.xls";
- /**
- * POI生成文件位置
- */
- public static final String POI_TARGET_FILE_NAME = "E:/MyKernelPlatformWorkspace/Template/target/poi_order.xls";
- /**
- * JXL临时文件位置
- */
- public static final String JXL_TEMP_DIR = "E:/MyKernelPlatformWorkspace/Template/temp";
- }
然后在此测试条件下编写JXL和POI的测试类,首先是JXL的
- public class JXLExcel {
- /**
- * 起始行
- */
- private static final int start_row = 3;
- private WorkbookSettings settings;
- private File target;
- public JXLExcel() {
- this.settings = new WorkbookSettings();
- //设定JXL在生成excel文件时使用临时文件
- settings.setUseTemporaryFileDuringWrite(true);
- settings.setTemporaryFileDuringWriteDirectory(new File(TestCondition.JXL_TEMP_DIR));
- this.target = new File(TestCondition.JXL_TARGET_FILE_NAME);
- }
- public void execute() throws Exception {
- // 读入模板文件
- Workbook template = Workbook.getWorkbook(new File(TestCondition.TEMPLATE_FILE));
- WritableWorkbook worbook = Workbook.createWorkbook(target, template, settings);
- // 获取第一个sheet
- WritableSheet sheet = worbook.getSheet(0);
- Random random = new Random();
- // 循环写入数据
- for(int i = 0;i < TestCondition.RECORD_COUNT;i++) {
- int row = i + start_row;
- sheet.insertRow(row);
- Label col1 = new Label(0, row, String.valueOf(i + 1));
- Label col2 = new Label(1, row, String.valueOf(random.nextLong()));
- Label col3 = new Label(2, row, String.valueOf(random.nextLong()));
- Label col4 = new Label(3, row, "merchant" + (i +1));
- jxl.write.Number col5 = new Number(4, row, random.nextDouble());
- jxl.write.Number col6 = new Number(5, row, random.nextDouble());
- jxl.write.Number col7 = new Number(6, row, random.nextDouble());
- jxl.write.Number col8 = new Number(7, row, random.nextDouble());
- Label col9 = new Label(8, row, String.valueOf(random.nextLong()));
- Label col10 = new Label(9, row, "PAY");
- Label col11 = new Label(10, row, "POS");
- Label col12 = new Label(11, row, "2010-09-03 12:45:13");
- Label col13 = new Label(12, row, "2010-09-09 12:45:13");
- Label col14 = new Label(13, row, "interface" + (i + 1));
- Label col15 = new Label(14, row, "18701001830");
- Label col16 = new Label(15, row, "ccbc");
- Label col17 = new Label(16, row, String.valueOf(random.nextLong()));
- Label col18 = new Label(17, row, String.valueOf(random.nextLong()));
- jxl.write.Number col19 = new Number(18, row, random.nextDouble());
- jxl.write.Number col20 = new Number(19, row, random.nextDouble());
- Label col21 = new Label(20, row, "payer" + (i + 1));
- Label col22 = new Label(21, row, String.valueOf(random.nextLong()));
- Label col23 = new Label(22, row, "192.168.1.1");
- Label col24 = new Label(23, row, "192.168.1.1");
- sheet.addCell(col1);
- sheet.addCell(col2);
- sheet.addCell(col3);
- sheet.addCell(col4);
- sheet.addCell(col5);
- sheet.addCell(col6);
- sheet.addCell(col7);
- sheet.addCell(col8);
- sheet.addCell(col9);
- sheet.addCell(col10);
- sheet.addCell(col11);
- sheet.addCell(col12);
- sheet.addCell(col13);
- sheet.addCell(col14);
- sheet.addCell(col15);
- sheet.addCell(col16);
- sheet.addCell(col17);
- sheet.addCell(col18);
- sheet.addCell(col19);
- sheet.addCell(col20);
- sheet.addCell(col21);
- sheet.addCell(col22);
- sheet.addCell(col23);
- sheet.addCell(col24);
- }
- worbook.write();
- worbook.close();
- }
- }
执行Main函数
- public class JXLMain {
- /**
- * 描述:
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- long jxlStart = System.currentTimeMillis();
- JXLExcel jxl = new JXLExcel();
- jxl.execute();
- long jxlStop = System.currentTimeMillis();
- System.out.println("jxl takes : " + (jxlStop - jxlStart)/1000 + " seconds.");
- }
然后是POI的
public class POIExcel {
- /**
- * 起始行
- */
- private static final int start_row = 3;
- public void execute() throws Exception {
- // 读入模板文件
- InputStream is = new FileInputStream(TestCondition.TEMPLATE_FILE);
- POIFSFileSystem poifsFileSystem = new POIFSFileSystem(is);
- HSSFWorkbook workbook = new HSSFWorkbook(poifsFileSystem);
- // 获取第一个sheet
- HSSFSheet sheet = workbook.getSheetAt(0);
- Random random = new Random();
- // 将模板的最后两行移动
- sheet.shiftRows(3, 4, TestCondition.RECORD_COUNT);
- OutputStream os = new FileOutputStream(
- TestCondition.POI_TARGET_FILE_NAME);
- // 循环写入数据
- for (int i = 0; i < TestCondition.RECORD_COUNT; i++) {
- int rowNum = i + start_row;
- HSSFRow row = sheet.createRow(rowNum);
- HSSFCell cell1 = row.createCell(0);
- cell1.setCellValue(i + 1);
- HSSFCell cell2 = row.createCell(1);
- cell2.setCellValue(String.valueOf(random.nextLong()));
- HSSFCell cell3 = row.createCell(2);
- cell3.setCellValue(String.valueOf(random.nextLong()));
- HSSFCell cell4 = row.createCell(3);
- cell4.setCellValue("merchant" + (i +1));
- HSSFCell cell5 = row.createCell(4);
- cell5.setCellValue(random.nextDouble());
- HSSFCell cell6 = row.createCell(5);
- cell6.setCellValue(random.nextDouble());
- HSSFCell cell7 = row.createCell(6);
- cell7.setCellValue(random.nextDouble());
- HSSFCell cell8 = row.createCell(7);
- cell8.setCellValue(random.nextDouble());
- HSSFCell cell9 = row.createCell(8);
- cell9.setCellValue(String.valueOf(random.nextLong()));
- HSSFCell cell10 = row.createCell(9);
- cell10.setCellValue("PAY");
- HSSFCell cell11 = row.createCell(10);
- cell11.setCellValue("POS");
- HSSFCell cell12 = row.createCell(11);
- cell12.setCellValue(new Date());
- HSSFCell cell13 = row.createCell(12);
- cell13.setCellValue(new Date());
- HSSFCell cell14 = row.createCell(13);
- cell14.setCellValue("interface" + (i + 1));
- HSSFCell cell15 = row.createCell(14);
- cell15.setCellValue("18701001830");
- HSSFCell cell16 = row.createCell(15);
- cell16.setCellValue("ccbc");
- HSSFCell cell17 = row.createCell(16);
- cell17.setCellValue(String.valueOf(random.nextLong()));
- HSSFCell cell18 = row.createCell(17);
- cell18.setCellValue(String.valueOf(random.nextLong()));
- HSSFCell cell19 = row.createCell(18);
- cell19.setCellValue(random.nextDouble());
- HSSFCell cell20 = row.createCell(19);
- cell20.setCellValue(random.nextDouble());
- HSSFCell cell21 = row.createCell(20);
- cell21.setCellValue("payer" + (i + 1));
- HSSFCell cell22 = row.createCell(21);
- cell22.setCellValue(String.valueOf(random.nextLong()));
- HSSFCell cell23 = row.createCell(22);
- cell23.setCellValue("192.168.1.1");
- HSSFCell cell24 = row.createCell(23);
- cell24.setCellValue("192.168.1.1");
- }
- workbook.write(os);
- os.close();
- }
执行Main函数
- public class POIMain {
- /**
- * 描述:
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- long jxlStart = System.currentTimeMillis();
- POIExcel poi = new POIExcel();
- poi.execute();
- long jxlStop = System.currentTimeMillis();
- System.out.println("poi takes : " + (jxlStop - jxlStart)/1000 + " seconds.");
- }
- }
转:http://tianwenbo.iteye.com/blog/1485654
Java导出数据为EXCEL的两种方式JXL和POI的更多相关文章
- mysql导出数据到excel的两种方式
使用第一种方式如果数据中有换行符的话会自动换行,但使用第二种方式就不会出现这种效果了.两种方式自己选择哈 1:select * from into outfile 'c:/Users/a.xls' t ...
- Java EXCEL导入的两种方式JXL和POI
Excel导入有两个方法:JXL 和POI 1.JXL解析Excel public class JxlReadExcel { /** * JXL解析Excel * @author Da ...
- java分段加载数据,循环和递归两种方式
package org.jimmy.autosearch2019.test; import java.util.ArrayList; public class Test20190328 { priva ...
- Delphi 导出数据至Excel的7种方法【转】
一; delphi 快速导出excel uses ComObj,clipbrd; function ToExcel(sfilename:string; ADOQuery:TADOQuery): ...
- Delphi 导出数据至Excel的7种方法
一; delphi 快速导出excel uses ComObj,clipbrd; function ToExcel(sfilename:string; ADOQuery:TADOQuery):bool ...
- Java并发--线程间协作的两种方式:wait、notify、notifyAll和Condition
在前面我们将了很多关于同步的问题,然而在现实中,需要线程之间的协作.比如说最经典的生产者-消费者模型:当队列满时,生产者需要等待队列有空间才能继续往里面放入商品,而在等待的期间内,生产者必须释放对临界 ...
- 遍历Map集合:java.util.Map.Entry、KeySet两种方式
遍历Map集合的两种方式: 1.用KeySet Map.keySet(),返回一个存放所有key的set集合,通过遍历集合,根据key值取出所有的value值. Map<String,Strin ...
- Android提交数据到服务器的两种方式四种方法
本帖最后由 yanghe123 于 2012-6-7 09:58 编辑 Android应用开发中,会经常要提交数据到服务器和从服务器得到数据,本文主要是给出了利用http协议采用HttpClient方 ...
- python利用mongodb上传图片数据 : GridFS 与 bson两种方式
利用mongodb保存图片通常有两种方法,一种是将图片数据转化为二进制作为字典的键值对进行保存,另一种是利用mongodb提供的GridFS进行保存,两者各有利弊.性能方面的优劣未曾测试,无法进行评价 ...
随机推荐
- RESTful API 简书
RESTful API 概述 参考地址 RESTful架构是一种流行的互联网软件架构,它结构清晰,符合标准,易于理解,扩展方便.REST是Representational State Transfer ...
- 【Linux】之系统工具top
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来不断刷新 ...
- 黄聪:MYSQL提交一批ID,查询数据库中不存在的ID并返回
假设你数据库有个A表: ID NAME 1 aaa 2 bbb 3 ccc 4 ddd 需求:给你几个ID,返回A表中不存在的ID? 例如提交1,2,8,9 返回8,9 sel ...
- Cpk
CPK:Complex Process Capability index 的缩写,是现代企业用于表示制程能力的指标.制程能力是过程性能的允许最大变化范围与过程的正常偏差的比值.制程能力研究在於确认这些 ...
- jetty-run运行报错的原因
- JAVA 处理程序异常,(try、catch、finally),(thorws)
一.try语句: try{//这里写可能出现异常的程序} catch(Exception e){//这里写如果出现异常怎么处理的程序} 二.throws语句 语法:函数方法() throws Exc ...
- js实现的新闻列表垂直滚动实现详解
js实现的新闻列表垂直滚动实现详解:新闻列表垂直滚动效果在大量的网站都有应用,有点自然是不言而喻的,首先由于网页的空间有限,使用滚动代码可以使用最小的空间提供更多的信息量,还有让网页有了动态的效果,更 ...
- Ubuntu编写开机自启动脚本(转载)
From:http://blog.csdn.net/marujunyy/article/details/8466255 1.首先编写一个简单的shell脚本test.sh #! /bin/bash e ...
- 要件审判九步法及其基本价值 z
要件审判九步法及其基本价值 发布时间:2014-12-24 14:29:05 作者介绍 邹碧华,男,1967年出生于江西奉新,毕业于北京大学法学院,获法学博士学位.上海市高级人民法院副院长.2006年 ...
- 程序员遇到Bug时的30个反应
开发应用程序是一个非常有压力的工作.没有人是完美的,因此在这个行业中,代码中出现bug是相当普遍的现象.面对bug,一些程序员会生气,会沮丧,会心烦意乱,甚至会灰心丧气,而另一些程序员会依然保持冷静沉 ...