java代码实现导出excel
时间: 2024-05-14 21:13:34 浏览: 80
java实现excel的导出
Sure, here is an example Java code to export data to an Excel file using Apache POI library:
```
// Create a Workbook
Workbook workbook = new XSSFWorkbook();
// Create a sheet
Sheet sheet = workbook.createSheet("Sheet1");
// Create a header row
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue("Name");
headerRow.createCell(1).setCellValue("Age");
headerRow.createCell(2).setCellValue("Email");
// Create some data rows
Row dataRow = sheet.createRow(1);
dataRow.createCell(0).setCellValue("John Doe");
dataRow.createCell(1).setCellValue(30);
dataRow.createCell(2).setCellValue("john.doe@example.com");
// Save the workbook to a file
String filename = "data.xlsx";
try (OutputStream outputStream = new FileOutputStream(filename)) {
workbook.write(outputStream);
System.out.println("Data exported to " + filename + " successfully!");
} catch (IOException e) {
System.out.println("Failed to export data to " + filename);
e.printStackTrace();
}
```
Hope this helps!
阅读全文