POIUtils 导出 poi Test 100w 600w 条数据
依赖:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.</version>
</dependency>
<!-- ############ poi ############## -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency> <dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency> <dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
//
java用POI设置Excel的列宽
HSSFSheet.setColumnWidth(int columnIndex, int width);
eg:
sheet.setColumnWidth(0, 252*width+323);//width=35
PoiExportUtils:
package com.icil.esolution.utils; import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; /**
*
* @ClassName: PoiExportUtils
* @Description: use export excel , some common code
* @Author: Sea
* @Date: 15 Oct 2018 2:26:38 PM
* @Copyright: 2018 ICIL All rights reserved.
*/
public class PoiExportUtils { private static String STANDARD_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public Workbook workbook = new XSSFWorkbook();
DataFormat format = null; {
format = workbook.createDataFormat();
} public Sheet createXSheet(String sheetName) { // 在Excel工作簿中建一工作表,其名为缺省值, 也可以指定Sheet名称
Sheet sheet = null;
if (StringUtils.isNotBlank(sheetName)) {
sheet = workbook.createSheet(sheetName);
} else {
workbook.createSheet();
}
//Freeze the title row
/**
* cellNum:表示要冻结的列数;
rowNum:表示要冻结的行数;
firstCellNum:表示被固定列右边第一列的列号;
firstRollNum :表示被固定行下边第一列的行号;
*/
sheet.createFreezePane( , , , ); return sheet; } public CellStyle getTitleCellStyle() {
// 用于格式化单元格的数据
// DataFormat format = workbook.createDataFormat();
// 设置字体
Font font = workbook.createFont();
// font.setFontHeightInPoints((short) 20); // 字体高度
// font.setColor(Font.COLOR_RED); // 字体颜色
font.setFontName("黑体"); // 字体
font.setBold(true); // 加粗
// font.setBoldweight(Font.BOLDWEIGHT_BOLD); // 宽度
font.setItalic(true); // 是否使用斜体
font.setStrikeout(true); //是否使用划线
// 设置单元格类型
CellStyle titleCellStyle = workbook.createCellStyle();
titleCellStyle.setBorderBottom(BorderStyle.THIN); // 下边框
titleCellStyle.setBorderLeft(BorderStyle.THIN);// 左边框
titleCellStyle.setBorderTop(BorderStyle.THIN);// 上边框
titleCellStyle.setBorderRight(BorderStyle.THIN);// 右边框
// titleCellStyle.setFillForegroundColor(HSSFColor.GREEN.index); //
// titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); //填充形式
titleCellStyle.setFont(font);
titleCellStyle.setAlignment(HorizontalAlignment.CENTER); // 水平布局:居中
titleCellStyle.setWrapText(true); return titleCellStyle;
} public CellStyle getDateCellStyle() {
CellStyle cellStyle1 = workbook.createCellStyle();
cellStyle1.setDataFormat(format.getFormat(STANDARD_TIME_FORMAT));
return cellStyle1;
}
/**
* @ such as 0.000 | yyyy-MM-dd hh:mm:ss
* @param formats
* @return
*/
public CellStyle getDataCellStyle(String formats) {
CellStyle cellStyle1 = workbook.createCellStyle();
cellStyle1.setDataFormat(format.getFormat(formats));
return cellStyle1;
} }
POIUtils
package com.icil.report.utils; import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
/**
* *************************************************************************
* <PRE>
* @ClassName: : POIUtils
*
* @Description: :
*
* @Creation Date : 8 May 2019 1:58:29 PM
*
* @Author : Sea
*
*
* </PRE>
**************************************************************************
*/
public class POIUtils { public static CellStyle getTitleCellStyle(Workbook workbook) {
// 用于格式化单元格的数据
// DataFormat format = workbook.createDataFormat();
// 设置字体
Font font = workbook.createFont();
// font.setFontHeightInPoints((short) 20); // 字体高度
// font.setColor(Font.COLOR_RED); // 字体颜色
font.setFontName("黑体"); // 字体
font.setBold(true); // 加粗
// font.setBoldweight(Font.BOLDWEIGHT_BOLD); // 宽度
font.setItalic(true); // 是否使用斜体
// font.setStrikeout(true); //是否使用划线
// 设置单元格类型
CellStyle titleCellStyle = workbook.createCellStyle();
titleCellStyle.setBorderBottom(BorderStyle.THIN); // 下边框
titleCellStyle.setBorderLeft(BorderStyle.THIN);// 左边框
titleCellStyle.setBorderTop(BorderStyle.THIN);// 上边框
titleCellStyle.setBorderRight(BorderStyle.THIN);// 右边框
// titleCellStyle.setFillForegroundColor(HSSFColor.GREEN.index); //
// titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); //填充形式
titleCellStyle.setFont(font);
titleCellStyle.setAlignment(HorizontalAlignment.CENTER); // 水平布局:居中
titleCellStyle.setWrapText(true); return titleCellStyle;
} /**
* @font "黑体" "加粗" “斜体”
* @param workbook
* @return
*/
public static CellStyle getFontStyle(Workbook workbook,boolean isItalic) {
// 设置字体
Font font = workbook.createFont();
// font.setFontHeightInPoints((short) 20); // 字体高度
// font.setColor(Font.COLOR_RED); // 字体颜色
font.setFontName("黑体"); // 字体
font.setBold(true); // 加粗
// font.setBoldweight(Font.BOLDWEIGHT_BOLD); // 宽度
font.setItalic(true); // 是否使用斜体
CellStyle titleCellStyle = workbook.createCellStyle();
titleCellStyle.setFont(font);
titleCellStyle.setAlignment(HorizontalAlignment.CENTER); // 水平布局:居中
titleCellStyle.setWrapText(true); return titleCellStyle;
} /**
* @param sheet
* @param rownum
* @param cellColNum
* @param cellValue
* @param cellstyle
*/
public static void setCellValue(SXSSFSheet sheet, int rownum, int cellColNum, String cellValue,
CellStyle cellstyle) { SXSSFRow row = sheet.getRow(rownum);
if(null==sheet.getRow(rownum)){
row= sheet.createRow(rownum);
}
SXSSFCell cell= row.getCell(cellColNum);
if(null==row.getCell(cellColNum)){
cell = row.createCell(cellColNum);
}
cell.setCellStyle(cellstyle);
cell.setCellValue(cellValue);
} }
test
package com.sea.shan.poi; import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test; import com.sea.shan.utils.POIUtils;
import com.sea.shan.utils.PoiExportUtils; public class POIUtilsTest { @Test
public void readExcel() throws Exception {
Workbook workBook = POIUtils.getWorkBook("/home/sea/Desktop/Test/airline-airport-country-code.xlsx"); Sheet sheetAt0 = workBook.getSheetAt();
int lastRowNum = sheetAt0.getLastRowNum(); for (int i = ; i <= lastRowNum; i++) {
// get per row
Row row = sheetAt0.getRow(i); if (row == null) {
continue;
} // String cellValue0 = POIUtils.getCellValue(row.getCell(0));
// String cellValue1 = POIUtils.getCellValue(row.getCell(1));
// String cellValue0 = POIUtils.getCellValues(row.getCell(0));
// String cellValue1 = POIUtils.getCellValues(row.getCell(1));
String cellValue0 = new DataFormatter().formatCellValue(row.getCell()); String cellValue1 = new DataFormatter().formatCellValue(row.getCell()); System.err.println(cellValue0 + "=" + cellValue1); }
} @Test
public void writeExcel() throws Exception { String sheetName = "Inventory";
PoiExportUtils poiExportUtils = new PoiExportUtils();
Sheet sheet = poiExportUtils.createXSheet(sheetName);
// 2. set title //"seqId","partNo","partDesc","qtyInv","storeInDtLoc"
String[] title = { "商品編號 ", " 商品描述 ", " 數量 ", " 數量單位 ", "入庫時間 " };
// set order by
sheet.setAutoFilter(CellRangeAddress.valueOf("A1:E1"));
// set content
for (int contentColumn = ; contentColumn <= ; contentColumn++) { Row contentRow = sheet.createRow(contentColumn);
// set title
sheet.autoSizeColumn((short) contentColumn); // 自动调整该列的宽度
if (contentColumn == ) {
for (int titleColumn = ; titleColumn < title.length; titleColumn++) {
Cell titleCell = contentRow.createCell(titleColumn);
titleCell.setCellStyle(poiExportUtils.getTitleCellStyle());
titleCell.setCellValue(title[titleColumn]);
}
continue;
}
// set content body
int i = ;
contentRow.createCell(i++).setCellValue("cell" + i);
contentRow.createCell(i++).setCellValue("cell" + i++);
Cell cell2 = contentRow.createCell(i++);
cell2.setCellValue("cell" + i++);
contentRow.createCell(i++).setCellValue("cell" + i++);
contentRow.createCell(i++).setCellValue("cell" + i++);
}
Workbook workbook = poiExportUtils.workbook;
// 保存
String filename = "/home/sea/Desktop/workbook0oo1.xls";
if (workbook instanceof XSSFWorkbook) {
filename = filename + "x";
}
FileOutputStream out = new FileOutputStream(filename);
workbook.write(out);
out.close(); } /**
* test 导出大量的数据
* @throws Exception
*/
@Test
public void testWriteExcel() throws Exception { // 第一步,创建一个HSSFWorkbook,对应一个Excel文件
// Workbook workbook = new XSSFWorkbook(5000); long start = System.currentTimeMillis();
SXSSFWorkbook workbook = new SXSSFWorkbook();//内存中保留 10000 条数据,以免内存溢出,其余写入 硬盘
String sheetName = "test";
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
SXSSFSheet sheet = workbook.createSheet(sheetName);
Sheet sheet1 = workbook.createSheet("sa1");
Sheet sheet2 = workbook.createSheet("sa2");
Sheet sheet3 = workbook.createSheet("sa3");
// Freeze the title row
/**
* cellNum:表示要冻结的列数; rowNum:表示要冻结的行数; firstCellNum:表示被固定列右边第一列的列号;
* firstRollNum :表示被固定行下边第一列的行号;
*/
sheet.createFreezePane(, , , );
sheet.setAutoFilter(CellRangeAddress.valueOf("A1:H1")); String[] title = { "商品編號 ", " 商品描述", " 數量", " 數量單位 ", "入庫時間 " }; // set content
for (int contentColumn = ; contentColumn <= ; contentColumn++)
{
Row contentRow = sheet.createRow(contentColumn);
// sheet.autoSizeColumn((short) contentColumn); // 自动调整该列的宽度 // ################# set title ################
if (contentColumn == ) {
for (int titleColumn = ; titleColumn < title.length; titleColumn++) {
Cell titleCell = contentRow.createCell(titleColumn);
titleCell.setCellStyle(getTitleCellStyle(workbook));
titleCell.setCellValue(title[titleColumn]);
}
continue;
}
// ################# set title end ################ //********************* set body content **************************************
for (int titleColumn = ; titleColumn < title.length+; titleColumn++) {
contentRow.createCell(titleColumn).setCellValue("cell" + contentColumn);
}
//********************* set body content **************************************
} FileOutputStream out = new FileOutputStream("/home/sea/Desktop/seatest.xlsx");
workbook.write(out); System.out.println("total cost time:"+(System.currentTimeMillis()-start));
} @Test
public void testWriteExcel01() throws Exception { // 第一步,创建一个HSSFWorkbook,对应一个Excel文件
// Workbook workbook = new XSSFWorkbook(5000); long start = System.currentTimeMillis();
SXSSFWorkbook workbook = new SXSSFWorkbook();//内存中保留 10000 条数据,以免内存溢出,其余写入 硬盘
String sheetName = "test";
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet String[] title = { "商品編號 ", " 商品描述", " 數量", " 數量單位 ", "入庫時間 " }; for(int i=;i<;i++)
{
SXSSFSheet sheet =workbook.createSheet(sheetName+i);;
// Freeze the title row
/**
* cellNum:表示要冻结的列数; rowNum:表示要冻结的行数; firstCellNum:表示被固定列右边第一列的列号;
* firstRollNum :表示被固定行下边第一列的行号;
*/
sheet.createFreezePane(, , , );
sheet.setAutoFilter(CellRangeAddress.valueOf("A1:H1")); // set content
for (int contentColumn = ; contentColumn <= ; contentColumn++)
{
Row contentRow = sheet.createRow(contentColumn);
// sheet.autoSizeColumn((short) contentColumn); // 自动调整该列的宽度 // ################# set title ################
if (contentColumn == ) {
for (int titleColumn = ; titleColumn < title.length; titleColumn++) {
Cell titleCell = contentRow.createCell(titleColumn);
titleCell.setCellStyle(getTitleCellStyle(workbook));
titleCell.setCellValue(title[titleColumn]);
}
continue;
}
// ################# set title end ################ //********************* set body content **************************************
for (int titleColumn = ; titleColumn < title.length+; titleColumn++) {
contentRow.createCell(titleColumn).setCellValue("cell" + contentColumn);
}
//********************* set body content **************************************
}
} FileOutputStream out = new FileOutputStream("/home/sea/Desktop/seatest.xlsx");
workbook.write(out); System.out.println("total cost time:"+(System.currentTimeMillis()-start));
} public CellStyle getTitleCellStyle(Workbook workbook) {
// 用于格式化单元格的数据
// DataFormat format = workbook.createDataFormat();
// 设置字体
Font font = workbook.createFont();
// font.setFontHeightInPoints((short) 20); // 字体高度
// font.setColor(Font.COLOR_RED); // 字体颜色
font.setFontName("黑体"); // 字体
font.setBold(true); // 加粗
// font.setBoldweight(Font.BOLDWEIGHT_BOLD); // 宽度
font.setItalic(true); // 是否使用斜体
// font.setStrikeout(true); //是否使用划线
// 设置单元格类型
CellStyle titleCellStyle = workbook.createCellStyle();
titleCellStyle.setBorderBottom(BorderStyle.THIN); // 下边框
titleCellStyle.setBorderLeft(BorderStyle.THIN);// 左边框
titleCellStyle.setBorderTop(BorderStyle.THIN);// 上边框
titleCellStyle.setBorderRight(BorderStyle.THIN);// 右边框
// titleCellStyle.setFillForegroundColor(HSSFColor.GREEN.index); //
// titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); //填充形式
titleCellStyle.setFont(font);
titleCellStyle.setAlignment(HorizontalAlignment.CENTER); // 水平布局:居中
titleCellStyle.setWrapText(true); return titleCellStyle;
} }
POIUtils 导出 poi Test 100w 600w 条数据的更多相关文章
- SpringMVC 实现POI读取Excle文件中数据导入数据库(上传)、导出数据库中数据到Excle文件中(下载)
读取Excale表返回一个集合: package com.shiliu.game.utils; import java.io.File; import java.io.FileInputStream; ...
- 问问题_Java一次导出百万条数据生成excel(web操作)
需求:在web页面操作,一次导出百万条数据并生成excel 分析: 1.异步生成Excel,非实时,完成后使用某种方式通知用户 2.生成多个excel文件,并打包成zip文件,因为一个excel容纳不 ...
- 从系统报表页面导出20w条数据到本地只用了4秒,我是如何做到的
背景 最近有个学弟找到我,跟我描述了以下场景: 他们公司内部管理系统上有很多报表,报表数据都有分页显示,浏览的时候速度还可以.但是每个报表在导出时间窗口稍微大一点的数据时,就异常缓慢,有时候多人一起导 ...
- SqlBulkCopy 插入100W条数据时 属性BatchSize的作用
(1)100W条insert语句在一个连接内一句一句加 花了01:17:19.0542805 (2) SqlBulkCopy 插入100W条数据 设置BatchSize=500 耗时:00:03:29 ...
- 快速生成100W条数据
快速生成100W条数据,生成的时候是顺序生成,取的时候是随机取用,生成100W条数据大概一分多钟,比网上其他代码速度要快很多 )) --truncate table tb --select top 1 ...
- PHP导出3w条数据成表格
亲测有效,三万条数据秒秒钟导出 先进行数据表插入数据 ini_set('memory_limit','1024M'); //设置程序运行的内存 ini_set('max_execution_time' ...
- 向数据库添加100W 条数据 性能测试
向数据库添加100W 条数据 性能测试 : 参考的相关网站目录: JDBC实现往MySQL插入百万级数据 https://www.cnblogs.com/fnz0/p/5713102.html MyS ...
- php使用cvs导出百万条数据,大量数据
MySQL CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(45) NOT NULL DEFAUL ...
- 你向 Mysql 数据库插入 100w 条数据用了多久?
阅读本文大概需要 2 分钟. ▌目录 多线程插入(单表) 多线程插入(多表) 预处理 SQL 多值插入 SQL 事务( N 条提交一次) ▌多线程插入(单表) 问:为何对同一个表的插入多线程会比单线程 ...
随机推荐
- 学习笔记DL008:概率论,随机变量,概率分布,边缘概率,条件概率,期望、方差、协方差
概率和信息论. 概率论,表示不确定性声明数学框架.提供量化不确定性方法,提供导出新不确定性声明(statement)公理.人工智能领域,概率法则,AI系统推理,设计算法计算概率论导出表达式.概率和统计 ...
- 第六章Django
web应用程序 server端建立socket,不断地accept,当收到客户端连接信号之后,服务端向客户端发送数据,将html网页打开,read出来,并发送至客户端,这样客户端就可以浏览到网页的内容 ...
- 真tm郁闷
昨天这时还是信心满满,今天这时就已经彻底颓了. 感觉这次是最接近的了,4个题目都做出来了,怎么还会fail,那边也不说为什么,到底是哪里不足,尽说些没用的. 前后都当了4次炮灰了,以后也不会再冲动了, ...
- 面试题之python基础
基础语法 输入和输出 代码中要修改不可变的数据会出现什么问题,抛出什么异常? 代码不会征程运行,抛出TypeError异常 a = 1,b = 2,不用中间变量交换a和b的值? # 方法1 a = a ...
- flutter 容器 几种写法
1.Stack: 取代线性布局 (译者语:和Android中的LinearLayout相似),Stack允许子 widget 堆叠, 你可以使用 Positioned 来定位他们相对于Stack的上下 ...
- Java String类和StringBuffer类的区别
1.String与StringBuffer的区别简单地说,就是一个变量和常量的关系.StringBuffer对象的内容可以修改:而String对象一旦产生后就不可以被修改,重新赋值其实是两个对象.St ...
- delphi 多线程之 TEvent 和 TLightweightEvent
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- C# 将DataTable转换成list (--分页--) 【Skip--Take】
将DataTable转换成list 及数据分页: /// <summary> /// 酒店评论列表-分页 /// </summary> /// <param name=& ...
- 蓝桥第八届省赛 javaB组承压计算
X星球的高科技实验室中整齐地堆放着某批珍贵金属原料. 每块金属原料的外形.尺寸完全一致,但重量不同.金属材料被严格地堆放成金字塔形. 7 5 8 7 8 8 9 2 7 2 8 1 4 9 1 8 1 ...
- goflyway简单使用
前言 一个朋友最近新买的搬瓦工ip突然被强了,要等10周左右才能免费更换ip.而恰巧在网上看到了Goflyway 进阶教程:KCP 协议复活被墙IP 决定试一试.在vultr上临时搭建了测试环境,可能 ...