写一个excle导出的工具类
时间: 2024-02-07 17:02:50 浏览: 42
好的,下面是一个简单的Excel导出工具类,可以将Java对象列表导出为Excel文件。
```java
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelExporter {
public static <T> void export(List<T> list, String[] headers, String[] fields, String filePath) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
// Create header row
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerFont.setColor(IndexedColors.WHITE.getIndex());
CellStyle headerCellStyle = workbook.createCellStyle();
headerCellStyle.setFont(headerFont);
headerCellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
headerCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
Row headerRow = sheet.createRow(0);
for (int i = 0; i < headers.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(headers[i]);
cell.setCellStyle(headerCellStyle);
}
// Create data rows
for (int i = 0; i < list.size(); i++) {
T item = list.get(i);
Row row = sheet.createRow(i + 1);
for (int j = 0; j < fields.length; j++) {
try {
Field field = item.getClass().getDeclaredField(fields[j]);
field.setAccessible(true);
Object value = field.get(item);
if (value != null) {
Cell cell = row.createCell(j);
if (value instanceof Number) {
cell.setCellValue(((Number) value).doubleValue());
} else if (value instanceof String) {
cell.setCellValue((String) value);
} else {
cell.setCellValue(value.toString());
}
}
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
// Auto size columns
for (int i = 0; i < headers.length; i++) {
sheet.autoSizeColumn(i);
}
// Write Excel file
FileOutputStream outputStream = new FileOutputStream(filePath);
workbook.write(outputStream);
workbook.close();
outputStream.close();
}
}
```
使用该工具类时,需要传入一个Java对象列表、一个字符串数组作为表头、一个字符串数组作为字段名,以及输出文件路径。该工具类会自动将Java对象列表转换为Excel文件,并写入到指定的输出文件中。
例如,如果有一个Student类,包含name、age和score三个字段,可以使用以下代码将Student列表导出为Excel文件:
```java
public class Student {
private String name;
private int age;
private double score;
// getters and setters
}
List<Student> list = new ArrayList<>();
list.add(new Student("Alice", 18, 90.0));
list.add(new Student("Bob", 20, 80.0));
list.add(new Student("Charlie", 22, 70.0));
ExcelExporter.export(list, new String[]{"Name", "Age", "Score"}, new String[]{"name", "age", "score"}, "students.xlsx");
```
该代码将会生成一个名为students.xlsx的Excel文件,包含三列数据:Name、Age和Score。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)