依赖:

        <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. JSP+javaBean:简易投票网页练习

    一.项目目标 session作用域中如果不及时清理一些对象,在一定久的时间或相当数量的请求后,造成session中大量对象堆积,这将严重影响服务器的性能. 在本实例中,通过写一个jsp与javaBea ...

  2. node搭建简易的websocket服务

    http协议单向请求,只能客户端向服务器发送消息,然而websocket一旦双方建立连接就可以双方通信,更加深层次的用法是websocket可以做基础,然后不同的客户端可以通过websocket连接可 ...

  3. 大数据量下的SQL Server数据库自身优化

    原文: http://www.d1net.com/bigdata/news/284983.html 1.1:增加次数据文件 从SQL SERVER 2005开始,数据库不默认生成NDF数据文件,一般情 ...

  4. 网络请求 selenium

    网络请求 selenium 部分流程: 第一:爬虫引擎生成requests请求,送往scheduler调度模块,进入等待队列,等待调度.第二:scheduler模块开始调度这些requests,出队, ...

  5. Yii2 设计模式——设计模式简介

    我们首先来思考一个问题:作为工程师,我们的价值是什么? 笔者认为是——解决用户问题. 我们的任何知识和技能,如果不能解决特定的问题,那么就是无用的屠龙之术:我们的任何经验,如果不能对解决新的问题有用, ...

  6. Mysql ssl 连接

    在Azure创建了一个Mysql5.7服务,因为默认使用ssl连接,需要下载Azure的证书,并使用openssl生成客户端的证书.具体流程参考官方文档 大致步骤: 下载根证书, 安装openssl, ...

  7. FirewallD 快速使用文档

    FirewallD简介 FirewallD是CentOS7系列上代替iptables管理netfilter的配置工具,提供图形化和命令行,使用python开发(新版中计划使用c++重写),提供图形化和 ...

  8. IntellijIDEA常用快捷键总结

    转载自:http://blog.csdn.net/qq_17586821/article/details/52554731 下面的这些常用快捷键需要在实际操作中不断地体会才能真正感受到它们的方便之处. ...

  9. Kettle解决方案: 第三章 安装和配置

  10. 开发者必备的 12 个 JavaScript 库

    现在 web 设计是最有趣的了,做好 web 设计不仅要熟练使用 Javascript,css 和 html 等,还要有自己的创意设计.为了方便大家发挥自己的创意,就产生了很多 JS 框架,Node. ...