Это должно сделать работу
/// <summary>
/// This method retrieves the excel sheet names from
/// an excel workbook & reads the excel file
/// </summary>
/// <param name="excelFile">The excel file.</param>
/// <returns></returns>
#region GetsAllTheSheetNames of An Excel File
public static string[] ExcelSheetNames(String excelFile)
{
DataTable dt;
string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFile + ";Extended Properties='Excel 12.0;HDR=Yes'";
using (OleDbConnection objConn = new OleDbConnection(connString))
{
objConn.Open();
dt =
objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return null;
}
string[] res = new string[dt.Rows.Count];
for (int i = 0; i < res.Length; i++)
{
string name = dt.Rows[i]["TABLE_NAME"].ToString();
if (name[0] == '\'')
{
//numeric sheetnames get single quotes around
//remove them here
if (Regex.IsMatch(name, @"^'\d\w+\$'$"))
{
name = name.Substring(1, name.Length - 2);
}
}
res[i] = name;
}
return res;
}
}
#endregion
///
/// This method retrieves the excel sheet specified and
/// gets the data from the required columns
/// and inserts the required columns into database
/// </summary>
/// <param name="excelFile">The excel file.</param>
/// <returns></returns>
#region GetstheRequiredcolumns from the Specified sheet
public static DataTable columnNamessheet1(String excelFile)
{
string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFile + ";Extended Properties='Excel 12.0;HDR=yes'";
string connectionstring ="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DebitCare;Data Source=SSDEV7-HP\\SQLEXPRESS";
using (OleDbConnection conn = new OleDbConnection(connString))
{
SqlConnection SqlConn1 = new SqlConnection(connectionstring);
SqlConn1.Open();
OleDbCommand odc = new OleDbCommand(string.Format("Select LoanNumber,CustomerName,DateofBirth,MobileNo,SanctionDate,EmployerName FROM [BAJAJ DUMP$]"), conn);
conn.Open();
OleDbDataReader reader = odc.ExecuteReader();
DataTable sheetSchema = reader.GetSchemaTable();
SqlBulkCopy sqlbulk = new SqlBulkCopy(SqlConn1);
sqlbulk.DestinationTableName = "CUSTOMER_DETAILS";
sqlbulk.ColumnMappings.Add("LoanNumber", "CUSTOMER_LOAN_NO");
sqlbulk.ColumnMappings.Add("CustomerName", "CUSTOMER_ NAME");
sqlbulk.ColumnMappings.Add("DateofBirth", "CUSTOMER_DOB");
sqlbulk.ColumnMappings.Add("MobileNo", "CUSTOMER_MOBILE NUMBER");
sqlbulk.ColumnMappings.Add("SanctionDate", "CUSTOMER_SANCTION DATE");
sqlbulk.ColumnMappings.Add("EmployerName", "CUSTOMER_COMPANY NAME");
sqlbulk.WriteToServer(reader);
conn.Close();
return sheetSchema;
}
}