且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将excel导入到sqlserver,并将列转换为row。

更新时间:2023-01-21 18:01:03

您可以使用OLEDB数据提供者来使用它。

互联网上有很多解决方案。您需要关注最适合您要求的任何文章之一。您甚至可以下载演示项目(如果有的话)并查看代码。

请检查这些链接 -

如何使用ASP.Net将Excel工作表数据导入SQL Server [ ^ ]

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

将Excel数据导入ASP.NET中的SQL Server [ ^ ]



希望,它会有所帮助:)

Hi, how to import excel to sql server using asp.net
I am using OLEDB Connection to import data.
The code-behind is

protected void BtnUpload_Click(object sender, EventArgs e)
        {
            string excelPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName);

            FileUpload1.SaveAs(excelPath);

            string conString = string.Empty;

            string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);

            switch (extension)
            {
                case ".xls": //Excel 97-03

                    conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;

                    break;

                case ".xlsx": //Excel 07 or higher

                    conString = ConfigurationManager.ConnectionStrings["Excel07+ConString"].ConnectionString;

                    break;
            }

            conString = string.Format(conString, excelPath);

            using (OleDbConnection excel_con = new OleDbConnection(conString))
            {

                excel_con.Open();

                string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();

                DataTable dtExcelData = new DataTable();                
                
 string st = "Select [sno], [Id],[country],[Qtr]=case(substring([Qtr],1,2) as nvarchar(4)), [value]  from ["+sheet1+"] outer apply(values((N'Q1',Q1),(N'Q2',Q2)) P(Qtr,value)";              
  using (OleDbDataAdapter oda = new OleDbDataAdapter(st, excel_con))

                {
                    oda.Fill(dtExcelData);
                }

                excel_con.Close();

                string consString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

                using (SqlConnection con = new SqlConnection(consString))
                {

                    using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
                    {

                        //Set the database table name

                        sqlBulkCopy.DestinationTableName = "[dbo].[exceldemo]";
                       
                        con.Open();

                        sqlBulkCopy.WriteToServer(dtExcelData);

                        con.Close();

                        lblUpload.Text = "Uploaded Succesfully";

                    }





while running the code I am getting the below error
"IErrorInfo.GetDescription failed with E_FAIL(0x80004005)."

at "oda.Fill(dtExcelData);"

below is the input and output format.
http://i61.tinypic.com/217pk0.jpg[^]

please help me from this situation.



Thanks in advance.

You can use this using OLEDB dataprovider.
There are lots of solutions available over the internet. You need to follow one of any article which best suits your requirement. You can even download the demo projects (if any) and go through the code.
Please check these links-
How to Import Excel Sheet data into SQL Server using ASP.Net[^]
Import MS Excel data to SQL Server table using C#[^]
Import Excel Data to SQL Server in ASP.NET[^]

Hope, it helps :)