Working with Excel files - MSDN let me down :-(

Hi all,

I'm having major problems accessing Excel data in files. I've found the following snippet which seems to work:

                Range range = firstSheet.get_Range("A1", "C3");

                //Retrieve the data from the range.
                Object[,] saRet;
                saRet = (System.Object[,])range.get_Value(Type.Missing);

                //Determine the dimensions of the array.
                long iRows;
                long iCols;
                iRows = saRet.GetUpperBound(0);
                iCols = saRet.GetUpperBound(1);

                //Build a string that contains the data of the array.
                String valueString;
                valueString = "Array Data\n";

                for (long rowCounter = 1; rowCounter <= iRows; rowCounter++)
                {
                    for (long colCounter = 1; colCounter <= iCols; colCounter++)
                    {

                        //Write the next value into the string.
                        if (saRet[rowCounter, colCounter] != null) valueString += saRet[rowCounter, colCounter].ToString() + ", ";
                        else valueString += "[NULL], ";
                    }

                    //Write in a new line.
                    valueString = String.Concat(valueString, "\n");
                }


 

And also this one, which doesn't (because System.___ComObject cannot be cast to DocumentProperties).

 

                System.Windows.Forms.MessageBox.Show(swb.CustomDocumentProperties.ToString());
                Microsoft.Office.Core.DocumentProperties docProps = (Microsoft.Office.Core.DocumentProperties) swb.CustomDocumentProperties;

 

Is there a way to interpret this data properly  In either of the two snippets, if this is the actual way to do things it's a load of cr*p IMO. Why are there no classes that accept this data in C#, and why would MS recommend to unpack an unknown object into object arrays like this What's more, what is this 'Missing.Type' that every single sample seems to use To get that to work I had to use Type.Missing instead. Everything I'm reading just doesn't work, except the first example above which appears to be someone's hack.

TIA for *anything* that would help me understand this and why the documentation and my implementation differ so greatly.

Aaron.




Answer this question

Working with Excel files - MSDN let me down :-(

  • NehpetsZero1

    Have you tried to access EXCEL trough ODBC

    And then use it like if it were a table and put a SQL Query on it

    You might find it easier.


  • Ruprect8696

    Thanks, but unfortunately I need to do cell by cell operations on two open files, and I need to access the CustomDocumentProperties as well.

  • osarsif

    Thanks for the reply Miguel. I've yet to analyse the differences between the code I found before and what you have given me, however the top link's code did allow me to read document properties without a COM casting error.

    I haven't looked at the other link yet, I probably should have made it clearer that I am not automating Excel, but accessing Excel data programmatically (for the purpose of reconstructing broken automated sheets ).

    Thanks again,

    Aaron.



  • louis murray

    Try here for the custom properties: http://support.microsoft.com/kb/303296/

    You also have a nice article here: http://www.codeproject.com/csharp/CsAutomateWord.asp

    You could (should) also use the contents of this article if you are creating a server-side classlibrary for changing custom properties in office: http://support.microsoft.com/kb/224351/


  • Working with Excel files - MSDN let me down :-(