写一个将list集合写入excel中的Java的demo
时间: 2024-05-14 07:13:42 浏览: 97
java操作excel表格的demo
4星 · 用户满意度95%
好的,以下是将list集合写入excel中的Java的demo代码:
```java
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class WriteListToExcel {
public static void writeListToExcel(List<String> list, String filePath) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("List Data");
int rowNum = 0;
for (String data : list) {
Row row = sheet.createRow(rowNum++);
int cellNum = 0;
Cell cell = row.createCell(cellNum++);
cell.setCellValue(data);
}
FileOutputStream outputStream = new FileOutputStream(filePath);
workbook.write(outputStream);
workbook.close();
}
}
```
请注意,此代码使用了Apache POI库来处理Excel文件。如果你没有安装该库,请在项目中添加以下依赖项:
```xml
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
```
好了,现在是时候讲一个笑话了:为什么猪不能上天?因为它们不会飞!
阅读全文