package com.zy.asrs.wcs.utils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; /** * Created by vincent on 2/17/2024 */ public class ExcelUtil { public static Workbook create() { // 创建 Workbook 和 Sheet HSSFWorkbook workbook = new HSSFWorkbook(); Sheet sheet = workbook.createSheet("运维报表"); // 创建表头 Row header = sheet.createRow(0); header.createCell(0).setCellValue("姓名"); header.createCell(1).setCellValue("年龄"); header.createCell(2).setCellValue("性别"); header.createCell(3).setCellValue("职位"); // 添加一些数据 Row row1 = sheet.createRow(1); row1.createCell(0).setCellValue("张三"); row1.createCell(1).setCellValue(28); row1.createCell(2).setCellValue("男"); row1.createCell(3).setCellValue("运维工程师"); Row row2 = sheet.createRow(2); row2.createCell(0).setCellValue("李四"); row2.createCell(1).setCellValue(32); row2.createCell(2).setCellValue("女"); row2.createCell(3).setCellValue("项目经理"); Row row3 = sheet.createRow(3); row3.createCell(0).setCellValue("王五"); row3.createCell(1).setCellValue(25); row3.createCell(2).setCellValue("男"); row3.createCell(3).setCellValue("开发工程师"); // 执行单元格自动列宽 for (int i = 0; i <= 3; i++) { sheet.autoSizeColumn(i); } return workbook; } }