上传excel到gridview时system.data.dll错误中出现'system.data.oledb.oledbexception'类型的第一次机会异常 [英] A first chance exception of type 'system.data.oledb.oledbexception' occurred in system.data.dll error while uploading excel to gridview

查看:100
本文介绍了上传excel到gridview时system.data.dll错误中出现'system.data.oledb.oledbexception'类型的第一次机会异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将Excel工作表上传到datagrid时出现上述错误。经过一系列的调试后,它会在我的代码的Connection.Open()部分出现错误,下面是我的代码



我尝试了什么:



I get the above error while trying to upload excel sheet to datagrid. After series of debugging, it throws the error just when it gets to the "Connection.Open()" section of my code, Below is my code

What I have tried:

protected void btnUpload_Click(object sender, EventArgs e)
        {
            string FilePath = ConfigurationManager.AppSettings["FilePath"].ToString();
            string filename = string.Empty;
            
            if (FUpload.HasFile)
            {
                try
                {
                    string[] allowedFile = { ".xls", ".xlsx" };
                    string fileExtension = System.IO.Path.GetExtension(FUpload.PostedFile.FileName);
                    bool isValidFile = allowedFile.Contains(fileExtension);
                    if (!isValidFile)
                    {
                        ErrorMessage.ForeColor = System.Drawing.Color.Red;
                        ErrorMessage.Text = "Please Upload only excel file";
                    }
                    else
                    {
                        filename = Path.GetFileName(Server.MapPath(FUpload.FileName));
                        FUpload.SaveAs(Server.MapPath(FilePath) + filename);
                        string filePath = Server.MapPath(FilePath) + filename;
                        OleDbConnection connection = null;
                        if (fileExtension == ".xls")
                        {
                            connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + "; Extended Properties='Excel 8.0;HDR=YES;'");
                        }
                        else if (fileExtension == ".xslx")
                        {
                 connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + filePath + ";Extended Properties='Excel 12.0 Xml;HDR=YES;'");
                        }
                        connection.Open();
                        DataTable data = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                        string getExcelSheetName = data.Rows[0]["Table_Name"].ToString();
                        OleDbCommand excelCommand = new OleDbCommand(@"SELECT * FROM[ getExcelSheetName$]", connection);
                        OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(excelCommand);
                        DataSet ExcelDataSet = new DataSet();
                        ExcelAdapter.Fill(ExcelDataSet);
                        connection.Close();
                        GridView1.DataSource = ExcelDataSet;
                        GridView1.DataBind();

                    }
                }
                catch (Exception ex)
                {

                }
            }
            else
            {
                ErrorMessage.Text = " Please select a file to upload";
            }
        }

推荐答案

,连接);
OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(excelCommand);
DataSet ExcelDataSet = new DataSet();
ExcelAdapter.Fill(ExcelDataSet);
connection.Close();
GridView1.DataSource = ExcelDataSet;
GridView1.DataBind();

}
}
catch (例外情况)
{

}
}
else
{
ErrorMessage.Text = 请选择要上传的文件;
}
}
", connection); OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(excelCommand); DataSet ExcelDataSet = new DataSet(); ExcelAdapter.Fill(ExcelDataSet); connection.Close(); GridView1.DataSource = ExcelDataSet; GridView1.DataBind(); } } catch (Exception ex) { } } else { ErrorMessage.Text = " Please select a file to upload"; } }


检查此主题外部表格格式不符合预期格式。 [ ^ ]

Microsoft.ACE.OLEDB.12.0'外部表格在Windows 2003服务器上没有预期的格式'错误 [ ^ ]
check this thread External table is not in the expected format.[^]
Microsoft.ACE.OLEDB.12.0 'External table is not in the expected format' error on Windows 2003 server[^]

这篇关于上传excel到gridview时system.data.dll错误中出现'system.data.oledb.oledbexception'类型的第一次机会异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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