zoukankan      html  css  js  c++  java
  • java从Excle中读取数据集

     pom.xml:

            <dependency>
                <groupId>net.sourceforge.jexcelapi</groupId>
                <artifactId>jxl</artifactId>
                <version>2.6.12</version>
            </dependency>
    GetExcelInfo:
    package  com.baosight.wisdomsf.lite.persist.system.syndata.util;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    import jxl.Cell;
    import jxl.Sheet;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;
    import jxl.WorkbookSettings;
    
    /**
     * @Author: Yuan.
     **/
    public class GetExcelInfo {
    
        public static void main(String[] args) {
            String filePath =  "C:\Users\Lenovo\Desktop\测试读取文件\机构信息.xls";
            // 获取文件后缀名
            /*String fileTyle=filePath.substring(filePath.lastIndexOf("."),filePath.length());
            System.out.println(fileTyle);*/
            GetExcelInfo obj = new GetExcelInfo();
            // 这个是excel数据文件
            // 多表 - 方法
            /*List list = obj.readExcelMany(filePath);
            System.out.println(list);*/
            // 单表
            try {
                //得到所有数据
                List<List<String>> allData = readExcelSingle(filePath);
                System.out.println(allData);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        // 去读Excel的方法readExcel,该方法的入口参数为一个File对象
        public static List readExcelMany(String filePath) {
            File file = new File(filePath);
            List<String> resultList = new ArrayList<String>();
            try {
                // 创建输入流,读取Excel
                InputStream is = new FileInputStream(file.getAbsolutePath());
    
                // jxl提供的Workbook类
                //Workbook wb = Workbook.getWorkbook(is);//这样会出现乱码,改成下面的这种形式
    
                WorkbookSettings workbookSettings = new WorkbookSettings();
                workbookSettings.setEncoding("ISO-8859-1");
                Workbook wb= Workbook.getWorkbook(is,workbookSettings);
    
                // 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);
                            resultList.add(cellinfo);
                        }
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (BiffException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return resultList;
        }
    
        /**
         * 获取数据 - 读取单个sheet表
         * @param filePath
         * @return
         * @throws Exception
         */
        public static List<List<String>> readExcelSingle(String filePath) throws Exception {
            File file = new File(filePath);
            // 创建输入流,读取Excel
            InputStream is = new FileInputStream(file.getAbsolutePath());
    
            // jxl提供的Workbook类
            //Workbook wb = Workbook.getWorkbook(is);//这样会出现乱码,改成下面的这种形式
    
            WorkbookSettings workbookSettings = new WorkbookSettings();
            workbookSettings.setEncoding("ISO-8859-1");
            Workbook wb= Workbook.getWorkbook(is,workbookSettings);
    
            //只有一个sheet,直接处理,创建一个Sheet对象
            Sheet sheet = wb.getSheet(0);
            // 得到所有的行数
            int rows = sheet.getRows();
            // 所有的数据
            List<List<String>> allData = new ArrayList<List<String>>();
            // 越过第一行 它是列名称
            for (int j = 1; j < rows; j++) {
                List<String> oneData = new ArrayList<String>();
                // 得到每一行的单元格的数据
                Cell[] cells = sheet.getRow(j);
                for (int k = 0; k < cells.length; k++) {
                    oneData.add(cells[k].getContents().trim());
                }
                // 存储每一条数据
                allData.add(oneData);
                // 打印出每一条数据
                //System.out.println(oneData);
            }
            return allData;
        }
    }
    原:科技改变生活!
  • 相关阅读:
    Linux 添加环境变量
    postgresql 获取修改列的值
    5月30日周一上午
    周日5月29日
    2016年5月26日
    如何使用Gson(添加到项目里去)
    linux内核分析课程总结()待完善
    5月5日离散课笔记
    4月28日的离散课(还少了一部分)
    2016年4月29日
  • 原文地址:https://www.cnblogs.com/iyuanpeng/p/15523947.html
Copyright © 2011-2022 走看看