Java利用POI實現(xiàn)導(dǎo)入導(dǎo)出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
實現(xiàn)
已經(jīng)在代碼中加入了完整的注釋。
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) {
// 創(chuàng)建Excel表格
createExcel(getStudent());
// 讀取Excel表格
List<Student> list = readExcel();
System.out.println(list.toString());
}
/**
* 初始化數(shù)據(jù)
*
* @return 數(shù)據(jù)
*/
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;
}
/**
* 創(chuàng)建Excel
*
* @param list
* 數(shù)據(jù)
*/
private static void createExcel(List<Student> list) {
// 創(chuàng)建一個Excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
// 創(chuàng)建一個工作表
HSSFSheet sheet = workbook.createSheet("學(xué)生表一");
// 添加表頭行
HSSFRow hssfRow = sheet.createRow(0);
// 設(shè)置單元格格式居中
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 添加表頭內(nèi)容
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);
// 添加數(shù)據(jù)內(nèi)容
for (int i = 0; i < list.size(); i++) {
hssfRow = sheet.createRow((int) i + 1);
Student student = list.get(i);
// 創(chuàng)建單元格,并設(shè)置值
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 數(shù)據(jù)集合
*/
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();
}
// 循環(huán)工作表
for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = workbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循環(huán)行
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow == null) {
continue;
}
// 將單元格中的內(nèi)容存入集合
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
+ "]";
}
}
測試結(jié)果
導(dǎo)出的Excel表格

students
打印讀取的Excel數(shù)據(jù)
[Student [name=小明, age=8, grade=二年級], Student [name=小光, age=9, grade=三年級], Student [name=小花, age=10, grade=四年級]]
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
- java poi導(dǎo)出圖片到excel示例代碼
- Java使用POI實現(xiàn)導(dǎo)出Excel的方法詳解
- Java使用poi組件導(dǎo)出Excel格式數(shù)據(jù)
- Java使用POI導(dǎo)出大數(shù)據(jù)量Excel的方法
- Java中利用POI優(yōu)雅的導(dǎo)出Excel文件詳解
- Java Web使用POI導(dǎo)出Excel的方法詳解
- Java中用POI實現(xiàn)將數(shù)據(jù)導(dǎo)出到Excel
- java poi導(dǎo)出excel時如何設(shè)置手動換行
- Java使用Poi導(dǎo)出Excel表格方法實例
相關(guān)文章
Spring 實現(xiàn)excel及pdf導(dǎo)出表格示例
本篇文章主要介紹了Spring 實現(xiàn)excel及pdf導(dǎo)出表格示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03
Java圖形界面之JFrame,JLabel,JButton詳解
這篇文章主要介紹了Java圖形界面之JFrame、JLabel、JButton詳解,文中有非常詳細的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
SpringBoot整合Dubbo+Zookeeper實現(xiàn)RPC調(diào)用
這篇文章主要給大家介紹了Spring Boot整合Dubbo+Zookeeper實現(xiàn)RPC調(diào)用的步驟詳解,文中有詳細的代碼示例,對我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-07-07
Java如何根據(jù)key值修改Hashmap中的value值
這篇文章主要介紹了Java如何根據(jù)key值修改Hashmap中的value值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
java8 如何實現(xiàn)分組計算數(shù)量和計算總數(shù)
這篇文章主要介紹了java8 如何實現(xiàn)分組計算數(shù)量和計算總數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07

