poi Excel导出

本文提供了一个使用Apache POI库将数据集导出为Excel文件的示例,包括数据集创建、Excel工作簿与工作表初始化、数据填充以及文件保存过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.需要poi-3.9.jar包

 

2.代码

package com.java.poi;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExportToExcel {
 //设置数据集
 private static List<Student> getStudent() throws Exception {
  List list = new ArrayList();
  SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
  Student user1 = new Student(1, "张三", 16, df.parse("1997-03-12"));
  Student user2 = new Student(2, "李四", 17, df.parse("1996-08-12"));
  Student user3 = new Student(3, "王五", 26, df.parse("1985-11-12"));
  list.add(user1);
  list.add(user2);
  list.add(user3);
  return list;
 }
  //创建表格并导出
 public void createExcel() throws Exception {
  // 第一步,创建一个webbook,对应一个Excel文件
  HSSFWorkbook wb = new HSSFWorkbook();
  // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
  HSSFSheet sheet = wb.createSheet("学生表一");
  // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
  HSSFRow row = sheet.createRow((int) 0);
  // 第四步,创建单元格,并设置值表头 设置表头居中
  HSSFCellStyle style = wb.createCellStyle();
  style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

  HSSFCell cell = row.createCell((short) 0);
  cell.setCellValue("学号");
  cell.setCellStyle(style);
  cell = row.createCell((short) 1);
  cell.setCellValue("姓名");
  cell.setCellStyle(style);
  cell = row.createCell((short) 2);
  cell.setCellValue("年龄");
  cell.setCellStyle(style);
  cell = row.createCell((short) 3);
  cell.setCellValue("生日");
  cell.setCellStyle(style);

  // 第五步,写入实体数据 实际应用中这些数据从数据库得到,
  List list = ExportToExcel.getStudent();
  for (int i = 0; i < list.size(); i++) {
   row = sheet.createRow((int) i + 1);
   Student stu = (Student) list.get(i);
   // 第四步,创建单元格,并设置值
   row.createCell((short) 0).setCellValue((double) stu.getId());
   row.createCell((short) 1).setCellValue(stu.getName());
   row.createCell((short) 2).setCellValue((double) stu.getAge());
   row.createCell((short) 3).setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu.getBirth()));
  }
  // 第六步,将文件存到指定位置
  try {
   FileOutputStream fout = new FileOutputStream("D:/students.xls");
   wb.write(fout);
   fout.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
}

您好!感谢您的提问。关于POIExcel导出模板,您可以参考以下步骤: 1. 首先,确保您已经引入了POIExcel库。您可以在项目的pom.xml文件中添加如下依赖项: ``` <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建一个Excel模板文件,可以使用Microsoft Excel或其他电子表格软件创建。在模板中,设置好表头和样式等内容。 3. 在Java代码中,使用POIExcel库读取模板文件,并进行数据填充。以下是一个简单的示例: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class ExcelExportTemplate { public static void main(String[] args) { String templateFilePath = "path/to/template.xlsx"; // 模板文件路径 String outputFilePath = "path/to/output.xlsx"; // 输出文件路径 try (FileInputStream fis = new FileInputStream(templateFilePath); Workbook workbook = new XSSFWorkbook(fis); FileOutputStream fos = new FileOutputStream(outputFilePath)) { Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表 Row dataRow = sheet.getRow(1); // 获取数据行 Cell cell = dataRow.getCell(0); // 获取第一列单元格 cell.setCellValue("John Doe"); // 填充数据 workbook.write(fos); // 写入输出文件 } catch (IOException e) { e.printStackTrace(); } } } ``` 上述代码中,我们首先使用FileInputStream读取模板文件,然后创建XSSFWorkbook实例表示工作簿。接着,我们获取工作表和数据行,并使用setCellValue方法填充数据。最后,使用FileOutputStream将工作簿写入输出文件。 请注意,以上只是一个简单示例,您可以根据自己的需求进行更复杂的操作,例如循环填充数据、设置样式等。 希望以上信息能对您有所帮助!如有任何疑问,请随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值