我是靠谱客的博主 强健书包,最近开发中收集的这篇文章主要介绍POI分多次向生成的EXCEL中写入数据一:分多次将数据写入EXCEL,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一:分多次将数据写入EXCEL

  1:这种方式效率比较低,数据量越大越明显,4万条数据要2分钟左右

package com.test;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class POIController {
	/**
	 * 这种方式效率比较低,数据量越大越明显,4万条数据要2分钟左右
	 * @param args
	 * @throws FileNotFoundException
	 * @throws InvalidFormatException
	 */
	public static void main(String[] args) throws FileNotFoundException, InvalidFormatException {
		long startTime = System.currentTimeMillis();
		BufferedOutputStream outPutStream = null;
		XSSFWorkbook workbook = null;
		FileInputStream inputStream = null;
		String filePath = "E:\txt\111.xlsx";
		try {
			 workbook = getWorkBook(filePath);
			 XSSFSheet sheet = workbook.getSheetAt(0);
			 for (int i = 0; i < 40; i++) {
				 for (int z = 0; z < 1000; z++) {
					 XSSFRow row = sheet.createRow(i*1000+z);
					 for (int j = 0; j < 10; j++) {
						 row.createCell(j).setCellValue("你好:"+j);
					 }
				 }
				 //每次要获取新的文件流对象,避免将之前写入的数据覆盖掉
				 outPutStream = new BufferedOutputStream(new FileOutputStream(filePath));
				 workbook.write(outPutStream);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(outPutStream!=null) {
				try {
					outPutStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(inputStream!=null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(workbook!=null) {
				try {
					workbook.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		long endTime = System.currentTimeMillis();
		System.out.println(endTime-startTime);
	}
	/**
	 * 先创建一个XSSFWorkbook对象
	 * @param filePath
	 * @return
	 */
	public static XSSFWorkbook getWorkBook(String filePath) {
		XSSFWorkbook workbook =  null;
		try {
			File fileXlsxPath = new File(filePath);
			BufferedOutputStream outPutStream = new BufferedOutputStream(new FileOutputStream(fileXlsxPath));
			workbook = new XSSFWorkbook();
			workbook.createSheet("测试");
			workbook.write(outPutStream);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return workbook;
	}
	
}

  2:这种方式效率高一些,属于一次性写入,数据量越大越明显,4万条数据要17秒左右

package com.test;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class POIController {
	/**
	 * 这种方式效率高一些,属于一次性写入,数据量越大越明显,4万条数据要17秒左右
	 * @param args
	 * @throws FileNotFoundException
	 * @throws InvalidFormatException
	 */
	public static void main(String[] args) throws FileNotFoundException, InvalidFormatException {
		long startTime = System.currentTimeMillis();
		BufferedOutputStream outPutStream = null;
		XSSFWorkbook workbook = null;
		FileInputStream inputStream = null;
		String filePath = "E:\txt\111.xlsx";
		try {
			 workbook = getWorkBook(filePath);
			 XSSFSheet sheet = workbook.getSheetAt(0);
			 for (int i = 0; i < 40; i++) {
				 for (int z = 0; z < 1000; z++) {
					 XSSFRow row = sheet.createRow(i*1000+z);
					 for (int j = 0; j < 10; j++) {
						 row.createCell(j).setCellValue("你好:"+j);
					 }
				 }
			}
			 //每次要获取新的文件流对象,避免将之前写入的数据覆盖掉
			 outPutStream = new BufferedOutputStream(new FileOutputStream(filePath));
			 workbook.write(outPutStream);
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(outPutStream!=null) {
				try {
					outPutStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(inputStream!=null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(workbook!=null) {
				try {
					workbook.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		long endTime = System.currentTimeMillis();
		System.out.println(endTime-startTime);
	}
	/**
	 * 先创建一个XSSFWorkbook对象
	 * @param filePath
	 * @return
	 */
	public static XSSFWorkbook getWorkBook(String filePath) {
		XSSFWorkbook workbook =  null;
		try {
			File fileXlsxPath = new File(filePath);
			BufferedOutputStream outPutStream = new BufferedOutputStream(new FileOutputStream(fileXlsxPath));
			workbook = new XSSFWorkbook();
			workbook.createSheet("测试");
			workbook.write(outPutStream);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return workbook;
	}
	
}

 下一篇:POI之SXSSFWorkbook大量数据导出至excel
 文章目录

最后

以上就是强健书包为你收集整理的POI分多次向生成的EXCEL中写入数据一:分多次将数据写入EXCEL的全部内容,希望文章能够帮你解决POI分多次向生成的EXCEL中写入数据一:分多次将数据写入EXCEL所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部