博客
关于我
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 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>
mysql 添加索引
查看>>
MySQL 添加索引,删除索引及其用法
查看>>
mysql 状态检查,备份,修复
查看>>
MySQL 用 limit 为什么会影响性能?
查看>>
MySQL 用 limit 为什么会影响性能?有什么优化方案?
查看>>
MySQL 用户权限管理:授权、撤销、密码更新和用户删除(图文解析)
查看>>
mysql 用户管理和权限设置
查看>>
MySQL 的 varchar 水真的太深了!
查看>>
mysql 的GROUP_CONCAT函数的使用(group_by 如何显示分组之前的数据)
查看>>