欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java利用POI实现导入导出Excel表格示例代码

程序员文章站 2024-03-12 08:17:02
介绍 jakarta poi 是一套用于访问微软格式文档的java api。jakarta poi有很多组件组成,其中有用于操作excel格式文件的hssf和用于操作wo...

介绍

jakarta poi 是一套用于访问微软格式文档的java api。jakarta poi有很多组件组成,其中有用于操作excel格式文件的hssf和用于操作word的hwpf,在各种组件中目前只有用于操作excel的hssf相对成熟。官方主页http://poi.apache.org/index.html,api文档http://poi.apache.org/apidocs/index.html

实现

已经在代码中加入了完整的注释。

import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.inputstream;
import java.io.outputstream;
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 exceloperate {

  public static void main(string[] args) {
    // 创建excel表格
    createexcel(getstudent());

    // 读取excel表格
    list<student> list = readexcel();
    system.out.println(list.tostring());
  }

  /**
   * 初始化数据
   * 
   * @return 数据
   */
  private static list<student> getstudent() {
    list<student> list = new arraylist<student>();
    student student1 = new student("小明", 8, "二年级");
    student student2 = new student("小光", 9, "三年级");
    student student3 = new student("小花", 10, "四年级");
    list.add(student1);
    list.add(student2);
    list.add(student3);
    return list;
  }

  /**
   * 创建excel
   * 
   * @param list
   *      数据
   */
  private static void createexcel(list<student> list) {
    // 创建一个excel文件
    hssfworkbook workbook = new hssfworkbook();
    // 创建一个工作表
    hssfsheet sheet = workbook.createsheet("学生表一");
    // 添加表头行
    hssfrow hssfrow = sheet.createrow(0);
    // 设置单元格格式居中
    hssfcellstyle cellstyle = workbook.createcellstyle();
    cellstyle.setalignment(hssfcellstyle.align_center);

    // 添加表头内容
    hssfcell headcell = hssfrow.createcell(0);
    headcell.setcellvalue("姓名");
    headcell.setcellstyle(cellstyle);

    headcell = hssfrow.createcell(1);
    headcell.setcellvalue("年龄");
    headcell.setcellstyle(cellstyle);

    headcell = hssfrow.createcell(2);
    headcell.setcellvalue("年级");
    headcell.setcellstyle(cellstyle);

    // 添加数据内容
    for (int i = 0; i < list.size(); i++) {
      hssfrow = sheet.createrow((int) i + 1);
      student student = list.get(i);

      // 创建单元格,并设置值
      hssfcell cell = hssfrow.createcell(0);
      cell.setcellvalue(student.getname());
      cell.setcellstyle(cellstyle);

      cell = hssfrow.createcell(1);
      cell.setcellvalue(student.getage());
      cell.setcellstyle(cellstyle);

      cell = hssfrow.createcell(2);
      cell.setcellvalue(student.getgrade());
      cell.setcellstyle(cellstyle);
    }

    // 保存excel文件
    try {
      outputstream outputstream = new fileoutputstream("d:/students.xls");
      workbook.write(outputstream);
      outputstream.close();
    } catch (exception e) {
      e.printstacktrace();
    }
  }

  /**
   * 读取excel
   * 
   * @return 数据集合
   */
  private static list<student> readexcel() {
    list<student> list = new arraylist<student>();
    hssfworkbook workbook = null;

    try {
      // 读取excel文件
      inputstream inputstream = new fileinputstream("d:/students.xls");
      workbook = new hssfworkbook(inputstream);
      inputstream.close();
    } catch (exception e) {
      e.printstacktrace();
    }

    // 循环工作表
    for (int numsheet = 0; numsheet < workbook.getnumberofsheets(); numsheet++) {
      hssfsheet hssfsheet = workbook.getsheetat(numsheet);
      if (hssfsheet == null) {
        continue;
      }
      // 循环行
      for (int rownum = 1; rownum <= hssfsheet.getlastrownum(); rownum++) {
        hssfrow hssfrow = hssfsheet.getrow(rownum);
        if (hssfrow == null) {
          continue;
        }

        // 将单元格中的内容存入集合
        student student = new student();

        hssfcell cell = hssfrow.getcell(0);
        if (cell == null) {
          continue;
        }
        student.setname(cell.getstringcellvalue());

        cell = hssfrow.getcell(1);
        if (cell == null) {
          continue;
        }
        student.setage((int) cell.getnumericcellvalue());

        cell = hssfrow.getcell(2);
        if (cell == null) {
          continue;
        }
        student.setgrade(cell.getstringcellvalue());

        list.add(student);
      }
    }
    return list;
  }
}

附上student类的代码

public class student {

  private string name;
  private int age;
  private string grade;

  public student() {
  }

  public student(string name, int age, string grade) {
    super();
    this.name = name;
    this.age = age;
    this.grade = grade;
  }

  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 string getgrade() {
    return grade;
  }

  public void setgrade(string grade) {
    this.grade = grade;
  }

  @override
  public string tostring() {
    return "student [name=" + name + ", age=" + age + ", grade=" + grade
        + "]";
  }
}

测试结果

导出的excel表格

Java利用POI实现导入导出Excel表格示例代码
students

打印读取的excel数据

[student [name=小明, age=8, grade=二年级], student [name=小光, age=9, grade=三年级], student [name=小花, age=10, grade=四年级]]

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。