要导出表格,可以使用Java中的Apache POI库。具体步骤如下:

  1. 添加POI库的依赖。在Maven中,可以添加以下依赖:
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>
  1. 创建工作簿和工作表对象。使用Workbook和Sheet类创建Excel文件和表格对象:
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
  1. 创建表头和填充数据。使用Row和Cell类创建表头和填充数据:
// 创建表头
Row headerRow = sheet.createRow(0);
Cell headerCell1 = headerRow.createCell(0);
headerCell1.setCellValue("姓名");
Cell headerCell2 = headerRow.createCell(1);
headerCell2.setCellValue("年龄");

// 填充数据
Row dataRow = sheet.createRow(1);
Cell dataCell1 = dataRow.createCell(0);
dataCell1.setCellValue("张三");
Cell dataCell2 = dataRow.createCell(1);
dataCell2.setCellValue(20);
  1. 导出表格。使用FileOutputStream类将工作簿对象写入文件:
FileOutputStream outputStream = new FileOutputStream("data.xlsx");
workbook.write(outputStream);
workbook.close();

以上是一个简单的示例,可以根据实际需要进行修改。

java怎么导出表格

原文地址: https://www.cveoy.top/t/topic/7dq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录