Excel报表操作在日常办公和生活中应用非常广泛,今天我们来看看Java是怎么操作Excel的。
先分享一下工具类:工具类ExcelOperators,点击下载
欢迎下载使用,更希望自己动动手,写一下。
一、依赖POI
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
二、 工具类源码
主要包含读和写功能,使用方式查看注释。
测试:
static void main(String[] args) throws Exception {
//输入文件
String filePath = "E:\\TestExcelOperator.xlsx";
List<String> filedCodes = Arrays.asList(new String[]{"name", "age", "height"});
Map<String, Object> result = readExcelSimple(
new FileInputStream(new File(filePath)), filedCodes, ExcelOperators.XLSX);
System.out.println(JSON.toJSONString(result));
//输出文件
String writeFilePath = "E:\\WriteExcelOperator.xlsx";
writeExcelSimple(new FileOutputStream(new File(writeFilePath)),
(Map<String, String>)result.get("fileds"), (List<Map<String, Object>>) result.get("datas"), 100);
}