通过POI组件把数据写入到Excel文件中

本文介绍了一个使用Java和Apache POI库将学生数据写入Excel文件的方法。通过创建一个包含学生姓名、性别、年龄、班级和成绩的数据模型,并将其填充到Excel工作表中。此过程包括初始化数据、创建Excel工作簿、工作表、行和单元格,最后保存文件。

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

CODE:
package com.way.poi.writetoexcel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.way.poi.bean.Student;
/**
 * 
 * @author fenzaiway
 * 
 * 模拟数据,把数据写入到Excel中、
 *
 */
public class GenerateExcel {
 
 private static String xlsPath = "D:\\student.xls";
 private static List<Student> studentList = null;
 private static Student[] students = new Student[4];
 
 /**
  * 静态块初始化数据
  */
 static{
  studentList = new ArrayList<Student>();
  students[0] = new Student("张三","男",23,"一班",94);
  students[1] = new Student("李四","女",22,"二班",90);
  students[2] = new Student("王五","男",21,"五班",84);
  students[3] = new Student("赵六","女",24,"三班",98);
  studentList.addAll(Arrays.asList(students));
 }
 
 /**
  * 主函数
  */
 public static void main(String[] args) {
  long start = System.currentTimeMillis();
  generateExcel(xlsPath);
  long end = System.currentTimeMillis();
  System.out.println("耗时:" + (end -start) + "毫秒");
 }
 
 //创建Excel文件
 public static void generateExcel(String path){
  //创建工作簿对象
  HSSFWorkbook wb = new HSSFWorkbook();
  
  //创建工作表对象并命名
  HSSFSheet sheet = wb.createSheet("学生信息统计表");
  
  //遍历集合对象创建行和单元格
  for(int i = 0; i < studentList.size(); i++){
   //取出Student对象
   Student student = studentList.get(i);
   //创建行
   HSSFRow row = sheet.createRow(i);
   //创建单元格并赋值
   HSSFCell nameCell = row.createCell(0);
   nameCell.setCellValue(student.getName());
   HSSFCell genderCell = row.createCell(1);
   genderCell.setCellValue(student.getGender());
   HSSFCell ageCell = row.createCell(2);
   ageCell.setCellValue(student.getAge());
   HSSFCell sclassCell = row.createCell(3);
   sclassCell.setCellValue(student.getSclass());
   HSSFCell scoreCell = row.createCell(4);
   sclassCell.setCellValue(student.getScore());
  }
  
  //生成文件
  File file = new File(path);
  FileOutputStream fos = null;
  try {
   fos = new FileOutputStream(file);
   wb.write(fos);
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
   if(fos != null){
    try {
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
  
 }
}
bean:
package com.way.poi.bean;
public class Student {
 private String name;
 private String gender;
 private int age;
 private String sclass;
 private int score;
 
 public Student(){
  super();
 }
 
 public Student(String name, String gender, int age, String sclass, int score) {
  super();
  this.name = name;
  this.gender = gender;
  this.age = age;
  this.sclass = sclass;
  this.score = score;
 }
 public String toString(){
  return "Student [age= " + age +",gender= " + gender +",name= " + name +",sclass= " + sclass +",score= " + score +"]";
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getGender() {
  return gender;
 }
 public void setGender(String gender) {
  this.gender = gender;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public String getSclass() {
  return sclass;
 }
 public void setSclass(String sclass) {
  this.sclass = sclass;
 }
 public int getScore() {
  return score;
 }
 public void setScore(int score) {
  this.score = score;
 }
 
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值