无法读取Server Machine上的Excel工作表

时间:2015-05-20 09:43:49

标签: c# asp.net-mvc-4

我无法在服务器上阅读我的Excel工作表..当我在本地运行此代码时工作正常...但无法在服务器上运行此方法...  错误显示"对象引用未设置为实例"



  public DataTable GetRead_Excel(string strExcelFile)
        {



            System.Data.OleDb.OleDbConnection OledbCon = new System.Data.OleDb.OleDbConnection();
            System.Data.OleDb.OleDbCommand OledbCmd = new System.Data.OleDb.OleDbCommand();
            System.Data.OleDb.OleDbDataAdapter OledbDAdapter = new System.Data.OleDb.OleDbDataAdapter();
            DataTable dt_1 = new DataTable();
            dt_1.Columns.Add("ID", typeof(int));
            dt_1.Rows.Add(25);
            OledbCon.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source= " + strExcelFile.Trim() + " ;Extended Properties=\"Excel 8.0;HDR=NO\"";

            OledbCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.12.0; Data Source= " + strExcelFile.Trim() + " ;Extended Properties=\"Excel 8.0;HDR=NO\"";


            if (System.IO.File.Exists(strExcelFile.Trim()) == false)
            {
                strErrorCode = "Invalid Excel Data file / File dose not exists";
                return null;
            }

            try
            {
                OledbCon.Open();

                OledbCmd.Connection = OledbCon;
                OledbCmd.CommandTimeout = pSQL_CommandTimeOut;
                OledbCmd.CommandType = CommandType.Text;
                OledbCmd.CommandText = "SELECT * FROM [Sheet1$]";
                OledbDAdapter.SelectCommand = OledbCmd;
                OledbDAdapter.Fill(dt_1);
                OledbDAdapter.Dispose();
                OledbCmd.Dispose();
                OledbCon.Close();

            }
            catch (Exception ex)
            {
                strErrorCode = Get_ErrorString(ex.Message);
                return null;
            }

            return dt_1;
        }




0 个答案:

没有答案