Narayan Mali

Narayan Mali

  • NA
  • 56
  • 21.2k

How To find non empty column of excel using c# ?

Jun 26 2014 5:32 AM

please help
i want to fetch only those column of excel having data.
when i am fetching all column i get error while excel adapter fill dataset ExcelAdapter.Fill(ExcelDataSet);"
Error is "to many value"
so i want to fetch those column having data in excel.
thanks in advance
my code is 
OleDbConnection con = null;
string FilePath = Server.MapPath(Strfilename);
string FileExt = System.IO.Path.GetExtension(FilePath);
if (FileExt == ".xls")
{
con = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + FilePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"");
}
con.Open();
DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);



Session["Dtexl"] = dt;
if (dt.Rows.Count > 0)
{
string getExcelSheetName = dt.Rows[0]["Table_Name"].ToString();

OleDbCommand ExcelCommand = new OleDbCommand(@"SELECT * FROM [" + getExcelSheetName + @"]", con);
OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
DataSet ExcelDataSet = new DataSet();
ExcelAdapter.Fill(ExcelDataSet);

Answers (3)