程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java應用POI完成導入導出Excel表格示例代碼

Java應用POI完成導入導出Excel表格示例代碼

編輯:關於JAVA

Java應用POI完成導入導出Excel表格示例代碼。本站提示廣大學習愛好者:(Java應用POI完成導入導出Excel表格示例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是Java應用POI完成導入導出Excel表格示例代碼正文


引見

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表格


students

打印讀取的Excel數據

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

總結

以上就是這篇文章的全體內容了,願望本文的內容對年夜家的進修或許任務能帶來必定的贊助,假如有疑問年夜家可以留言交換。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved