java实现excel数据写入
时间: 2023-11-24 22:03:23 浏览: 32
可以使用Apache POI库来实现Java写入Excel数据。首先需要创建一个Workbook对象,然后创建一个Sheet对象,接着创建Row和Cell对象,最后将数据写入Cell中并保存即可。具体实现可以参考以下代码:
```
// 创建Workbook对象
Workbook workbook = new XSSFWorkbook();
// 创建Sheet对象
Sheet sheet = workbook.createSheet("Sheet1");
// 创建Row对象
Row row = sheet.createRow(0);
// 创建Cell对象并写入数据
Cell cell1 = row.createCell(0);
cell1.setCellValue("姓名");
Cell cell2 = row.createCell(1);
cell2.setCellValue("年龄");
// 创建新的Row对象并写入数据
Row row1 = sheet.createRow(1);
Cell cell3 = row1.createCell(0);
cell3.setCellValue("张三");
Cell cell4 = row1.createCell(1);
cell4.setCellValue(20);
// 保存Excel文件
FileOutputStream outputStream = new FileOutputStream("test.xlsx");
workbook.write(outputStream);
outputStream.close();
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)