• 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

  • 相关阅读:
    iOS开发之Quartz2D 二:绘制直线,曲线,圆弧,矩形,椭圆,圆
    统计英文文章中各单词的频率,打印频率最高的十个单词(C语言实现)
    Android protectionLevel
    nmon for linux
    UFT 小技巧
    adb测试使用相关
    Java 反射
    Android USB安全调试
    android.process.acore和system进程
    Android 屏幕截图
  • 原文地址:https://www.cnblogs.com/chafe/p/7569670.html
Copyright © 2020-2023  润新知