jxl导出Excel报内存溢出...

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jxl导出Excel报内存溢出...相关的知识,希望对你有一定的参考价值。

1万条左右没问题,但是大于1.5万就有问题了,下面是代码,求高手帮忙优化一下。。。
/**
* 导出XSL文件
* @param title 第一行表头
* @param keys 表头对应的字段名
* @param dataList resultList
* @param xslName excelName
* @throws WriteException
*/
public void exportXslEntrys(String[] title, String[] keys, List dataList, String xslName) throws WriteException

//获得导出的数据
WritableWorkbook wbook = null ;
OutputStream out = null;
try

//获得此时的系统时间,为本次导出的Excel表起表名
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
String todayDate = sDateFormat.format(new Date());

this.getContext().getResponse().setHeader("Content-Type","application/vnd.ms-excel; charset=UTF-8");
//Excel表表名
String excelTableName=xslName+".xls";

String FileName = new String(excelTableName.getBytes("GBK"),"ISO8859_1");
this.getContext().getResponse().setHeader("Content-Disposition","attachment;filename=\"" + FileName + "\"");
out = this.getContext().getResponse().getOutputStream();
wbook = Workbook.createWorkbook(out); // 建立excel文件
// WritableSheet
WritableSheet wsheet = wbook.createSheet(xslName, 0); // 工作表名称
for(int i = 0; i < title.length;i++)
wsheet.setColumnView(i, (int) 20);

// 设置Excel字体
WritableFont wfont = new WritableFont(WritableFont.ARIAL, 10,
WritableFont.BOLD, false,
jxl.format.UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK);

WritableCellFormat titleFormat = new WritableCellFormat(wfont);
WritableFont wfont1 = new WritableFont(WritableFont.ARIAL, 20,
WritableFont.BOLD, false,
jxl.format.UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK);
WritableCellFormat format1 = new WritableCellFormat(wfont1);

format1.setAlignment(jxl.format.Alignment.CENTRE);

wsheet.mergeCells(0,0,title.length,0);
Label excelTitle1 = new Label(0, 0, xslName, format1);
wsheet.addCell(excelTitle1);
titleFormat.setWrap(true);
// 设置Excel记录
for (int i = 0; i < title.length; i++)

Label excelTitle = new Label(i, 1, title[i], titleFormat);
wsheet.addCell(excelTitle);

int size = dataList.size();
if(!dataList.isEmpty())
for (int i = 0; i < size; i++)
Map map = (HashMap) dataList.get(i);
for (int j = 0; j < keys.length; j++)
if(map.containsKey(keys[j]))
if(map.get(keys[j])!=null)
wsheet.addCell(new Label(j, i+2, map.get(keys[j]).toString()));

else
wsheet.addCell(new Label(j, i+2, ""));
else
wsheet.addCell(new Label(j, i+2, ""));




catch (Exception e)
e.printStackTrace();
finally
try
wbook.write();
wbook.close();
out.flush();
out.close();
System.gc();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
// 写入文件


this.getContext().dataResult.setMessage("OK");

定义记录的总数的变量,不能是Ingter型,要定义为长整型 参考技术A 不要全都写完才flush 参考技术B 右键——设置单元格格式——对齐——方向——选择一下——确定。

SpringBoot基于EasyExcel解析Excel实现文件导出导入读取写入

1. 简介

  Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。easyexcel重写了poi对07版Excel的解析,能够原本一个3M的excel用POI sax依然需要100M左右内存降低到几M,并且再大的excel不会出现内存溢出,03版依赖POI的sax模式,在上层做了模型转换的封装,让使用者更加简单方便。
  64M内存1分钟内读取75M(46W行25列)的Excel:

  更多请参考:https://github.com/alibaba/easyexcel

2. 示例代码

  • 创建工程
  • 修改pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.c3stones</groupId>
	<artifactId>spring-boot-easyexcel-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-easyexcel-demo</name>
	<description>SpringBoot + Easyexcel Demo</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.8.RELEASE</version>
		<relativePath />
	</parent>

	<dependencies>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>easyexcel</artifactId>
			<version>2.2.6</version>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

</project>
  • 创建实体
import java.util.Date;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 学生实体
 * 
 * @author CL
 *
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@HeadRowHeight(20)
@ColumnWidth(20)
@ContentRowHeight(15)
public class Student {

	@ExcelProperty(index = 0, value = "学号")
	private String sno;

	@ExcelProperty(index = 1, value = "姓名")
	private String name;

	@ExcelProperty(index = 2, value = "年龄")
	private Integer age;

	@ExcelProperty(index = 3, value = "性别")
	private String gender;

	@ExcelProperty(index = 4, value = "籍贯")
	private String nativePlace;

	@ExcelProperty(index = 5, value = "入学时间")
	private Date enrollmentTime;

}
  • 创建文件读取类
      该类需要继承抽象类AnalysisEventListener,但是不需要被Spring管理。
import java.util.ArrayList;
import java.util.List;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.c3stones.entity.Student;

