I have an excel sheet similar to :
I want to read data columns header:All,col1,col2,col3,col4,col5
and get all cell data.for example cell in Row = 2 and column 2 = 0
I currently write this code :
OleDbDataAdapter dbAdapter = new OleDbDataAdapter("SELECT top 5 * FROM " + excelSheets[j], connString);
DataTable fooData = new DataTable();
dbAdapter.Fill(fooData);
foreach (DataRow row in fooData.Rows)
{
Response.Write(row[0].ToString());
//Response.Write(row[1].ToString());
}
but it return just a data table with 1 column and just row 1..5 text.
How I can do this?
Please say answer without using Linq to Excel Provider and Open Xml.
Edit 1:
string file_name = "C:\\Book1.xlsx";
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + file_name + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\"";
objConn = new OleDbConnection(connString);
objConn.Open();
dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
Response.Write("Not Exist");
}
String[] excelSheets = new String[dt.Rows.Count];
int i = 0;
foreach (DataRow row in dt.Rows)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
i++;
}
// Loop through all of the sheets if you want too...
for (int j = 0; j < excelSheets.Length; j++)
{
OleDbDataAdapter dbAdapter = new OleDbDataAdapter("SELECT top 100 * FROM " + excelSheets[j], connString);
DataTable fooData = new DataTable();
dbAdapter.Fill(fooData);
foreach (DataRow row in fooData.Rows)
{
Response.Write(row[0].ToString());
}
}
You get the column name for the first column for example by fooData.Columns[0].ColumnName
- see http://msdn.microsoft.com/en-us/library/system.data.datacolumn.columnname.aspx
EDIT:
Change the SELECT
to SELECT * FROM
AND use Fill (0, 5, new DataTable[] { fooData })
.