CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Faltering Windows support
  • Internet Explorer 8 Click Clever Click Safe
  • Release Candidate 2 for ASP.NET MVC 2
  • Learn How to Create Dual Mode Windows Services

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > .NET Programming > C-Sharp Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C-Sharp Programming Post questions, answers, and comments about C#.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old August 14th, 2009, 03:12 AM
    Saeed's Avatar
    Saeed Saeed is offline
    Member +
     
    Join Date: May 1999
    Location: G day Mayt Land
    Posts: 937
    Saeed is on a distinguished road (10+)
    [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();
    Reply With Quote
      #2    
    Old August 17th, 2009, 10:18 PM
    Saeed's Avatar
    Saeed Saeed is offline
    Member +
     
    Join Date: May 1999
    Location: G day Mayt Land
    Posts: 937
    Saeed is on a distinguished road (10+)
    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.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > .NET Programming > C-Sharp Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 08:15 AM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.