RSS

To Retrieve the Sheet Names From the Excel Book

To Use the bellow Method we can get the Sheets Names in the List Box(i.e, lstSheetName) availble in the Form.

public string GetExcelSheetName(String xlsName)
{
OleDbConnection ObjCon = null;
DataTable ObjTable = null;
String ConStr = String.Empty;

try
{
ConStr = "Provider=Microsoft.Jet.Oledb.4.0;Data source=" + XlsFileName;
ConStr += ";Extended Properties=Excel 8.0;";
ObjCon = new OleDbConnection(ConStr);
ObjCon.Open();
ObjTable = ObjCon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (ObjTable != null)
{
String[] Sheets = new String[ObjTable.Rows.Count];
int i = 0;
foreach (DataRow dr in ObjTable.Rows)
{
Sheets[i] = dr["TABLE_NAME"].ToString();
i++;
}
// This List box Used to List Sheet Names in Given Excel File
lstSheetName.Items.AddRange(Sheets);
bflg = true;
return "Sheet Name Retrieved Successfuly.";
}
else
{
bflg =false;
return "Excel Database is Empty!.";
}
}
catch (Exception ex)
{
bflg = false;
return ex.Message;
}
}

0 comments:

Post a Comment