zoukankan      html  css  js  c++  java
  • java--读取excel中数据

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package readexcel;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import jxl.Sheet;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;
    public class GetExcelInfo {
        public static void main(String[] args) {
            GetExcelInfo obj = new GetExcelInfo();
            // 此处为我创建Excel路径:E:/zhanhj/studysrc/jxl下
            File file = new File("G:/公共性文档/2018-9-30在校学生名单 (1).XLS");
            obj.readExcel(file);
        }
        // 去读Excel的方法readExcel,该方法的入口参数为一个File对象
        public void readExcel(File file) {
            try {
                // 创建输入流,读取Excel
                InputStream is = new FileInputStream(file.getAbsolutePath());
                // jxl提供的Workbook类
                Workbook wb = Workbook.getWorkbook(is);
                // Excel的页签数量
                int sheet_size = wb.getNumberOfSheets();
                for (int index = 0; index < sheet_size; index++) {
                    // 每个页签创建一个Sheet对象
                    Sheet sheet = wb.getSheet(index);
                    // sheet.getRows()返回该页的总行数
                    for (int i = 0; i < sheet.getRows(); i++) {
                        // sheet.getColumns()返回该页的总列数
                        for (int j = 0; j < sheet.getColumns(); j++) {
                            String cellinfo = sheet.getCell(j, i).getContents();
                            System.out.println(cellinfo);
                        }
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } 
            catch (BiffException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    测试成功:

  • 相关阅读:
    Android 在资源文件(res/strings.xml)定义一维数组,间接定义二维数组
    Android 设计模式之MVC模式
    Android Studio 1.0首次安装遇到的问题,无法下载SDK
    android 项目中规范使用SharedPreferences
    Android 在布局容器中动态添加控件
    Android ble 蓝牙4.0 总结一
    Mac Android签名生成keystore
    Android ndk 加载简单的gif 图像
    Android EventBus
    c语言内存
  • 原文地址:https://www.cnblogs.com/Catherinezhilin/p/9808123.html
Copyright © 2011-2022 走看看