import lombok.Getter;

/**
 * 学生读取类
 * 
 * @author CL
 *
 */
public class StudentListener extends AnalysisEventListener<Student> {

	@Getter
	private List<Student> studentList = new ArrayList<Student>();

	public StudentListener() {
		super();
		studentList.clear();
	}

	/**
	 * 每一条数据解析都会调用
	 */
	@Override
	public void invoke(Student student, AnalysisContext context) {
		studentList.add(student);
	}

	/**
	 * 所有数据解析完成都会调用
	 */
	@Override
	public void doAfterAllAnalysed(AnalysisContext context) {
		studentList.forEach(System.out::println);
	}

}
  • 创建文件导出导入Controller示例
import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.excel.EasyExcel;
import com.c3stones.entity.Student;
import com.c3stones.listener.StudentListener;

/**
 * 学生Controller
 * 
 * @author CL
 *
 */
@RestController
@RequestMapping(value = "student")
public class StudentController {

	/**
	 * 导出学生信息
	 * 
	 * @param response
	 * @param request
	 * @throws IOException
	 * @throws ParseException
	 */
	@SuppressWarnings("serial")
	@RequestMapping(value = "export")
	public void exportStudentInfos(HttpServletResponse response, HttpServletRequest request)
			throws IOException, ParseException {
		// 设置响应类型
		response.setContentType("application/vnd.ms-excel");
		// 设置字符编码
		response.setCharacterEncoding("utf-8");
		// 设置响应头信息
		response.setHeader("Content-disposition",
				"attachment;filename*=utf-8\'\'" + URLEncoder.encode("学生花名册", "UTF-8") + ".xlsx");

		List<Student> studentList = new ArrayList<Student>() {
			{
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				add(new Student("1001", "张三", 23, "男", "陕西西安", dateFormat.parse("2020-09-01")));
				add(new Student("1002", "李四", 22, "女", "陕西渭南", dateFormat.parse("2020-09-01")));
			}
		};

		// 写入文件
		EasyExcel.write(response.getOutputStream(), Student.class).sheet("学生信息").doWrite(studentList);
	}

	/**
	 * 导入学生信息
	 * 
	 * @param file
	 * @throws IOException
	 */
	@RequestMapping(value = "import")
	public List<Student> importStudentInfos(MultipartFile file) throws IOException {
		StudentListener studentListener = new StudentListener();
		EasyExcel.read(file.getInputStream(), Student.class, studentListener).sheet().doRead();
		return studentListener.getStudentList();
	}

}
  • 创建文件读取写入Controller示例
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.excel.EasyExcel;
import com.c3stones.entity.Student;
import com.c3stones.listener.StudentListener;

/**
 * 文件Controller
 * 
 * @author CL
 *
 */
@RestController
@RequestMapping(value = "file")
public class FileController {

	/**
	 * 读取Excel
	 * 
	 * @return
	 */
	@RequestMapping(value = "readExcel")
	public List<Student> readExcel() {
		String fileName = "C:\\\\Users\\\\Administrator\\\\Desktop\\\\学生花名册.xlsx";
		StudentListener studentListener = new StudentListener();
		EasyExcel.read(fileName, Student.class, studentListener).sheet().doRead();
		return studentListener.getStudentList();
	}

	/**
	 * 写入Excel
	 * 
	 * @return
	 * @throws ParseException
	 */
	@SuppressWarnings("serial")
	@RequestMapping(value = "writeExcel")
	public Boolean writeExcel() throws ParseException {
		String fileName = "C:\\\\Users\\\\Administrator\\\\Desktop\\\\学生花名册2.xlsx";

		List<Student> studentList = new ArrayList<Student>() {
			{
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				add(new Student("2001", "张三2", 23, "男", "陕西西安", dateFormat.parse("2020-09-01")));
				add(new Student("2002", "李四2", 22, "女", "陕西渭南", dateFormat.parse("2020-09-01")));
			}
		};

		EasyExcel.write(fileName, Student.class).sheet("学生信息2").doWrite(studentList);
		return true;
	}

}
  • 创建启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 启动类
 * 
 * @author CL
 *
 */
@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}
  • 启动项目

3. 测试

  通过Postman依次测试导出、导入、读取和写入:

  • 测试导出

      将导出文件保存到桌面(学生花名册.xlsx)。
  • 测试导入
  • 测试读取
  • 测试写入

      可以看到在代码中配置的文件目录已存在写入成功的文件(学生花名册2.xlsx)。

4. 项目地址

  spring-boot-easyexcel-demo

以上是关于jxl导出Excel报内存溢出...的主要内容,如果未能解决你的问题,请参考以下文章

easyexcel合并单元格导出内存溢出

java 大数据量导出Excel内存溢出,求解答!

POI导出数据内存溢出问题

关于内存溢出遇到的两种情况

java使用jxl操作一百万数据到excel中,分sheet写入(一个sheet写五万)。如何避免内存溢出?

easyExcel导出excel的简单使用