示例#1
0
        public string GetElement(object col, int row)
        {
            ExcelFileException.ThrowIfStringNull((string)col);
            if ((int)col < 0 | row < 0)
            {
                throw new ExcelFileException($"Столбцы могут быть только положительные.");
            }
            string res = null;

            switch (col.GetType().ToString())
            {
            case "System.String":
            {
                if (IsExistColumn((string)col))
                {
                    res = data.Rows[row][(string)col].ToString();
                }
                break;
            }

            case "System.Int32":
            {
                res = data.Rows[row][(int)col].ToString();
                break;
            }

            default:
                throw new Exception($"Форма колонки не правильно задан.");
            }
            return(res);
        }
示例#2
0
        public ExcelFile(string fullpath)
        {
            FileInfo file = new FileInfo(fullpath);

            ExcelFileException.ThrowIfFileNotExsist(fullpath, file);
            extension     = file.Extension;
            this.fullpath = file.FullName;
            filename      = file.Name;
            State         = Status.Initialized;
        }
示例#3
0
        public bool IsExistSheet(string name)
        {
            int i = 0;

            ExcelFileException.ThrowIfStringNull(name);
            foreach (string sheet in sheetnames.Split(';'))
            {
                if (sheet == name)
                {
                    i++;
                }
            }
            return(i > 0 ? true : false);
        }
示例#4
0
        public bool IsExistColumn(string name)
        {
            int i = 0;

            ExcelFileException.ThrowIfStringNull(name);
            foreach (string colname in columns.Split(';'))
            {
                if (colname == name)
                {
                    i++;
                }
            }
            return(i > 0 ? true : false);
        }
示例#5
0
        public static DataTable ReadData(ExcelFile excel, string sheetname)
        {
            sheetname += "$";
            ExcelFileException.ThrowIfSheetNotExist(excel, sheetname);
            var res = new DataTable();

            using (var connection = new OleDbConnection(excel.ConnectionStr))
            {
                connection.Open();
                if (connection.State == ConnectionState.Open)
                {
                    var cmd = new OleDbCommand()
                    {
                        Connection = connection, CommandText = $"SELECT *FROM [{sheetname}]"
                    };
                    OleDbDataReader reader = cmd.ExecuteReader();
                    res.Load(reader);
                    reader.Close();
                    connection.Close();
                }
            }
            ReadDataFinished?.Invoke(null, new ReadDataEventArgs(sheetname, excel.FileName, res));
            return(res);
        }