zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    docker学习
    redis哨兵部署
    HUE中一些重要元数据表的DDL整理
    Autosys中ON_HOLD和ON_ICE的区别
    Spark结构化API的执行过程——Logical Plan & Physical Plan
    关于Spark中Columns的引用方法
    关于Spark Dataset API中的Typed transformations和Untyped transformations
    关于Kafka Consumer 与 Partitions
    使用sed根据变量值注释掉文件中相匹配的记录行
    sqoop export to teradata时出现java.lang.NullPointerException
  • 原文地址:https://www.cnblogs.com/chafe/p/7569670.html
Copyright © 2011-2022 走看看