Java 如何将List<String> 集合导到excel中
时间: 2023-06-07 08:10:18 浏览: 62
您可以使用一些现有的 Java 库,比如 Apache POI,来实现将 List<String> 集合导出到 Excel 中。以下是一个简单的示例:
```
// 创建工作簿和工作表
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
// 创建表头
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue("列1");
headerRow.createCell(1).setCellValue("列2");
// 循环遍历 List,将数据填充到行中
int rowNum = 1;
for (String str : list) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(str.split(",")[0]);
row.createCell(1).setCellValue(str.split(",")[1]);
}
// 输出至文件
FileOutputStream outputStream = new FileOutputStream("output.xlsx");
workbook.write(outputStream);
workbook.close();
outputStream.close();
```
相关问题
springboot将list<Map>集合导出为,固定列名,将map的键和值作为两列内容,自动分配列宽的Excel文件
可以使用Apache POI来实现将List<Map>集合导出为Excel文件。
步骤如下:
1. 创建Excel工作簿和工作表。
```java
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
```
2. 创建表头,并设置列宽。
```java
Row headerRow = sheet.createRow(0);
CellStyle headerStyle = workbook.createCellStyle();
headerStyle.setAlignment(HorizontalAlignment.CENTER);
headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerStyle.setFont(headerFont);
int colNum = 0;
for (String key : dataList.get(0).keySet()) {
Cell cell = headerRow.createCell(colNum++);
cell.setCellValue(key);
cell.setCellStyle(headerStyle);
sheet.setColumnWidth(colNum, (key.length() + 4) * 256);
}
```
3. 填充数据。
```java
int rowNum = 1;
for (Map<String, Object> map : dataList) {
Row row = sheet.createRow(rowNum++);
int colNum = 0;
for (String key : map.keySet()) {
Cell cell = row.createCell(colNum++);
cell.setCellValue(String.valueOf(map.get(key)));
}
}
```
4. 将工作簿写入输出流中。
```java
workbook.write(outputStream);
workbook.close();
```
完整代码示例:
```java
public void exportExcel(List<Map<String, Object>> dataList, OutputStream outputStream) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
Row headerRow = sheet.createRow(0);
CellStyle headerStyle = workbook.createCellStyle();
headerStyle.setAlignment(HorizontalAlignment.CENTER);
headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerStyle.setFont(headerFont);
int colNum = 0;
for (String key : dataList.get(0).keySet()) {
Cell cell = headerRow.createCell(colNum++);
cell.setCellValue(key);
cell.setCellStyle(headerStyle);
sheet.setColumnWidth(colNum, (key.length() + 4) * 256);
}
int rowNum = 1;
for (Map<String, Object> map : dataList) {
Row row = sheet.createRow(rowNum++);
int colNum = 0;
for (String key : map.keySet()) {
Cell cell = row.createCell(colNum++);
cell.setCellValue(String.valueOf(map.get(key)));
}
}
workbook.write(outputStream);
workbook.close();
}
```
若依的java架构导出excel,对象属性为list<map>怎么办
如果你需要将一个对象属性为`List<Map<String, Object>>`的Java对象导出为Excel文件,可以使用Apache POI来实现。
下面是一个简单的示例代码:
```
public void exportToExcel(List<Map<String, Object>> dataList, String filePath) throws IOException {
// 创建工作簿对象
Workbook workbook = new XSSFWorkbook();
// 创建工作表对象
Sheet sheet = workbook.createSheet("Sheet1");
// 创建表头行
Row headerRow = sheet.createRow(0);
// 获取第一个Map对象的所有key作为表头
Map<String, Object> firstData = dataList.get(0);
int columnIndex = 0;
for (String key : firstData.keySet()) {
Cell headerCell = headerRow.createCell(columnIndex);
headerCell.setCellValue(key);
columnIndex++;
}
// 写入数据行
int rowIndex = 1;
for (Map<String, Object> data : dataList) {
Row dataRow = sheet.createRow(rowIndex);
columnIndex = 0;
for (String key : firstData.keySet()) {
Cell dataCell = dataRow.createCell(columnIndex);
dataCell.setCellValue(data.get(key).toString());
columnIndex++;
}
rowIndex++;
}
// 保存工作簿到文件
FileOutputStream outputStream = new FileOutputStream(filePath);
workbook.write(outputStream);
outputStream.close();
}
```
使用该方法,你可以将`List<Map<String, Object>>`类型的数据写入Excel文件,并保存到指定路径。你只需要将`dataList`替换为你的数据集合,`filePath`替换为你要保存的文件路径即可。
相关推荐
![cs](https://img-home.csdnimg.cn/images/20210720083646.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)