EasyExcel is a Java-based, fast, concise Excel processing tool that solves the memory overflow of large files. It allows you to quickly complete functions such as reading and writing Excel without considering factors such as performance and memory.
Java stores array data into Excel based on EasyExcel
foreword
This article refers to the content of the repeated writing part in Excel written on the official website . The purpose of this article is to store the calculated one-dimensional or two-dimensional arrays into different Sheets of the same Excel.
EasyExcel writes Excel files
1. Import dependencies
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.1</version>
</dependency>
2. Write array to Excel file
When EasyExcel writes to Excel, the type of the written data needs to be List<Map<>>
type, so we need to convert the array into the specified data type first, as shown below:
public static List<Map<Integer,Double>> get_exportdata_list(String sheet,Double[] data1, Double[][] data2)
{
List<Map<Integer,Double>> list = new LinkedList<>();
if(data1!=null) //数据为一维
{
for(int i=0;i<data1.length;i++)
{
Map<Integer, Double> map = new HashedMap<>() ;
map.put(0, data1[i]);
list.add(map);
}
}
else
{
for(int i=0;i<data2.length;i++)
{
Map<Integer, Double> map = new HashedMap<>() ;
for(int j=0;j<data2[0].length;j++)
{
map.put(j, data2[i][j]);
}
list.add(map);
}
}
return list;
}
The first item of the function is the table name, the second item is a one-dimensional array, and the third item is a two-dimensional array. There can only be one item of the two, and the other item isnull
For example, our array {1.0,2.0,3.0,4.0}
is returned after running the program [{4=4.0, 3=3.0, 2=2.0, 1=1.0}
, and each item in the sequence is a hash table, which is a key-value pair type.
2.1 Method 1: Combine all arrays and write them together
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections4.map.HashedMap;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
//import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
public class ExportDemo {
static Double[][] d = {
{
1.0,2.0,3.0},{
4.0,5.0,6.0}};
static Double[] a = {
6.0,7.0,8.0};
static String[] sheet = {
"d","a"}; //sheet名
public static void main(String[] args) {
//将数组数据转化为列表
List<List<Map<Integer,Double>>> LIST = new LinkedList<>(); //创建一个总List存放两个数组返回的List
List<Map<Integer, Double>> list1 = new LinkedList<>();
list1 = get_exportdata_list("d",null,d);
System.out.println("list1="+list1);
LIST.add(list1);
list1 = get_exportdata_list("a",a,null);
System.out.println("list2="+list1);
LIST.add(list1);
System.out.println("LIST="+LIST);
//写入.xlsx文件
try (ExcelWriter excelWriter = EasyExcel.write("data/demo.xlsx").build()) //创建excel
{
//去调用写入,有几个数组就循环几次
for(int i=0;i<2;i++)
{
WriteSheet writeSheet = EasyExcel.writerSheet(sheet[i]).needHead(false).build();
// 将数组写入sheet[i]
excelWriter.write(LIST.get(i), writeSheet);
}
}
}
public static List<Map<Integer,Double>> get_exportdata_list(String sheet,Double[] data1, Double[][] data2)
{
List<Map<Integer,Double>> list = new LinkedList<>();
if(data1!=null) //数据为一维
{
for(int i=0;i<data1.length;i++)
{
Map<Integer, Double> map = new HashedMap<>() ;
map.put(0, data1[i]);
list.add(map);
}
}
else
{
for(int i=0;i<data2.length;i++)
{
Map<Integer, Double> map = new HashedMap<>() ;
for(int j=0;j<data2[0].length;j++)
{
map.put(j, data2[i][j]);
}
list.add(map);
}
}
return list;
}
}
The output after running is as follows:
The generated data/demo.xlsx
file looks like this:
This way of writing has a flaw, that is, it is impossible to add data to the existing Excel. If you need to add data, you can use the following method.
2.2 Method 2: Adding an array to Excel
This way is to make a copy of the existing Excel, then write the new data into the copy file, and finally delete the original file and rename the copy file to be the same as the original file.
This way you can both write data to a new Excel and append new data on the original Excel.
Suppose we have data/demo.xlsx
something like this:
Run the following program:
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections4.map.HashedMap;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
//import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
public class ExportDemo2 {
static Double[][] d = {
{
1.0,2.0,3.0},{
4.0,5.0,6.0}};
static List<List<Map<Integer,Double>>> list = new LinkedList<>();
public static void main(String[] args) {
//将数组数据转化为列表
exportdata("d",null,d);
//exportdata("c",a,null);
}
public static void exportdata(String sheet,Double[] data1, Double[][] data2)
{
List<Map<Integer,Double>> list = new LinkedList<>();
if(data1!=null) //数据为一维
{
for(int i=0;i<data1.length;i++)
{
Map<Integer, Double> map = new HashedMap<>() ;
map.put(0, data1[i]);
list.add(map);
}
}
else
{
for(int i=0;i<data2.length;i++)
{
Map<Integer, Double> map = new HashedMap<>() ;
for(int j=0;j<data2[0].length;j++)
{
map.put(j, data2[i][j]);
}
list.add(map);
}
}
String filePath="data"; //路径
String fileName="demo.xlsx"; //目标excel文件名
ExcelWriter excelWriter = null;
File destFile = new File(filePath, fileName);
File transFile = new File(filePath, "test.xlsx"); //这个文件名取为什么都可以,中转文件
System.out.println(destFile);
System.out.println(transFile);
try {
if (destFile.exists())
{
//创建中转文件
//追加数据,中转文件与目标文件不能是同一个文件名
//withTemplate()指定模板文件,即复制一份; file() 中间文件名; autoCloseStream() 必须为True,自动关闭输入流
excelWriter = EasyExcel.write().withTemplate(destFile)
//.file() 指定目标文件,不能与模板文件是同一个文件
.file(transFile).autoCloseStream(true).build(); //
}
else
{
excelWriter = EasyExcel.write(destFile).build();
}
WriteSheet writeSheet = EasyExcel.writerSheet(sheet).needHead(false).build();
excelWriter.write(list, writeSheet);
}
finally
{
if (excelWriter != null)
{
excelWriter.finish();
}
}
if (transFile.exists())
{
//删除原模板文件,新生成的文件变成新的模板文件
destFile.delete();
boolean result = transFile.renameTo(destFile);
if(result)System.out.println("改名成功");
else System.out.println("改名失败");
}
}
}
The output is as follows:
data/demo.xlsx
The content in is as follows:
You can see that the array d is written to Excel.