poi excel读取

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());
            }
            }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值