java读取Excel —— XSSFWorkbook 找不到该类

做一个Excel表格的读取时导入 org.apache.poi 包后居然提示 

XSSFWorkbook
找不到:

java读取Excel —— XSSFWorkbook 找不到该类_第1张图片

原来是还需要下载一个jar包: poi-ooxml 包   ,之后在引入相关类即可:

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

写到这里就顺便把读取Excel表格的方法也贴上来:

package com.test;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class readExcels {
    private Workbook wb;        //工作谱
    private Sheet sheet;        //工作表
    private Row row;            //工作行

    //读取的Excel文件路径
    private final static String path = "D:\\infor.xlsx";

    //读取Excels表格
    public readExcels(String filepath) {
        if(filepath==null){
            return;
        }
        String ext = filepath.substring(filepath.lastIndexOf("."));
        try {
            InputStream is = new FileInputStream(filepath);
            if(".xls".equals(ext)){
                wb = new HSSFWorkbook(is);
            }else if(".xlsx".equals(ext)){
                wb = new XSSFWorkbook(is);
            }else{
                wb=null;
            }
        } catch (FileNotFoundException e) {
            System.out.println("FileNotFoundException" + e);
        } catch (IOException e) {
            System.out.println("IOException" + e);
        }
    }


    /**
     * 读取Excel表格表头的内容
     * @return String 表头内容的数组
     */
    public String[] readExcelTitle() throws Exception{
        if(wb==null){
            throw new Exception("Workbook对象为空!");
        }
        sheet = wb.getSheetAt(0);
        row = sheet.getRow(0);

        // 标题总列数
        int colNum = row.getPhysicalNumberOfCells();
        System.out.println("colNum:" + colNum);
        String[] title = new String[colNum];
        for (int i = 0; i < colNum; i++) {

            //修改单元格的数据格式,获取表头信息
            row.getCell(i).setCellType(Cell.CELL_TYPE_STRING);
            title[i] = row.getCell(i).getStringCellValue();
        }
        return title;
    }

    /**
     * 读取Excel数据内容
     *
     * @return Map 包含单元格数据内容的Map对象
     * @author zengwendong
     */
    public List readExcelContent() throws Exception{
        if(wb==null){
            throw new Exception("Workbook对象为空!");
        }

        sheet = wb.getSheetAt(0);
        int rowNum = sheet.getLastRowNum();
        row = sheet.getRow(0);
        int colNum = row.getPhysicalNumberOfCells();
        List userList = new ArrayList();
        userInfo user = null;
        // 正文内容应该从第二行开始,第一行为表头的标题
        for (int i = 1; i <= rowNum; i++) {
            user = new userInfo();
            user.setUserId(String.valueOf(sheet.getRow(i).getCell(0)));
            user.setUserName(String.valueOf(sheet.getRow(i).getCell(1)));
            user.setUserEmail(String.valueOf(sheet.getRow(i).getCell(2)));
            user.setUserTel(String.valueOf(sheet.getRow(i).getCell(3)));
            userList.add(user);
        }
        return userList;
    }

    public static void main(String args[]){
        readExcels re = new readExcels(path);
        try{

            String[] title = re.readExcelTitle();
            for (String ele:title)
                System.out.print(ele + "                ");
            System.out.println();

            List li = re.readExcelContent();

            for(userInfo ele : li){
                System.out.println(ele.toString());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //内部类
    class userInfo{
        private String userId;          //用户ID
        private String userName;        //用户名
        private String userEmail;       //用户邮箱
        private String userTel;         //用户电话

        public userInfo() {

        }

        public String getUserId() {
            return userId;
        }

        public void setUserId(String userId) {
            this.userId = userId;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getUserEmail() {
            return userEmail;
        }

        public void setUserEmail(String userEmail) {
            this.userEmail = userEmail;
        }

        public String getUserTel() {
            return userTel;
        }

        public void setUserTel(String userTel) {
            this.userTel = userTel;
        }

        @Override
        public String toString() {
            return "userId:" + userId + " userName:" + userName + " userEmail:" + userEmail + " userTel" + userTel;
        }
    }
}

Excel表格信息:

java读取Excel —— XSSFWorkbook 找不到该类_第2张图片

输出:

java读取Excel —— XSSFWorkbook 找不到该类_第3张图片



你可能感兴趣的:(java)