源代码如下图
public class PoiTest { public static void main(String[] args) throws IOException { Workbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet("new sheet"); Row headerRow = sheet.createRow(0); // 创建表头行 headerRow.createCell(0).setCellValue("姓名"); headerRow.createCell(1).setCellValue("性别"); headerRow.createCell(2).setCellValue("年龄"); Row row = null; for (int i = 1; i < 10; i++) { // 设置几行数据 row = sheet.createRow(i); // 创建表头行 row.createCell(0).setCellValue("张三"); row.createCell(1).setCellValue("男"); row.createCell(2).setCellValue(20); } OutputStream fileOut = null; File file = new File("D:/poitest/workbook.xls"); File fileParent = file.getParentFile(); if (!fileParent.exists()) { fileParent.mkdirs(); } fileOut = new FileOutputStream(file); wb.write(fileOut); fileOut.close(); }}