Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过跳过n行,使用ASP.NET将数据从excel导入SQL_C#_Sql_Excel - Fatal编程技术网

C# 通过跳过n行,使用ASP.NET将数据从excel导入SQL

C# 通过跳过n行,使用ASP.NET将数据从excel导入SQL,c#,sql,excel,C#,Sql,Excel,我使用以下代码使用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)

我使用以下代码使用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();
            }
        }
    }
}
问题是,在我的Excel文件中,实际数据是从第7行开始的。 所以它不起作用,但每当我将数据移动到第1行时,它就会在SQL中获取并上传数据。 所以我想要的是在我的代码中预定义从excel文件的第7行选择数据。 另一个查询是,我的SQL和Excel工作表的标题名称不同,因此,除非两个标题相同,否则它不会拾取数据,因此是否有其他方法可以保持标题不同并仍然获取数据?
请帮助我,因为我真的被困在这里。

您需要让excel知道在查询中从何处获取数据,请尝试以下方法

更改此行:

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

OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + sheet1 + "$" + "A7:ZZ]", excel_con))
更改单元格和行参数以满足您的要求

希望这有帮助