private bool sheetNameValidation()
{
string FileName = Path.GetFileName(FileUpload.PostedFile.FileName);
string Extension = Path.GetExtension(FileUpload.PostedFile.FileName);
string FolderPath = ConfigurationManager.AppSettings["FolderPath"];
string FilePath = Server.MapPath(FolderPath + FileName);
string ExcelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=" + "\"Excel 12.0;HDR=YES;\"";
string myExcelDataQuery = "Select top 1 * from [Data$]";
OleDbConnection OleDbConn = new OleDbConnection(ExcelConnectionString);
OleDbCommand OleDbCmd = new OleDbCommand(myExcelDataQuery, OleDbConn);
OleDbConn.Open();
DataTable dt = new DataTable();
dt = OleDbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return false;
}
String[] excelSheets = new String[dt.Rows.Count];
int i = 0;
foreach (DataRow row in dt.Rows)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
i++;
}
OleDbConn.Close();
if (!excelSheets.Contains("Data$"))
{
Master.showError = "Invalid excel document. Please select correct file.";
return false;
}
else
{
return true;
}
}
I am sure it will help you...to check the sheet name.
if you want to import data into db then follow the link
How to insert data using SqlBulCopy.[
^]
i was also facing the problem, but solve my ownself
if it helps you than rate me... :)