java导出excel
时间: 2023-07-26 10:04:00 浏览: 25
你可以使用Apache POI库来导出Excel文件。下面是一个简单的示例代码:
```java
// 创建工作簿对象
Workbook workbook = new XSSFWorkbook();
// 创建工作表对象
Sheet sheet = workbook.createSheet("Sheet1");
// 创建表头行
Row headerRow = sheet.createRow(0);
// 创建表头单元格
Cell headerCell = headerRow.createCell(0);
headerCell.setCellValue("姓名");
headerCell = headerRow.createCell(1);
headerCell.setCellValue("年龄");
// 创建数据行并填充数据
Row dataRow = sheet.createRow(1);
Cell dataCell = dataRow.createCell(0);
dataCell.setCellValue("张三");
dataCell = dataRow.createCell(1);
dataCell.setCellValue(20);
// 导出Excel文件
FileOutputStream outputStream = new FileOutputStream("test.xlsx");
workbook.write(outputStream);
outputStream.close();
workbook.close();
```
这个示例代码创建了一个工作簿对象和一个工作表对象,并在工作表中创建了表头和数据行。最后,使用`FileOutputStream`将工作簿对象写入到文件中。你可以根据自己的需求修改代码来导出不同的Excel文件。
相关推荐
![](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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)