博客
关于我
POI分多次向生成的EXCEL中写入数据
阅读量:307 次
发布时间:2019-03-03

本文共 4666 字,大约阅读时间需要 15 分钟。

一:分多次将数据写入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;	}	}

 

 

转载地址:http://vuql.baihongyu.com/

你可能感兴趣的文章
mysql面试题学校三表查询_mysql三表查询分组后取每组最大值,mysql面试题。
查看>>
MySQL面试题集锦
查看>>
mysql颠覆实战笔记(八)--mysql的自定义异常处理怎么破
查看>>
mysql驱动、durid、mybatis之间的关系
查看>>
mysql驱动支持中文_mysql 驱动包-Go语言中文社区
查看>>
MySQL高可用切换_(5.9)mysql高可用系列——正常主从切换测试
查看>>
MYSQL高可用集群MHA架构
查看>>
MySQL高级-MySQL并发参数调整
查看>>
MySQL高级-MySQL查询缓存优化
查看>>
MySQL高级-SQL优化步骤
查看>>
MySQL高级-视图
查看>>
MySQL高级-触发器
查看>>
mysql高级查询~分页查询
查看>>
MySQL(2)DDL详解
查看>>
MySQL:MySQL执行一条SQL查询语句的执行过程
查看>>
MySQL:判断逗号分隔的字符串中是否包含某个字符串
查看>>
MySQL:某个ip连接mysql失败次数过多,导致ip锁定
查看>>
Mysql:避免重复的插入数据方法汇总
查看>>
n 叉树后序遍历转换为链表问题的深入探讨
查看>>
nacos config
查看>>