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

  • 相关阅读:
    转:qcow2、raw、vmdk等镜像格式
    openstack配置xen
    透过 Linux 内核看无锁编程
    转:查看进程所消耗的内存
    Ubuntu安装内核源码
    How to mark volume groups as active or inactive
    Failed to access IIS metabase(IIS Exception Problem)
    Android电子书下载:Google Android SDK开发范例大全(第2版)pdf+源码
    几个C++单元测试框架
    JavaScript命名空间namespace的实现方法
  • 原文地址:https://www.cnblogs.com/chafe/p/7569670.html
Copyright © 2011-2022 走看看