java传递值生成EXCEL
时间: 2023-10-18 15:11:16 浏览: 32
要在Java中生成Excel文件,可以使用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) throws Exception {
// 创建一个新的 Excel 工作簿
Workbook workbook = new XSSFWorkbook();
// 创建一个工作表
Sheet sheet = workbook.createSheet("Sheet1");
// 创建行和单元格,并在单元格中添加数据
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Hello");
cell = row.createCell(1);
cell.setCellValue("World");
// 保存 Excel 文件
FileOutputStream outputStream = new FileOutputStream("output.xlsx");
workbook.write(outputStream);
outputStream.close();
System.out.println("Excel 文件已生成。");
}
}
```
这个程序将在当前目录下创建一个名为 `output.xlsx` 的 Excel 文件,其中包含一行两列的数据。你可以根据自己的需要修改程序来生成你想要的 Excel 文件。