java导出excel
时间: 2023-09-05 21:07:44 浏览: 31
Java可以使用Apache POI库来导出Excel文件。以下是一个简单的示例代码:
```java
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
public class ExcelExporter {
public static void main(String[] args) {
try (Workbook workbook = new HSSFWorkbook()) {
Sheet sheet = workbook.createSheet("Sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Hello, World!");
FileOutputStream outputStream = new FileOutputStream("output.xls");
workbook.write(outputStream);
workbook.close();
System.out.println("Excel file generated successfully!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
此代码将在项目根目录下生成名为“output.xls”的Excel文件,并将单元格A1设置为“Hello,World!”
相关推荐
![](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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)