• ADO.NET 读取Excel文件,并作数据源


    项目中需要用的功能,贴上代码了。

    需要注意的地方:配置Web.config的时候要注意版本问题!

    //若是在Web.config中配置数据源,如下
    <add key="ExcelConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0; Data Source='miroExcel/Info.xls';Extended Properties='Excel 8.0;HDR=yes;IMEX=2'"/>
     
            /// <summary>
            /// 读取Excel文件  传入绝对路径  查询条件
            /// </summary>
            /// <returns>DataSet</returns>
            public DataSet GetBoolExcelDAL(string cardId, string excelPath)
            {
                try
                {
                    string strCon = "Provider=Microsoft.Ace.OleDb.12.0; Data Source ='" + excelPath + "';Extended Properties='Excel 12.0;HDR=yes;IMEX=2'";
                    OleDbConnection Conn = new OleDbConnection(strCon);
                    string strsql = string.Format("select * from [Sheet1$] where CardId='{0}'", cardId);
                    Conn.Open();
                    OleDbCommand oleCommand = new OleDbCommand(strsql, Conn);
                    OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);
                    DataSet ds = new DataSet();
                    oleAdapter.Fill(ds, "ExcelData");
                    if (ds != null)
                    {
                        return ds;
                    }
                    else
                    {
                        return null;
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
  • 相关阅读:
    第一周。。。
    新人日报1129
    Daily Report-1126
    How to read source code[repost]
    Markdown tutorial [repost]
    蘑菇街面经
    阿里面经
    百度凤巢一二面经
    Mybatis最入门---代码自动生成(generatorConfig.xml配置)
    Maven的生命周期阶段
  • 原文地址:https://www.cnblogs.com/likeli/p/3884585.html
Copyright © 2020-2023  润新知