Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i have a excel sheet from where i have to fetch data from a selected data range , but how i fetch the data from the merged cells coming into the selected range.
Posted
Comments
ZurdoDev 6-Mar-14 8:10am    
You can record a macro in Excel to see exactly what the code would look like.

1 solution

try this
use this namespace using System.Data.OleDb;


FileNm = System.IO.Path.GetFileName(yourfilename);
string conStr = "";
if (FileNm.Contains(".xls") == true)
{
conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
}
if (FileNm.Contains(".xlsx") == true)
{
conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
}
string isHDR = "No";
conStr = String.Format(conStr, yourfilepath, isHDR);//isHrd is "YES" or "No"
OleDbConnection connExcel = new OleDbConnection(conStr);
OleDbCommand cmdExcel = new OleDbCommand();
OleDbDataAdapter oda = new OleDbDataAdapter();
DataTable dt = new DataTable();
cmdExcel.Connection = connExcel;
connExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
connExcel.Close();
connExcel.Open();
cmdExcel.CommandText = "SELECT * From [Sheet1$]";
oda.SelectCommand = cmdExcel;
oda.Fill(dt);
oda.Dispose();
connExcel.Close();

//get all excel value from dt
 
Share this answer
 
v2
Comments
Member 11313878 8-Jun-15 3:40am    
Its not working for me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900