poi excel读取
来源:互联网 发布:apache 泛解析 编辑:程序博客网 时间:2024/06/08 15:36
1.需要的包poi-3.9.jar poi-ooxml-3.9.jar
2.代码
package com.java.poi;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
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 org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcel {
//读取xlsx格式
public List<Student> readXlsx(String path) throws IOException {
InputStream is = new FileInputStream(path);
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
Student student = null;
List<Student> list = new ArrayList<Student>();
// Read the Sheet
for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
if (xssfSheet == null) {
continue;
}
// Read the Row
for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
XSSFRow xssfRow = xssfSheet.getRow(rowNum);
if (xssfRow != null) {
student = new Student();
XSSFCell no = xssfRow.getCell(0);
XSSFCell name = xssfRow.getCell(1);
XSSFCell age = xssfRow.getCell(2);
XSSFCell score = xssfRow.getCell(3);
student.setName(getValue(name));
// student.setName(getValue(name));
// student.setAge(getValue(age));
// student.setScore(Float.valueOf(getValue(score)));
list.add(student);
}
}
}
return list;
}
//读取xls格式
public List<Student> readXls(String path) throws IOException {
InputStream is = new FileInputStream(path);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
Student student = null;
List<Student> list = new ArrayList<Student>();
// Read the Sheet
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// Read the Row
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow != null) {
student = new Student();
HSSFCell no = hssfRow.getCell(0);
HSSFCell name = hssfRow.getCell(1);
HSSFCell age = hssfRow.getCell(2);
HSSFCell score = hssfRow.getCell(3);
student.setName(getValue(name));
list.add(student);
}
}
}
return list;
}
private String getValue(XSSFCell xssfRow) {
if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {
return String.valueOf(xssfRow.getBooleanCellValue());
} else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {
return String.valueOf(xssfRow.getNumericCellValue());
} else {
return String.valueOf(xssfRow.getStringCellValue());
}
}
private String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
return String.valueOf(hssfCell.getNumericCellValue());
} else {
return String.valueOf(hssfCell.getStringCellValue());
}
}
}
- POI-----POI读取Excel实例
- POI读取EXCEL
- POI读取EXCEL教程
- POI读取EXCEL
- poi 读取excel
- 通过POI读取Excel
- POI读取Excel
- POI 读取 Excel
- POI读取Excel浅谈
- POI 读取EXcel
- POI读取EXCEL教程
- POI读取Excel
- POI读取Excel
- POI读取Excel浅谈
- POI读取EXCEL教程
- Java POI 读取Excel
- java poi读取excel
- POI读取Excel浅谈
- 多线程四 经典线程同步之互斥量Mutex
- Apache端口复用-模块rootme
- 多线程五 经典线程同步之事件Event
- SOCKET通过代理连接服务器
- mysql分表和表分区详解
- poi excel读取
- 找到循环链表的入口节点
- weblogic详解
- 面试需要注意的十二个得分细节
- AngularJs应用页面切换优化方案
- ionic中隐藏tabs(tabs-item-hide)
- LNMP环境搭建——Nginx篇
- 计算机是怎样跑起来的——互动出版网
- jQuery Ajax 实例 ($.ajax、$.post、$.get)