使用com.alibaba.excel 写一个生成和读取excel的程序示例
时间: 2023-03-11 14:34:07 浏览: 89
可以参考以下示例:1、使用com.alibaba.excel写入Excel的示例:import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.support.ExcelTypeEnum;public class WriteExcel {
public static void main(String[] args) {
String path = "D:\\test.xlsx";
// 实例化写出excel对象
ExcelWriter writer = new ExcelWriter(path, ExcelTypeEnum.XLSX);
// 生成Sheet
Sheet sheet1 = new Sheet(1, 0);
sheet1.setSheetName("第一个sheet");
// 生成表头
Table table1 = new Table(1);
table1.addHeaders("姓名", "年龄");
// 写数据
writer.write0(Arrays.asList("张三", "20"), sheet1, table1);
writer.write0(Arrays.asList("李四", "30"), sheet1, table1);
writer.finish();
}
}2、使用com.alibaba.excel读取Excel的示例:import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;public class ReadExcel {
public static void main(String[] args) {
String path = "D:\\test.xlsx";
// 实例化读取excel对象
ExcelReader reader = new ExcelReader(path, ExcelTypeEnum.XLSX);
// 读取sheet
Sheet sheet1 = new Sheet(1, 0);
// 读取数据
reader.read(sheet1);
reader.finish();
}
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)