easyexcel导出excel jdk7
时间: 2023-07-21 09:45:06 浏览: 132
使用 EasyExcel 导出 Excel 需要的依赖包如下:
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.7</version>
</dependency>
```
在 JDK7 中,需要在代码中手动关闭 Excel 写入流和读取流,否则会出现资源未释放的问题。示例代码如下:
```java
// 导出 Excel
OutputStream out = null;
try {
out = new FileOutputStream("demo.xlsx");
ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
Sheet sheet = new Sheet(1, 0, DemoData.class);
writer.write(getData(), sheet);
writer.finish();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 导入 Excel
InputStream in = null;
try {
in = new FileInputStream("demo.xlsx");
ExcelReader reader = new ExcelReader(in, ExcelTypeEnum.XLSX, null, new DemoDataListener());
reader.read(new Sheet(1, 1, DemoData.class));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
阅读全文