如何使用OLEDB从第一行开始获取列名

时间:2018-10-03 15:56:03

标签: c# asp.net model-view-controller oledb

我在第一行中有列名称为doenst的工作表。列名从第6行开始,我需要将数据从此表导入到sql server。我尝试使用一些代码,但没有结果我该如何解决?我的代码在下面并带有注释,代码不起作用,我使用OLEDB进行查询,并使用BULKcopy进行导入数据

image of range i need to import to sql

            filePath = path + Path.GetFileName(postedFile.FileName);
            string extension = Path.GetExtension(postedFile.FileName);
            postedFile.SaveAs(filePath);

            string conString = string.Empty;
            switch (extension)
            {
                case ".xls": //Excel 97-03.
                    conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
                    break;
                case ".xlsx": //Excel 07 and above.
                    conString = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
                    break;
            }

            DataTable dt = new DataTable();
            conString = string.Format(conString, filePath);

            using (OleDbConnection connExcel = new OleDbConnection(conString))
            {
                using (OleDbCommand cmdExcel = new OleDbCommand())
                {
                    using (OleDbDataAdapter odaExcel = new OleDbDataAdapter())
                    {
                        cmdExcel.Connection = connExcel;

                        //Get the name of First Sheet.
                        connExcel.Open();
                        DataTable dtExcelSchema;
                        dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                        string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();

                        connExcel.Close();


                        //string Range = "B6";
                        //cmdExcel.CommandText = "SELECT * From [" + sheetName + "$B6:C10]";
                       // cmdExcel.CommandText = "SELECT * From [" + sheetName + Range +"]";
                        connExcel.Open();
                        cmdExcel.CommandText = "SELECT * From ["+ sheetName +"]"; 

                        odaExcel.SelectCommand = cmdExcel;
                        odaExcel.Fill(dt);
                        connExcel.Close();
                    }
                }
            }

            conString = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
            using (SqlConnection con = new SqlConnection(conString))
            {
                using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
                {
                    //Set the database table name.
                    sqlBulkCopy.DestinationTableName = "dbo.Tipo_Adquisicion";

                    //[OPTIONAL]: Map the Excel columns with that of the database table
                    sqlBulkCopy.ColumnMappings.Add("CÓDIGO EMPRESA", "Tipo_Adquisicion");
                    sqlBulkCopy.ColumnMappings.Add("PERÍODO INFORMACIÓN", "descripcion");
                   // sqlBulkCopy.ColumnMappings.Add("Country", "Country");

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

0 个答案:

没有答案