利用Apache POI 实现简单的Excel表格导出

1、利用POI API实现简单的Excel表格导出

首先假设一个学生实体类:

package com.sun.poi.domain;

import java.io.Serializable;
import java.util.Date;
public class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    //学生ID
    private int studentId;
    //学生姓名
    private String name;
    //年龄
    private int age;
    //出生年月
    private Date birth;
    public Student() {

    }
    public Student(int studentId, String name, int age, Date birth) {
        super();
        this.studentId = studentId;
        this.name = name;
        this.age = age;
        this.birth = birth;
    }
    public int getStudentId() {
        return studentId;
    }
    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
}

利用POI 实现Excel导出功能:

package com.sun.poi.fuction;

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;
import com.sun.poi.domain.Student;

public class CreateSimpleExcelToDisk {

    private static SimpleDateFormat sdf;

    private static List<Student> getStudent() throws Exception{
        List<Student> studentList = new ArrayList<Student>();
        sdf = new SimpleDateFormat("yyyy-mm-dd");

        Student stu1 = new Student(1, "张三", 12, sdf.parse("1999-12-30"));
        Student stu2 = new Student(1, "李四", 12, sdf.parse("2000-4-20"));
        Student stu3 = new Student(1, "王二", 12, sdf.parse("2003-12-15"));
        Student stu4 = new Student(1, "麻子", 12, sdf.parse("1989-11-22"));
        Student stu5 = new Student(1, "铁蛋", 12, sdf.parse("1999-5-11"));
        studentList.add(stu1);
        studentList.add(stu2);
        studentList.add(stu3);
        studentList.add(stu4);
        studentList.add(stu5);
        return studentList;
    }

    public static void main(String[] args) throws Exception {
        //获取数据源
        List<Student> studentList = getStudent();
        //1、创建一个webbook,表示一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //2、在Excel文件中添加一个sheet页
        HSSFSheet sheet = wb.createSheet("学生信息表");
        //3、在sheet页中添加表头,第一行
        HSSFRow row = sheet.createRow((int)0);
        //4、设置单元格的样式
        HSSFCellStyle style = wb.createCellStyle();
        //设置样式居中
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //5、创建一个单元格,并设置居中样式
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("学号");
        cell.setCellStyle(style);
        cell = row.createCell(1);
        cell.setCellValue("姓名");
        cell.setCellStyle(style);
        cell = row.createCell(2);
        cell.setCellValue("年龄");
        cell.setCellStyle(style);
        cell = row.createCell(3);
        cell.setCellValue("生日");
        cell.setCellStyle(style);

        for (int i = 0; i < studentList.size(); i++) {
            row = sheet.createRow(i + 1);
            Student stu = (Student)studentList.get(i);
            row.createCell(0).setCellValue(stu.getStudentId());
            row.createCell(1).setCellValue(stu.getName());
            row.createCell(2).setCellValue(stu.getAge());
            row.createCell(3).setCellValue(sdf.format(stu.getBirth()));
        }

        try {
            //6、导出到磁盘上
            FileOutputStream fout = new FileOutputStream("D:/student.xls");
            wb.write(fout);
            fout.close();
            System.out.println("导出excel成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值