c# 操作excel类

这个C#类提供了一种方法,通过使用OleDb来像操作数据库一样操作Excel文件。它包括执行查询、获取数据集、读取数据表以及执行非查询操作的功能。
摘要由CSDN通过智能技术生成
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using System.Data.OleDb;
using System.Configuration;
namespace IOF.CRM.Cancel.Controllers
{
    /// <summary>
    /// 像操作MSSQL数据库一去操作一个Excel文件:)
    /// </summary>
    public class Excel
    {
        public static string TableName = "Sheet1";

        public static string ExcelPath = string.Empty;
        /// <summary>
        /// 返回一个OleDbDataReader对象
        /// </summary>
        /// <param name="excelFileFullPath">Excel文件完路径</param>
        /// <param name="CommondText">查询语句 例如:Select * from [sheet1$]</param>
        /// <returns></returns>
        public static OleDbDataReader ExcuteReader(string excelFileFullPath, string CommondText)
        {
            try
            {
                OleDbConnection oConn = connection(excelFileFullPath);
                oConn.Open();
                OleDbCommand oCmd = new OleDbCommand();
                oCmd.CommandText = CommondText;
                oCmd.Connection = oConn;
                OleDbDataReader oReader = oCmd.ExecuteReader();
                return oReader;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                //oConn.Close();
            }
        }
        /// <summary>
        /// 返回一个DataSet
        /// </summary>
        /// <param name="excelFileFullPath">Excel文件完路径</param>
        /// <param name="CommondText">查询语句  例如:Select * from [sheet1$]</param>
        /// <returns></returns>
        public static DataSet ExcuteDataset(string excelFileFullPath, string CommondText)
        {
            try
            {
                DataSet ds = new DataSet();
                OleDbConnection oConn = connection(excelFileFullPath);
                oConn.Open();
                OleDbCommand oCmd = new OleDbCommand();
                oCmd.CommandText = CommondText;
                oCmd.Connection = oConn;
                OleDbDataAdapter da = new OleDbDataAdapter();
                da.SelectCommand = oCmd;
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                //oConn.Close();
            }
        }

        public static DataTable getExcelSheets(string excelFileFullPath)
        {
            try
            {
                DataTable dt = new DataTable();
                OleDbConnection oConn = connection(excelFileFullPath);
                oConn.Open();
                dt = oConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                //oConn.Close();
            }
        }

        public static int ExecuteNonQuery(string excelFileFullPath, string CommondText)
        {
            OleDbConnection oConn = connection(excelFileFullPath);
            oConn.Open();
            OleDbCommand oCmd = new OleDbCommand();
            oCmd.CommandText = CommondText;
            oCmd.Connection = oConn;
            return oCmd.ExecuteNonQuery();
        }

        private static OleDbConnection connection(string ecellFileFullPath)
        {
            string connstr = @"Provider=Microsoft.Jet.Oledb.4.0;Data Source={0};Extended Properties=Excel 8.0;";
            connstr = string.Format(connstr, ecellFileFullPath);
            OleDbConnection oConn = new OleDbConnection(connstr);
            return oConn;

        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值