使用Excel工作表导入数据在Server中不起作用 [英] Importing data using Excel sheet not working in Server

查看:70
本文介绍了使用Excel工作表导入数据在Server中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码使用Excel工作表导入数据。它在localhost中工作正常,但不在服务器中。我不知道原因。该文件存储在一个文件夹中,但没有发生数据获取和保存在数据库中。服务器是否需要安装MS Excel?。oconn.Open()函数没有发生,从他们唯一的错误是来了..Plz给我一个解决方案。

I am using the following code to import data using Excel sheet. It is working fine in localhost, but not in server. I d't know the reason. The file is storing in a folder, but the data fetching and saving in database is not happening. Is the server need to install the MS Excel ?.the oconn.Open() function is not happening,from their only the error is comming..Plz gve me a solution.

string extension = Path.GetExtension(FileUpload1.FileName);
                filename = DateTime.Now.ToString("dd-MM-yyyy") + "_" + DateTime.Now.Hour + "_" + DateTime.Now.Minute + "_" + DateTime.Now.Millisecond + extension;
                FileUpload1.SaveAs(Server.MapPath("~/Account/PublishedRanklist/") + filename);
                string path = Server.MapPath("~/Account/PublishedRanklist/" + filename);
                OleDbConnection oconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=Excel 8.0");
               
                    OleDbCommand ocmd = new OleDbCommand("select * from [Sheet1$]", oconn);
                    oconn.Open();
                    OleDbDataReader odr = ocmd.ExecuteReader();
                    while (odr.Read())
                   {       int Regno=valid(odr, 0);

                   }

                  oconn.Close();










protected string valid(OleDbDataReader myreader, int stval)//if any columns are found null then they are replaced by zero
        {
            object val = myreader[stval];
            if (val != DBNull.Value)
                return val.ToString();
            else
                return Convert.ToString(0);
        }

推荐答案

,oconn);
oconn.Open();
OleDbDataReader odr = ocmd.ExecuteReader();
while (odr.Read())
{ int Regno =有效(odr, 0 );

}

oconn.Close ();
", oconn); oconn.Open(); OleDbDataReader odr = ocmd.ExecuteReader(); while (odr.Read()) { int Regno=valid(odr, 0); } oconn.Close();










protected string valid(OleDbDataReader myreader, int stval)//if any columns are found null then they are replaced by zero
        {
            object val = myreader[stval];
            if (val != DBNull.Value)
                return val.ToString();
            else
                return Convert.ToString(0);
        }


要使用Oledb阅读Excel,需要安装Microsoft Excel。



而不是使用DataReader

For reading the excel using Oledb you do not require Microsoft Excel to be installed.

Instead of using DataReader
OleDbDataReader odr = ocmd.ExecuteReader();



尝试使用Datatable并检查数据是否在DataTable中填充?


try using Datatable and check if data are getting filled in DataTable or not?


请参考以下链接:



导入MS Excel数据使用C#到SQL Server表[ ^ ]

http://stackoverflow.com/questions/18006318/how-to-import-all-the-excel-sheets-to-dataset-in- c-sharp [ ^ ]
Please refer below link(s):

Import MS Excel data to SQL Server table using C#[^]
http://stackoverflow.com/questions/18006318/how-to-import-all-the-excel-sheets-to-dataset-in-c-sharp[^]


这篇关于使用Excel工作表导入数据在Server中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