下面是Java中EasyExcel的使用方式的完整攻略:
1. 简介
EasyExcel是一款基于Java开发的、功能强大的Excel读写解决方案,使用起来方便快捷,支持读写Excel、CSV、HTML、JSON等文件格式,并且具有高性能,内存消耗低等优点,是目前比较流行的Excel读写工具之一。
2. 如何使用
2.1 引入依赖
在项目中引入EasyExcel的相关依赖,可以通过在pom.xml文件中添加如下依赖来引入:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
2.2 Excel读操作
2.2.1 简单读取
首先需要定义实现Excel数据读取的listener类,例如:
public class DemoDataListener extends AnalysisEventListener<DemoData> {
private List<DemoData> list = new ArrayList<>();
public List<DemoData> getList() {
return list;
}
public void setList(List<DemoData> list) {
this.list = list;
}
@Override
public void invoke(DemoData data, AnalysisContext context) {
list.add(data);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
}
}
这里的DemoData是一个自定义的java类,用于存储Excel中每一行的数据:
@Data
public class DemoData {
@ExcelProperty(index = 0)
private String string;
@ExcelProperty(index = 1)
private Date date;
@ExcelProperty(index = 2)
private Double doubleData;
}
假设要从文件中读取Excel数据,可以通过如下代码读取:
// 构建读取对象
DemoDataListener listener = new DemoDataListener();
ExcelReader excelReader = new ExcelReader(inputStream, ExcelTypeEnum.XLSX, null, listener);
// 读取Excel数据
excelReader.read();
// 获取读取到的数据
List<DemoData> list = listener.getList();
然后就可以通过list获取到Excel中读取到的所有数据了。
2.2.2 多sheet读取
假设Excel中有多个sheet,需要逐个读取,可以通过如下代码实现:
// 创建Excel读取工具对象
ExcelReader excelReader = new ExcelReader(inputStream, ExcelTypeEnum.XLSX, null, new DemoDataListener());
// 获取所有sheet的名称
List<Sheet> sheets = excelReader.getSheets();
// 逐个读取sheet数据
for (Sheet sheet : sheets) {
sheetName = sheet.getSheetName();
System.out.println("正在读取: " + sheetName);
// 构建读取对象
DemoDataListener listener = new DemoDataListener();
AnalysisContext analysisContext = new AnalysisContext(sheet.getSheetName(), 0);
excelReader.read(sheet, analysisContext, listener);
// 获取读取到的数据
List<DemoData> list = listener.getList();
}
2.3 Excel写操作
2.3.1 简单写入
通过EasyExcel,可以方便地将数据写入到Excel文件中。假设有一个List
// 创建输出流
OutputStream outputStream = new FileOutputStream("demo.xlsx");
// 创建写入工具对象
ExcelWriter excelWriter = new ExcelWriter(outputStream, ExcelTypeEnum.XLSX);
// 构造ExcelSheet
ExcelSheet sheet = new ExcelSheet();
sheet.setSheetName("sheet1");
sheet.setHead(DemoData.class);
sheet.setDataList(list);
// 将数据写入sheet
excelWriter.write(sheet);
// 关闭流
excelWriter.finish();
outputStream.close();
2.3.2 多sheet写入
如果需要将数据写入到多个sheet中,可以通过如下代码实现:
// 创建输出流
OutputStream outputStream = new FileOutputStream("demo.xlsx");
// 创建写入工具对象
ExcelWriter excelWriter = new ExcelWriter(outputStream, ExcelTypeEnum.XLSX);
// 构造ExcelSheet列表
List<ExcelSheet> sheets = new ArrayList<>();
for (int i = 0; i < 3; i++) {
ExcelSheet excelSheet = new ExcelSheet();
excelSheet.setSheetName("sheet" + i);
excelSheet.setHead(DemoData.class);
excelSheet.setDataList(list);
sheets.add(excelSheet);
}
// 将数据写入每个sheet
for (ExcelSheet sheet : sheets) {
excelWriter.write(sheet);
}
// 关闭流
excelWriter.finish();
outputStream.close();
3. 示范代码
附上读取和写入各个文件格式的示例代码:
// 读取csv文件
InputStream inputStream = new FileInputStream("D:/demo.csv");
CsvReader csvReader = new CsvReader(inputStream, Charset.forName("UTF-8"));
csvReader.readHeaders();
while (csvReader.readRecord()) {
String cellValue = csvReader.get(0);
}
// 写入csv文件
ExcelWriter excelWriter = new ExcelWriter(outputStream, ExcelTypeEnum.CSV);
ExcelSheet sheet = new ExcelSheet();
sheet.setSheetName("Sheet1");
List<String> data = Arrays.asList("hello,"world", "easy", "excel");
sheet.setDataList(Arrays.asList(data));
excelWriter.write(sheet);
excelWriter.finish();
outputStream.close();
// 读取html文件
InputStream inputStream = new URL("http://www.baidu.com").openStream();
ExcelReader excelReader = new ExcelReader(inputStream, ExcelTypeEnum.HTML);
excelReader.read();
// 写入html文件
ExcelWriter excelWriter = new ExcelWriter(outputStream, ExcelTypeEnum.HTML);
ExcelSheet sheet = new ExcelSheet();
sheet.setSheetName("Sheet1");
List<String> data = Arrays.asList("hello,"world", "easy", "excel");
sheet.setDataList(Arrays.asList(data));
excelWriter.write(sheet);
excelWriter.finish();
outputStream.close();
// 读取json文件
InputStream inputStream = new FileInputStream("D:/demo.json");
ExcelReader excelReader = new ExcelReader(inputStream, ExcelTypeEnum.JSON);
excelReader.read();
// 写入json文件
ExcelWriter excelWriter = new ExcelWriter(outputStream, ExcelTypeEnum.JSON);
ExcelSheet sheet = new ExcelSheet();
sheet.setSheetName("Sheet1");
List<String> data = Arrays.asList("hello,"world", "easy", "excel");
sheet.setDataList(Arrays.asList(data));
excelWriter.write(sheet);
excelWriter.finish();
outputStream.close();
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java 中EasyExcel的使用方式 - Python技术站