使用java写一个生成excel的代码
时间: 2023-04-11 20:00:30 浏览: 49
可以的,你可以使用Apache POI库来生成Excel文件。以下是一个简单的Java代码示例:
```
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelGenerator {
public static void main(String[] args) {
try {
// 创建工作簿
Workbook workbook = new XSSFWorkbook();
// 创建工作表
Sheet sheet = workbook.createSheet("Sheet1");
// 创建行
Row row = sheet.createRow(0);
// 创建单元格
Cell cell = row.createCell(0);
cell.setCellValue("Hello World!");
// 保存文件
FileOutputStream outputStream = new FileOutputStream("output.xlsx");
workbook.write(outputStream);
workbook.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
这个代码将在当前目录下生成一个名为“output.xlsx”的Excel文件,并在第一个单元格中写入“Hello World!”。