如何将Excel文件转换为DataGrid? [英] How to Convert the Excel File to DataGrid?

查看:150
本文介绍了如何将Excel文件转换为DataGrid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我正在使用C#.net将Excel文件转换为DataGrid以进行客户端请求.

当我使用页面将excel将excel转换为本地主机上的datagrid时,它可以正常工作,但在发布站点时将不起作用.

错误是
System.Data.OleDb.OleDbException:Microsoft Jet数据库引擎无法打开文件''.它已经由另一个用户专门打开,或者您需要权限才能查看其数据.

代码是

Hi to everybody.

I am using C#.net to convert Excel file to DataGrid for Client Request.

When I use the page to convert the excel to datagrid at localhost it works properly, but it will not work when I publish the site.

The error is
System.Data.OleDb.OleDbException: The Microsoft Jet database engine cannot open the file ''''. It is already opened exclusively by another user, or you need permission to view its data.

The code is

protected void Upload_Click(object sender, EventArgs e)
        {
            try
            {
                strFileName = fleupForQuotation.PostedFile.FileName;
                if (strFileName.Trim() != "")
                {
                    string folderpath = "F://Upload//";
                    string attachedFile = fleupForQuotation.PostedFile.FileName;
                    if (Request.Browser.Browser == "IE")
                    {
                        fleupForQuotation.SaveAs(folderpath + fleupForQuotation.FileName);
                        strFileName = folderpath + fleupForQuotation.FileName;
                    }
                    else
                    {
                        fleupForQuotation.SaveAs(folderpath + attachedFile);
                        strFileName = folderpath + attachedFile;
                    }
                    hdnFileName.Value = strFileName;
                    

                }
                else
                {
                }
            }
            catch (Exception ex)
            {
                SRCMError.ErrorLog(ex.ToString());
                SRCMError.ExceptionToEventLog(ex);
            }


            
            string ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0;" + @"data source=" + strFileName + ";" + "Extended Properties=Excel 8.0;";

            OleDbConnection conn = new OleDbConnection(ConnectionString);

            conn.Open();

            try
            {


                OleDbDataAdapter oda = new OleDbDataAdapter("select * from [Sheet1$]", conn);

                DataSet ds = new DataSet();

                oda.Fill(ds, "Details");

                grdExceltoGrid.DataSource = ds;

                grdExceltoGrid.DataBind();

            }

            catch (Exception ex)
            {

                Response.Write(ex.Message);

            }

            finally
            {

                conn.Close();

            }

        }


请帮我解决这个问题..

非常感谢


Please help me to solve this problem..

Thanks a lot

推荐答案

",conn); DataSet ds = DataSet(); oda.Fill(ds," ); grdExceltoGrid.DataSource = ds; grdExceltoGrid.DataBind(); } 捕获(例外) { Response.Write(ex.Message); } 最终 { conn.Close(); } }
", conn); DataSet ds = new DataSet(); oda.Fill(ds, "Details"); grdExceltoGrid.DataSource = ds; grdExceltoGrid.DataBind(); } catch (Exception ex) { Response.Write(ex.Message); } finally { conn.Close(); } }


请帮我解决这个问题..

非常感谢


Please help me to solve this problem..

Thanks a lot


检查运行您的应用程序的帐户是否有权访问该文件.
Check if the account under which your application runs has right to access the file.


这篇关于如何将Excel文件转换为DataGrid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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