简体   繁体   English

使用ASP.NET通过跳过n行将数据从Excel导入到SQL中

[英]Importing data into SQL from excel using ASP.NET by skipping n rows

I am using following code to export data from excel into SQL using ASP.NET. 我正在使用以下代码使用ASP.NET将数据从excel导出到SQL。

protected void Upload(object sender, EventArgs e)
{
    //Upload and save the file
    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();

        //[OPTIONAL]: It is recommended as otherwise the data will be considered as String by default.
        dtExcelData.Columns.AddRange(new DataColumn[3] 
        { 
            new DataColumn("ID", typeof(string)),
            new DataColumn("Name", typeof(DateTime)),
            new DataColumn("Designation",typeof(DateTime))});

        using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + sheet1 + "]", excel_con))
        {
            oda.Fill(dtExcelData);
        }
        excel_con.Close();

        string consString = ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString;
        using (SqlConnection con = new SqlConnection(consString))
        {
            using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
            {

                //Set the database table name
                sqlBulkCopy.DestinationTableName = "dbo.Employee";

                //[OPTIONAL]: Map the Excel columns with that of the database table
                sqlBulkCopy.ColumnMappings.Add("ID", "ID");
                sqlBulkCopy.ColumnMappings.Add("Name", "Name");
                sqlBulkCopy.ColumnMappings.Add("Designation", "Designation");

                con.Open();
                sqlBulkCopy.WriteToServer(dtExcelData);
                con.Close();
            }
        }
    }
}

The problem is that in my Excel file the actual data is starting from Row 7. So it is not working but whenever i move data to row 1 it fetches and uploades the data in SQL just fine. 问题在于,在我的Excel文件中,实际数据是从第7行开始的。因此,它不起作用,但是每当我将数据移至第1行时,它就可以用SQL提取并上传数据。 So what i want is to predefine in my code that pick the data from row 7 in excel file. 所以我想要在我的代码中预定义从Excel文件的第7行中选择数据的代码。 Also another query that my header name of SQL and Excel sheets are different, so unless both headers are same it does not pick the data, so is there anyway that i can keep the header different and still fetch the data? 还有另一个查询,我的SQL和Excel工作表的标题名称不同,因此,除非两个标题都相同,否则它不会选择数据,因此无论如何我可以保持标题不同并仍获取数据? Please help, as i am really stuck here. 请帮忙,因为我真的被困在这里。

You need to let excel know where to get the data from in your query try something like the following. 您需要让excel知道从哪里获取查询中的数据,请尝试以下类似的操作。

Change this row: 更改此行:

OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + sheet1 + "]", excel_con))

to this: 对此:

OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + sheet1 + "$" + "A7:ZZ]", excel_con))

Alter the cell & row parameters to meet your requirements... 更改单元格和行参数以满足您的要求...

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM