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

  • 相关阅读:
    JavaScript 中的构造函数
    sql链路服务器提示该事务管理器已经禁止了它对远程/网络事务的支持
    Operating System:操作系统启动总结
    Operating System:信号量
    Operating System:管程相关概念
    ACwing(基础)--- 线性DP、区间DP
    ACwing(基础)--- C++STL库
    ACwing(基础)--- 并查集、堆
    ACwing(基础)--- 数组模拟链表、栈、队列
    Vue 学习笔记2 data数据对象
  • 原文地址:https://www.cnblogs.com/chafe/p/7569670.html
Copyright © 2011-2022 走看看