java读取excel文件

package com.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

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 {
    
    public static void main(String[] args) {
        readExcel();
    }
    
    public static void readExcel(){
        FileInputStream excelFileInputStream = null;
        try {
            excelFileInputStream = new FileInputStream("C:\Users\shafei\Desktop\test4.xlsx");
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(excelFileInputStream);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        XSSFSheet sheet = workbook.getSheetAt(0);
        for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
            // XSSFRow 代表一行数据
            XSSFRow row = sheet.getRow(rowIndex);
            if (row == null) {
                continue;
            }
            XSSFCell nameCell = row.getCell(0); // 姓名列
            XSSFCell genderCell = row.getCell(1); // 性别列
            XSSFCell ageCell = row.getCell(2); // 年龄列
            XSSFCell weightCell = row.getCell(3); // 体重列
            XSSFCell salaryCell = row.getCell(4); // 收入列
            XSSFCell addressCell = row.getCell(5); // 收入列
            StringBuilder employeeInfoBuilder = new StringBuilder();
            employeeInfoBuilder.append("员工信息 --> ")
            .append("姓名 : ").append(nameCell.getStringCellValue())
            .append(" , 性别 : ").append(genderCell.getStringCellValue())
            .append(" , 年龄 : ").append(ageCell.getNumericCellValue())
            .append(" , 体重(千克) : ").append(weightCell.getNumericCellValue())
            .append(" , 月收入(元) : ").append(salaryCell.getNumericCellValue())
            .append(" ,地址 : ").append(addressCell.getStringCellValue());
            System.out.println(employeeInfoBuilder.toString());
        }
        try {
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
}

相关jar包地址:http://pan.baidu.com/s/1eSvQVa6

原文地址:https://www.cnblogs.com/chafe/p/7569670.html