依赖:

        <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 条数据的更多相关文章

  1. SpringMVC 实现POI读取Excle文件中数据导入数据库(上传)、导出数据库中数据到Excle文件中(下载)

    读取Excale表返回一个集合: package com.shiliu.game.utils; import java.io.File; import java.io.FileInputStream; ...

  2. 问问题_Java一次导出百万条数据生成excel(web操作)

    需求:在web页面操作,一次导出百万条数据并生成excel 分析: 1.异步生成Excel,非实时,完成后使用某种方式通知用户 2.生成多个excel文件,并打包成zip文件,因为一个excel容纳不 ...

  3. 从系统报表页面导出20w条数据到本地只用了4秒,我是如何做到的

    背景 最近有个学弟找到我,跟我描述了以下场景: 他们公司内部管理系统上有很多报表,报表数据都有分页显示,浏览的时候速度还可以.但是每个报表在导出时间窗口稍微大一点的数据时,就异常缓慢,有时候多人一起导 ...

  4. SqlBulkCopy 插入100W条数据时 属性BatchSize的作用

    (1)100W条insert语句在一个连接内一句一句加 花了01:17:19.0542805 (2) SqlBulkCopy 插入100W条数据 设置BatchSize=500 耗时:00:03:29 ...

  5. 快速生成100W条数据

    快速生成100W条数据,生成的时候是顺序生成,取的时候是随机取用,生成100W条数据大概一分多钟,比网上其他代码速度要快很多 )) --truncate table tb --select top 1 ...

  6. PHP导出3w条数据成表格

    亲测有效,三万条数据秒秒钟导出 先进行数据表插入数据 ini_set('memory_limit','1024M'); //设置程序运行的内存 ini_set('max_execution_time' ...

  7. 向数据库添加100W 条数据 性能测试

    向数据库添加100W 条数据 性能测试 : 参考的相关网站目录: JDBC实现往MySQL插入百万级数据 https://www.cnblogs.com/fnz0/p/5713102.html MyS ...

  8. php使用cvs导出百万条数据,大量数据

    MySQL CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(45) NOT NULL DEFAUL ...

  9. 你向 Mysql 数据库插入 100w 条数据用了多久?

    阅读本文大概需要 2 分钟. ▌目录 多线程插入(单表) 多线程插入(多表) 预处理 SQL 多值插入 SQL 事务( N 条提交一次) ▌多线程插入(单表) 问:为何对同一个表的插入多线程会比单线程 ...

随机推荐

  1. centos tree 命令

    ftp://mama.indstate.edu/linux/tree/ download & make

  2. ios-时间换算

    经常会遇到时间转换的,在此收藏一个时间换算的方法〜 #pragma mark 时间换算 + (NSString *)setcreateTime:(NSString *)str { //yyyy-MM- ...

  3. 创建一个dynamics 365 CRM online plugin (五) - Images in Plugin

    Snapshots of the primary entity's attributes from database before(pre) and after (post) the core pla ...

  4. libvirt启动报错Failed to start Virtualization daemon

    libvirt启动报错Failed to start Virtualization daemon 1.启动libvirt的具体报错如下 [root@localhost IOS]# service li ...

  5. .NET Remoting、WebService、WCF、WebApi一些简单描述

    1. .NET Remoting是传输层协议TCP封装的,速度非常快,.NET Remoting基于.net反射机制,只方便.net使用,因此它有平台限制.(.NET Remoting的工作原理:服务 ...

  6. g2opy 记录

    20190429 10:31 之前安装了g2opy,g2o也一直没好好学,跑通了slam14讲第13章的单目稠密重建.想要改写成 python版,开始仔细研究g2opy的用法.

  7. [UE4]判断2个向量是否相等

    一.因为向量是3个浮点数,如果不使用误差的话,计算机里面的浮点数是有一定误差的. 二.如上图所示,如果2个向量的误差1厘米(UE4的单位是厘米),则表示2个向量相等.

  8. amd显卡更新最新驱动鼠标顿卡的解决方法

    测试了下游戏,很稳.方法:在卡顿的状态下打开RADEON设置,依次点击显示器→规格→覆盖,将支持HDCP选为禁用,按提示重启即可.

  9. analyse idoc by creation date

    t-code ZMM0127 infoset: ZMM_IDOC_READ_01 go to code: AUTHORITY-CHECK OBJECT 'S_IDOCMONI'ID 'ACTVT' F ...

  10. docker-compose hello word

    Compose 是 Docker 容器进行编排的工具, 是一个整合发布docker应用的利器,可定义和运行多容器的应用,在 Compose 中你可以使用 YAML 文件来配置你的应用服务.然后,只需要 ...