百万级别数据导出 EXCEL 办法
示例代码:
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.nio.channels.FileChannel;
import java.util.Map;
public class ExcelHelper {
private Workbook workbook = null;
private String filePath = null;
private File file = null;
private RandomAccessFile randomAccessFile = null;
private Sheet sheet = null;
private FileChannel fileChannel = null;
private SXSSFWorkbook sxssfWorkbook = null;
private FileOutputStream fileOutputStream = null;
private SXSSFSheet sxssfSheet = null;
private ExcelHelper(String filePath) {
this.filePath = filePath;
}
private ExcelHelper(File file) {
this.file = file;
}
/**
* 检查并新建文件
* @param helper
*/
private static void checkAndNewFile(ExcelHelper helper) throws FileNotFoundException {
// String parentDirectory =
// helper.filePath.replace(helper.filePath,helper.filePath.split(File.separator)[helper.filePath.split(File.separator).length - 1]);
String fileSep = "\\";
Map<String,String> systemInfo = System.getenv();
String osName = systemInfo.get("OS");
osName = osName.toLowerCase();
if (osName.contains("windows")){
fileSep = "/";
}else {
fileSep = "\\";
}
String filePathTemp = helper.filePath;
String[] filePatchArray = filePathTemp.split(fileSep);
String fileName = filePatchArray[filePatchArray.length - 1];
String parentDirectory = filePathTemp.replace(fileSep+fileName,"");
// 检查创建目录
File dir = new File(parentDirectory);
if (!dir.exists()){
dir.mkdirs();
}
// 检查创建文件
File tempFile = new File(helper.filePath);
if (tempFile.exists()){
tempFile.delete();
tempFile = null;
helper.file = new File(helper.filePath);
helper.fileOutputStream = new FileOutputStream(helper.file);
}else {
helper.file = new File(helper.filePath);
helper.fileOutputStream = new FileOutputStream(helper.file);
}
}
public static ExcelHelper getInstance(String filePath) throws FileNotFoundException {
ExcelHelper helper = new ExcelHelper(filePath);
checkAndNewFile(helper);
helper.randomAccessFile = new RandomAccessFile(helper.file,"rw");
helper.fileChannel = helper.randomAccessFile.getChannel();
if (helper.filePath.endsWith(".xlsx")){
XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
helper.workbook = xssfWorkbook;
helper.sxssfWorkbook = new SXSSFWorkbook(xssfWorkbook);
helper.sxssfSheet = helper.sxssfWorkbook.createSheet();
helper.sxssfSheet.setRandomAccessWindowSize(100);
}else if (helper.filePath.endsWith(".xls")){
helper.workbook = new HSSFWorkbook();
helper.sheet=helper.workbook.createSheet();
}else {
return null;
}
return helper;
}
public static ExcelHelper getInstance(File file) throws FileNotFoundException {
if (file == null) return null;
ExcelHelper helper = new ExcelHelper(file);
helper.randomAccessFile = new RandomAccessFile(helper.file,"rw");
helper.fileChannel = helper.randomAccessFile.getChannel();
helper.filePath = file.getAbsolutePath();
helper.fileOutputStream = new FileOutputStream(helper.file);
if (helper.filePath.endsWith(".xlsx")){
XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
helper.workbook = xssfWorkbook;
helper.sxssfWorkbook = new SXSSFWorkbook(xssfWorkbook);
helper.sxssfSheet = helper.sxssfWorkbook.createSheet();
helper.sxssfSheet.setRandomAccessWindowSize(100);
}else if (helper.filePath.endsWith(".xls")){
helper.workbook = new HSSFWorkbook();
helper.sheet=helper.workbook.createSheet();
}else {
return null;
}
return helper;
}
public void writeExcelTitle(String[] titles) throws Exception{
if (titles == null)return;
if (sheet == null){
throw new Exception("请先创建sheet!");
}
Row row=sheet.createRow(0);
Cell cell=null;
for (int i=0;i<titles.length;i++){
cell=row.createCell(i);
cell.setCellValue(titles[i]);
}
// workbook.write(fileOutputStream);
}
public void writeExcelLine(String[] line) throws IOException {
int lastRowNum = sheet.getLastRowNum();
Row nextrow=sheet.createRow(lastRowNum + 1);
for (int i=0; i<line.length; i++){
Cell contentCell=nextrow.createCell(i);
contentCell.setCellValue(line[i]);
}
// workbook.write(fileOutputStream);
}
public void writeSXSSExcelTitle(String[] titles) throws Exception{
if (titles == null)return;
if (sxssfSheet == null){
throw new Exception("请先创建sheet!");
}
Row row=sxssfSheet.createRow(0);
Cell cell=null;
for (int i=0;i<titles.length;i++){
cell=row.createCell(i);
cell.setCellValue(titles[i]);
}
// sxssfWorkbook.write(fileOutputStream);
}
public void writeSXSSExcelLine(String[] line) throws IOException {
int lastRowNum = sxssfSheet.getLastRowNum();
Row nextrow=sxssfSheet.createRow(lastRowNum + 1);
for (int i=0; i<line.length; i++){
Cell contentCell=nextrow.createCell(i);
contentCell.setCellValue(line[i]);
}
// sxssfWorkbook.write(fileOutputStream);
}
public void flushSXSS() throws IOException {
sxssfWorkbook.write(fileOutputStream);
}
public void flush() throws IOException {
workbook.write(fileOutputStream);
}
public void disposeSXSS(){
this.sxssfWorkbook.dispose();
}
public void destroy() throws IOException {
if (randomAccessFile!=null){
randomAccessFile.close();
}
if (workbook != null){
workbook.close();
}
if (fileOutputStream != null){
fileOutputStream.close();
}
if (sxssfWorkbook != null){
sxssfWorkbook.close();
}
if (fileChannel != null){
fileChannel.close();
}
}
/**
* POI 创建高版本Excel文件
* @author yangtingting
* @date 2019/07/29
*/
// public static void main(String[] args) throws Exception {
// //创建Excel文件薄
// XSSFWorkbook workbook=new XSSFWorkbook();
// //创建工作表sheeet
// Sheet sheet=workbook.createSheet();
// //创建第一行
// Row row=sheet.createRow(0);
// String[] title={"id","name","sex"};
// Cell cell=null;
// for (int i=0;i<title.length;i++){
// cell=row.createCell(i);
// cell.setCellValue(title[i]);
// }
// //追加数据
// for (int i=1;i<1000000;i++){
// Row nextrow=sheet.createRow(i);
// Cell cell2=nextrow.createCell(0);
// cell2.setCellValue("a"+i);
// cell2=nextrow.createCell(1);
// cell2.setCellValue("user"+i);
// cell2=nextrow.createCell(2);
// cell2.setCellValue("男");
// }
// //创建一个文件
// File file=new File("D:/poi_test.xlsx");
// file.createNewFile();
// FileOutputStream stream= new FileOutputStream(file);
// workbook.write(stream);
// stream.close();
//
// }
public static void main(String[] args) throws Exception {
ExcelHelper helper = ExcelHelper.getInstance("D:/poi_test.xlsx");
String[] title={"id","name","sex"};
String[] contents={"张三","李四","王五"};
helper.writeSXSSExcelTitle(title);
for (int i=1; i< 1000000; i++){
helper.writeSXSSExcelLine(contents);
}
helper.flushSXSS();
helper.disposeSXSS();
helper.destroy();
}
}
参考示例代码
展开阅读全文
版权声明:
本文内容由互联网用户自发贡献,版权归作者所有,本社区不拥有所有权,也不承担相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:jaagool@sina.cn 进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
本文内容由互联网用户自发贡献,版权归作者所有,本社区不拥有所有权,也不承担相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:jaagool@sina.cn 进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。