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 条提交一次) ▌多线程插入(单表) 问:为何对同一个表的插入多线程会比单线程 ...
随机推荐
- Evosuite使用方法入门
Evosuite使用方法入门 1.简要介绍 EvoSuite开源工具可以基于Eclipse进行测试用例的自动生成,生成的测试用例符合Junit标准(直接生成可进行Junit的java文件),满 ...
- 自己写的C#三层代码生成器
思来想去用T4生成代码要学习它的语法,C#本身能很简单地生成txt文件,为啥不直接批量替换模板方式自己写个的三层代码生成器.说干就干,2个小时搞定.当然各层还可以做的更精细,比如DAL层的Add方法I ...
- Vue2.5
1.1 创建第一个Vue实例
- 1.2.7 Excel表格打印技巧
1.打印居中显示 把打印的内容放在纸张中间,居中对齐. 方法: ①点击[页面布局]选项卡: ②找到[页面设置]群组点击右下角显示更多: ③在页边距对话框中选择[页边距]: ④勾选[居中方式]中的[水平 ...
- JQuery操作属性、样式、风格(attr、class、css)
样式操作 <p class="myclass" title="选择喜欢的水果">你最喜欢的水果是?</p> 在上面代码中,class也是 ...
- 【C++】ubuntu中读取指定目录中的所有文件
摘要:ubuntu系统下,C++程序读取指定文件夹中多个文件,保存文件名列表.文件名没有规律且不考虑读取子文件夹中的文件. 系统配置:ubuntu16.04, cmake编译 首先安利一个函数,输入s ...
- APS技术中的多目标规划问题
在进行APS(高级计划与排程)系统开发时,绝大多数情况下是需要考虑多目标的.但面对多目标问题进行规划求解时,我们往往极容易因处理方法不当,而影响输出结果,令结果与用户期望产生较大差别.事实上很多时候用 ...
- Android编程-Activity
重要的回调函数: onCreate():初始化activity要用的其他的组件(可以是其他的activity和contentprovider之类).调用setContentView(),设定本Acti ...
- qcom,msm8996-pinctrl.txt
Qualcomm Technologies, Inc. MSM8996 TLMM block This binding describes the Top Level Mode Multiplexer ...
- 60秒的快速巡检Linux服务器性能
uptime dmesg | tail vmstat 1 mpstat -P ALL 1 pidstat 1 iostat -xz 1 free -m sar -n DEV 1 sar -n TCP, ...