1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 通过poi实现解析并读取excel文件(包含xls xlsx后缀)

通过poi实现解析并读取excel文件(包含xls xlsx后缀)

时间:2021-04-22 10:15:23

相关推荐

通过poi实现解析并读取excel文件(包含xls xlsx后缀)

StudentBean.java类,用来存放学生信息

package com.demoexcel.util;public class StudentBean {private String no; //学号private String name; //姓名private String age; //年龄private float score; //成绩public StudentBean() {super();}public StudentBean(String no, String name, String age, float score) {super();this.no = no;this.name = name;this.age = age;this.score = score;}public String getNo() {return no;}public void setNo(String no) {this.no = no;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public float getScore() {return score;}public void setScore(float score) {this.score = score;}}

ReadExcel.java类,用来解析并读取excel文件

package com.demoexcel.util;import java.io.FileInputStream;import java.io.FileNotFoundException;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 {/*** 读取excel中的数据* @param path* @return List<StudentBean>* @author zhang -08-18 00:08*/public List<StudentBean> readExcel(String path) {if (path != null && !path.equals("")) {String ext = getExt(path);if (ext!=null && !ext.equals("")) {if (ext.equals("xls")) {return readXls(path);} else if (ext.equals("xlsx")) {return readXlsx(path);}}}return new ArrayList<StudentBean>();}/*** 读取后缀为xls的excel文件的数据* @param path* @return List<StudentBean>* @author zhang -08-18 00:10*/private List<StudentBean> readXls(String path) {HSSFWorkbook hssfWorkbook = null;try {InputStream is = new FileInputStream(path);hssfWorkbook = new HSSFWorkbook(is);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}StudentBean studentBean = null;List<StudentBean> list = new ArrayList<StudentBean>();if (hssfWorkbook != null) {// Read the Sheetfor (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);if (hssfSheet == null) {continue;}// Read the Rowfor (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {HSSFRow hssfRow = hssfSheet.getRow(rowNum);if (hssfRow != null) {studentBean = new StudentBean();HSSFCell no = hssfRow.getCell(0);HSSFCell name = hssfRow.getCell(1);HSSFCell age = hssfRow.getCell(2);HSSFCell score = hssfRow.getCell(3);studentBean.setNo(getValue(no));studentBean.setName(getValue(name));studentBean.setAge(getValue(age));studentBean.setScore(Float.valueOf(getValue(score)));list.add(studentBean);}}}}return list;}/*** 读取后缀为xlsx的excel文件的数据* @param path* @return List<StudentBean>* @author zhang -08-18 00:08*/private List<StudentBean> readXlsx(String path) {XSSFWorkbook xssfWorkbook = null;try {InputStream is = new FileInputStream(path);xssfWorkbook = new XSSFWorkbook(is);} catch (IOException e) {e.printStackTrace();}StudentBean studentBean = null;List<StudentBean> list = new ArrayList<StudentBean>();if(xssfWorkbook!=null){// Read the Sheetfor (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);if (xssfSheet == null) {continue;}// Read the Rowfor (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {XSSFRow xssfRow = xssfSheet.getRow(rowNum);if (xssfRow != null) {studentBean = new StudentBean();XSSFCell no = xssfRow.getCell(0);XSSFCell name = xssfRow.getCell(1);XSSFCell age = xssfRow.getCell(2);XSSFCell score = xssfRow.getCell(3);studentBean.setNo(getValue(no));studentBean.setName(getValue(name));studentBean.setAge(getValue(age));studentBean.setScore(Float.valueOf(getValue(score)));list.add(studentBean);}}}}return list;}/*** 获取文件扩展名* @param path* @return String* @author zhang -08-17 23:26*/private String getExt(String path) {if (path == null || path.equals("") || !path.contains(".")) {return null;} else {return path.substring(path.lastIndexOf(".") + 1, path.length());}}/*** 判断后缀为xlsx的excel文件的数据类型* @param xssfRow* @return String* @author zhang -08-18 00:12*/@SuppressWarnings("static-access")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());}}/*** 判断后缀为xls的excel文件的数据类型* @param hssfCell* @return String* @author zhang -08-18 00:12*/@SuppressWarnings("static-access")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());}}public static void main(String[] args) {System.out.println(new ReadExcel().readExcel("D://student_info.xlsx").size());}}

excel文件中数据形式:

以上代码经测试有效,先说明几个注意点:

1、文件名为student_info.xlsx,存放在D盘根目录下(这个可以自行更改,也可去测试xls后缀类型的文件)。

2、若直接测试的话,文件格式需与上面图片中的一致,若想按自己的需求则只需修改readXlsx(String path)和readXls(String path)方法中的少量代码即可,很容易看明白。

3、还需添加相应的jar包才行

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。