如何将excel导入到sqlserver,并将列转换为row。 [英] how to import excel to sqlserver with column transforming to row.

查看:95
本文介绍了如何将excel导入到sqlserver,并将列转换为row。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用asp.net将excel导入sql server

我正在使用OLEDB Connection导入数据。

代码隐藏是



 受保护 无效 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 (扩展名)
{
案例 。xls // < span class =code-comment> Excel 97-03

conString = ConfigurationManager.ConnectionStrings [ Excel03ConString]的ConnectionString。

break ;

case 。xlsx // Excel 07或更高版本

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

break ;
}

conString = string .Format(conString,excelPath);

使用(OleDbConnection excel_con = new OleDbConnection(conString))
{

excel_con.Open();

string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null )。行[ 0 ] [ TABLE_NAME ]的ToString();

DataTable dtExcelData = new DataTable();

string st = 选择[sno],[Id],[country],[Qtr] = case(子串([Qtr],1,2)为nvarchar(4)),[value]来自[ + sheet1 + ] outer apply(values((N'Q1',Q1),(N'Q2',Q2))P(Qtr ,值);
使用(OleDbDataAdapter oda = new OleDbDataAdapter(st ,excel_con))

{
oda.Fill(dtExcelData);
}

excel_con.Close();

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

使用(SqlConnection con = new SqlConnection(consString))
{

使用(SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{

// 设置数据库表名称

sqlBulkCopy.DestinationTableName = [dbo] [exceldemo];

con.Open();

sqlBulkCopy.WriteToServer(dtExcelData);

con.Close();

lblUpload.Text = 上传成功;

}








运行时
代码我得到以下错误

IErrorInfo.GetDescription失败了E_FAIL(0x80004005)。



在oda.Fill(dtExcelData);



以下是输入和输出格式。

http://i61.tinypic.com/217pk0.jpg [ ^ ]



please帮助我解决这种情况。







提前致谢。

解决方案

您可以使用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 :)


这篇关于如何将excel导入到sqlserver,并将列转换为row。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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