请用java帮我写一个excel导出代码
时间: 2023-12-21 14:04:21 浏览: 40
以下是一个示例代码,可以帮助您实现Excel导出功能:
```
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.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat;
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 void export(List<MyData> dataList, String filename) throws IOException {
// Create a workbook
Workbook workbook = new XSSFWorkbook();
// Create a sheet
Sheet sheet = workbook.createSheet();
// Create a header row
Row headerRow = sheet.createRow(0);
Cell headerCell = headerRow.createCell(0);
headerCell.setCellValue("Name");
headerCell = headerRow.createCell(1);
headerCell.setCellValue("Age");
headerCell = headerRow.createCell(2);
headerCell.setCellValue("Email");
// Create a cell style for date format
CreationHelper createHelper = workbook.getCreationHelper();
CellStyle dateCellStyle = workbook.createCellStyle();
dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd/MM/yyyy"));
// Create rows for data
int rowNumber = 1;
for (MyData data : dataList) {
Row row = sheet.createRow(rowNumber++);
Cell cell = row.createCell(0);
cell.setCellValue(data.getName());
cell = row.createCell(1);
cell.setCellValue(data.getAge());
cell = row.createCell(2);
cell.setCellValue(data.getEmail());
}
// Autosize columns
sheet.autoSizeColumn(0);
sheet.autoSizeColumn(1);
sheet.autoSizeColumn(2);
// Write the workbook to a file
FileOutputStream fileOut = new FileOutputStream(filename);
workbook.write(fileOut);
fileOut.close();
// Close the workbook
workbook.close();
}
public static class MyData {
private String name;
private int age;
private String email;
public MyData(String name, int age, String email) {
this.name = name;
this.age = age;
this.email = email;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getEmail() {
return email;
}
}
}
```
在上面的代码中,`export`方法接受一个数据列表和一个文件名作为参数,并将数据列表导出为Excel文件。在此示例中,我们使用了Apache POI库来创建和操作Excel文件。在`export`方法中,我们首先创建一个`Workbook`和一个`Sheet`对象,然后创建一个标题行和一些数据行。对于日期格式,我们创建了一个单元格样式,并使用`autoSizeColumn`方法自动调整列宽。最后,我们将工作簿写入文件,并关闭它。如果您想要使用不同的数据类型或格式,请修改代码以满足您的需求。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)