| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|
|||||||
| C-Sharp Programming Post questions, answers, and comments about C#. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
[RESOLVED] Reading Excel via csharp as a table
I have implemented a piece of code that reads values from an Excel table
I do a general sql query and then fetch relevant fields from my collection. However it seems that sometimes some of teh fields are coming back as null and i m not sure why is there anything wrong with teh way that the values are pulled out? Cheers Code:
const string strH = "QC-MatrixSetupNo,FinishedGoodsProductCode,Description Line 1,Description Line 2,...bllaa blaa blaa..";
AppSettingsReader m_AppSettingsReader = new AppSettingsReader();
string[] columns = Regex.Split(strH, ",");
string strOle = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sExcel + ";Extended Properties=Excel 8.0;";
string strSheet = "Sheet1";
try
{
strSheet = m_AppSettingsReader.GetValue("sheet", typeof(string)).ToString();
log("key [sheet] was not found in config file- Assuming Sheet1\r\n");
}
catch
{
}
string strSql = "SELECT * FROM [" + strSheet + "$]";
OleDbConnection connection = new OleDbConnection(strOle);
OleDbDataReader reader;
try
{
connection.Open();
}
catch
{
MessageBox.Show("FAILED:Can not open " + strOle);
}
OleDbCommand query = new OleDbCommand(strSql, connection);
int iRec = 0;
OleDbDataReader tempread = query.ExecuteReader();
reader = query.ExecuteReader();
string s = null;
iRec = 0;
label4.Visible = true;
while (reader.Read())
{
bool bfound = false;
s = null;
int iColumn = 0;
foreach(string column in columns)
{
iColumn++;
string x = null;
try
{
x = reader[column].ToString();
log(iColumn.ToString()+":["+column + "]=[" + x+"]");
}
catch
{
MessageBox.Show("FAILED to read column :[" + column + "].This column was expected and not found in the XLS");
return;
}
if (x != "")
bfound = true;
s += x + ",";
}
if (bfound == true)
{
// dso something good with it
}
}
reader.Close();
|
|
#2
|
||||
|
||||
|
Re: Reading Excel via csharp as a table
resolved:
A rule in spreadsheet when it was created . In other word what you see may not be what you can get from reading the table. |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|