C++, C#, and Excel
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: C++, C#, and Excel

  1. #1
    Join Date
    Mar 2011
    Posts
    53

    C++, C#, and Excel

    Hi,

    I just got a job programming a windows application. One of the requirements is reading and writing values from an excel spreadsheet. Looking online, it appears that C++ isn't the best (one of the worst?) languages for reading and writing values from excel files. Now the language I'm most familiar with is C++ and I've been looking at C#, and it appears to be much easier to read excel files. So I'm just debating what I should do, and I'm looking for advice. Do you think it might be easier to learn C#, since I'm quite familiar with C++, and write the application in C#? Or is using C++ with excel easier than it looks? Thank you very much for the help. This site has always been awesome.

    Greg

  2. #2
    Join Date
    Jan 2009
    Posts
    50

    Re: C++, C#, and Excel

    Quote Originally Posted by Gregorina View Post
    Hi,

    I just got a job programming a windows application. One of the requirements is reading and writing values from an excel spreadsheet. Looking online, it appears that C++ isn't the best (one of the worst?) languages for reading and writing values from excel files. Now the language I'm most familiar with is C++ and I've been looking at C#, and it appears to be much easier to read excel files. So I'm just debating what I should do, and I'm looking for advice. Do you think it might be easier to learn C#, since I'm quite familiar with C++, and write the application in C#? Or is using C++ with excel easier than it looks? Thank you very much for the help. This site has always been awesome.

    Greg
    Hi. I don't know C#, but I know that you can work with excel very well from C++ ... you can find on the article section some good examples to how to work with excel from C++ ...
    Quote Originally Posted by Gregorina View Post
    Hi,

    I just got a job programming a windows application. One of the requirements is reading and writing values from an excel spreadsheet. Looking online, it appears that C++ isn't the best (one of the worst?) languages for reading and writing values from excel files. Now the language I'm most familiar with is C++ and I've been looking at C#, and it appears to be much easier to read excel files. So I'm just debating what I should do, and I'm looking for advice. Do you think it might be easier to learn C#, since I'm quite familiar with C++, and write the application in C#? Or is using C++ with excel easier than it looks? Thank you very much for the help. This site has always been awesome.

    Greg
    Hi. I don't know C#, but I know that you can work with excel very well from C++ ... you can find on the article section some good examples to how to work with excel from C++ ... <url="http://www.codeguru.com/cpp/data/mfc_database/microsoftexcel/article.php/c11745/Microsoft-Excel-Automation-Class.htm">this</url> is the great one ... is the great one ...
    Last edited by mesajflaviu; August 6th, 2013 at 01:41 AM.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,132

    Re: C++, C#, and Excel

    Depending on how fancy you need to get, you can use ODBC and the CRecordset class to read and write to/from Excel. That's probably a bit easier than automation.

  4. #4
    Join Date
    Mar 2011
    Posts
    53

    Re: C++, C#, and Excel

    Quote Originally Posted by GCDEF View Post
    Depending on how fancy you need to get, you can use ODBC and the CRecordset class to read and write to/from Excel. That's probably a bit easier than automation.
    Ok, awesome, and thank you. Searching for ODBC and CRecordset, has given me some simpler examples than what I've seen before. Do you know of any tutorials for using ODBC and CRecordset? And of any code examples that can read both .xls and .xlsx?

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,132

    Re: C++, C#, and Excel

    Quote Originally Posted by Gregorina View Post
    Ok, awesome, and thank you. Searching for ODBC and CRecordset, has given me some simpler examples than what I've seen before. Do you know of any tutorials for using ODBC and CRecordset? And of any code examples that can read both .xls and .xlsx?
    Not off the top of my head. It's pretty easy though. Basic steps are use the ODBC administrator to create a DSN that points to an Excel spreadsheet. Use the IDE to create a CRecordset derived class that accesses that DSN. Uset the Open and MoveNext members to access the data.

  6. #6
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,527

    Re: C++, C#, and Excel

    Quote Originally Posted by Gregorina View Post
    ...One of the requirements is reading and writing values from an excel spreadsheet.
    What do you mean by that? Reading the values? Or the formulas?
    The big question is: is Excel present on the target system? It is way easier to automate Excel than to make sense of its file format manually.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  7. #7
    Join Date
    Dec 2012
    Posts
    4

    Re: C++, C#, and Excel

    Here you may find an example how excel sheets can be read and written by C#
    http://finaquant.com/table-function-...oft-excel/3788

    This example shows, how c# methods can be called by excel macros (VBA), how excel sheets can be read and written by C#/.NET and how simple parameters (like string, integer and double) can be passed from excel VBA to C#/.NET.

    In this example, the .NET-excel communication is accomplised through COM.

    I hope it helps.

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,997

    Re: C++, C#, and Excel

    If you know C++ and don't know C#
    then my advice would be "stick with what you know".

    THe thing about C++ is that it doesn't come with built in support for COM as does C#, but it only takes something like the ATL or MFC COM wrappers to make dealing with COM a lot less of a hassle than it seems to be.
    Alternatively write your own c++ wrapper around the Excel COM classes

    THe thing to keep in mind is that languages like C# and VB don't really do all that much. The main "magic" in what they do is hiding all the HRESULT return values. VB handles this in OnError, C# has exceptions, oh look, C++ has exceptions too.

    It's a recommended way to go for C++ also, "hide" the HRESULTS in exceptions. and then your COM interface code starts looking a lot more like regular C++.

  9. #9
    Join Date
    Jul 2002
    Posts
    2,519

    Re: C++, C#, and Excel

    This question was asked four months ago

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center