Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Excel Automation - Please help ...

Excel Automation - Please help ...

Scheduled Pinned Locked Moved C / C++ / MFC
questiontestingtoolshelp
17 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G GuyM

    Hi all, In my application I need to read/write values to/from certain cells in an excel sheet. I've imported the Microsoft Excel 11.0 Object Library and added all the required interfaces (I think). My question is how do I open an existing XLS file and how do I get or set values from a certain cell? Thanks in advance, Guy.

    N Offline
    N Offline
    Nikhil Trivedi
    wrote on last edited by
    #2

    Dear friend, I think Shellapi.h might be of your help. Once I used it to open an text and excel file. Dont know specific functions right now. ShellExecute() will help. I think. Nikhs Nikhil Trivedi

    1 Reply Last reply
    0
    • G GuyM

      Hi all, In my application I need to read/write values to/from certain cells in an excel sheet. I've imported the Microsoft Excel 11.0 Object Library and added all the required interfaces (I think). My question is how do I open an existing XLS file and how do I get or set values from a certain cell? Thanks in advance, Guy.

      N Offline
      N Offline
      Nikhil Trivedi
      wrote on last edited by
      #3

      GuyM wrote:

      how do I open an existing XLS file

      I think the ShellApi.h would be of your help. You can get its detail from msdn.com. I think the ShellExecute() function will do for you. Hope this would help you.

      G 1 Reply Last reply
      0
      • N Nikhil Trivedi

        GuyM wrote:

        how do I open an existing XLS file

        I think the ShellApi.h would be of your help. You can get its detail from msdn.com. I think the ShellExecute() function will do for you. Hope this would help you.

        G Offline
        G Offline
        GuyM
        wrote on last edited by
        #4

        Thanks for your answer, but I want to programmatically read/write values to/from excel (not just run the excel application). This is done with COM ... I've found an example in MSDN but: 1. It doesn't show how to open an existing file. 2. It only shows how to read/write an entire range of cells, not a single cell Thanks.

        N R 2 Replies Last reply
        0
        • G GuyM

          Thanks for your answer, but I want to programmatically read/write values to/from excel (not just run the excel application). This is done with COM ... I've found an example in MSDN but: 1. It doesn't show how to open an existing file. 2. It only shows how to read/write an entire range of cells, not a single cell Thanks.

          N Offline
          N Offline
          Nikhil Trivedi
          wrote on last edited by
          #5

          Sorry that I got your question wrong. I havent worked on office tools, but one of my friend who worked on this told me that he was using excel.h then. I dont know if it might help you or not. Nikhs Nikhil Trivedi

          1 Reply Last reply
          0
          • G GuyM

            Thanks for your answer, but I want to programmatically read/write values to/from excel (not just run the excel application). This is done with COM ... I've found an example in MSDN but: 1. It doesn't show how to open an existing file. 2. It only shows how to read/write an entire range of cells, not a single cell Thanks.

            R Offline
            R Offline
            Roger Stoltz
            wrote on last edited by
            #6

            An existing excel document is opened with Open(...) in the Workbooks interface. Do you have a link to the MSDN example? Perhaps the explanation becomes more clear if it's based on the example you've based your code on.


            "It's supposed to be hard, otherwise anybody could do it!" - selfquote
            "High speed never compensates for wrong direction!" - unknown

            G 1 Reply Last reply
            0
            • R Roger Stoltz

              An existing excel document is opened with Open(...) in the Workbooks interface. Do you have a link to the MSDN example? Perhaps the explanation becomes more clear if it's based on the example you've based your code on.


              "It's supposed to be hard, otherwise anybody could do it!" - selfquote
              "High speed never compensates for wrong direction!" - unknown

              G Offline
              G Offline
              GuyM
              wrote on last edited by
              #7

              This is the link to the example: http://support.microsoft.com/kb/308407[^] As you can see there, the example doesn't open an existing excel file but creates a new (unnamed) one. Second, is uses COleSafeArray in order to access (read/write) an entire range of cells, where I need to access only single cells ... Thanks for your help ... :cool:

              R 1 Reply Last reply
              0
              • G GuyM

                Hi all, In my application I need to read/write values to/from certain cells in an excel sheet. I've imported the Microsoft Excel 11.0 Object Library and added all the required interfaces (I think). My question is how do I open an existing XLS file and how do I get or set values from a certain cell? Thanks in advance, Guy.

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #8

                GuyM wrote:

                My question is how do I open an existing XLS file...

                if (app.CreateDispatch("Excel.Application") == TRUE)
                {
                app.SetVisible(TRUE);

                Workbooks books = app.GetWorkbooks();
                
                \_Workbook book = books.Open("C:\\\\Book1.xls",
                                             vtOptional, vtOptional, vtOptional,
                                             vtOptional, vtOptional, vtOptional,
                                             vtOptional, vtOptional, vtOptional,
                                             vtOptional, vtOptional, vtOptional);
                ...
                

                }


                "A good athlete is the result of a good and worthy opponent." - David Crow

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                G 1 Reply Last reply
                0
                • D David Crow

                  GuyM wrote:

                  My question is how do I open an existing XLS file...

                  if (app.CreateDispatch("Excel.Application") == TRUE)
                  {
                  app.SetVisible(TRUE);

                  Workbooks books = app.GetWorkbooks();
                  
                  \_Workbook book = books.Open("C:\\\\Book1.xls",
                                               vtOptional, vtOptional, vtOptional,
                                               vtOptional, vtOptional, vtOptional,
                                               vtOptional, vtOptional, vtOptional,
                                               vtOptional, vtOptional, vtOptional);
                  ...
                  

                  }


                  "A good athlete is the result of a good and worthy opponent." - David Crow

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  G Offline
                  G Offline
                  GuyM
                  wrote on last edited by
                  #9

                  Great - it worked !! Thanks !! Now just one more thing ...how do I get/set the value a certain cell (for example A1).

                  D 1 Reply Last reply
                  0
                  • G GuyM

                    This is the link to the example: http://support.microsoft.com/kb/308407[^] As you can see there, the example doesn't open an existing excel file but creates a new (unnamed) one. Second, is uses COleSafeArray in order to access (read/write) an entire range of cells, where I need to access only single cells ... Thanks for your help ... :cool:

                    R Offline
                    R Offline
                    Roger Stoltz
                    wrote on last edited by
                    #10

                    Before you continue you have to be aware that Excel deals with containers. This means that when you launch Excel with CreateDispatch or similar, the next step is usually to get an interface for the WorkBook collection/container called WorkBooks. The same goes for the WorkSheet objects in the WorkSheets collection/container and so on. Once you've got hold of the WorkBooks interface, you call WorkBooks::Open(...) in order to open an existing document, a WorkBook. Then you'll get a WorkBook interface to be able to access the document. When the WorkBook has been opened, you have to choose which WorkSheet you want to use with WorkBook::get_WorkSheets() and later WorkSheets::get_Item(...). For each cell you want to read or write, you have to select a "Range". The example shows this. Regarding the fact that you don't want to use a SafeArray to write the data, I think you can give a VARIANT as argument to Range::put_Value() with a single value and the type set to e.g. VT_I4 for a 32-bit integer.


                    "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                    "High speed never compensates for wrong direction!" - unknown

                    1 Reply Last reply
                    0
                    • G GuyM

                      Great - it worked !! Thanks !! Now just one more thing ...how do I get/set the value a certain cell (for example A1).

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #11

                      GuyM wrote:

                      ...how do I set the value a certain cell (for example A1).

                      Worksheets sheets = book.GetSheets();

                      _Worksheet sheet = sheets.GetItem(COleVariant((short) 1));

                      Range range = sheet.GetRange(COleVariant("A1"), COleVariant("A1"));

                      range.SetValue(COleVariant("12345.6"));


                      "A good athlete is the result of a good and worthy opponent." - David Crow

                      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                      H 1 Reply Last reply
                      0
                      • D David Crow

                        GuyM wrote:

                        ...how do I set the value a certain cell (for example A1).

                        Worksheets sheets = book.GetSheets();

                        _Worksheet sheet = sheets.GetItem(COleVariant((short) 1));

                        Range range = sheet.GetRange(COleVariant("A1"), COleVariant("A1"));

                        range.SetValue(COleVariant("12345.6"));


                        "A good athlete is the result of a good and worthy opponent." - David Crow

                        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                        H Offline
                        H Offline
                        hellogany
                        wrote on last edited by
                        #12

                        HI i want to set a particular cell to RED color ?? and also while quittin the application, the excel file need to be saved....

                        D 1 Reply Last reply
                        0
                        • H hellogany

                          HI i want to set a particular cell to RED color ?? and also while quittin the application, the excel file need to be saved....

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #13

                          hellogany wrote:

                          HI i want to set a particular cell to RED color ??

                          Range range = sheet.GetRange(COleVariant("A1"), COleVariant("A1"));
                          Interior interior = range.GetInterior();
                          interior.SetColorIndex(COleVariant(3L));

                          hellogany wrote:

                          and also while quittin the application, the excel file need to be saved....

                          So you might want to call Save() or SaveAs().

                          "Love people and use things, not love things and use people." - Unknown

                          "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                          H T 2 Replies Last reply
                          0
                          • D David Crow

                            hellogany wrote:

                            HI i want to set a particular cell to RED color ??

                            Range range = sheet.GetRange(COleVariant("A1"), COleVariant("A1"));
                            Interior interior = range.GetInterior();
                            interior.SetColorIndex(COleVariant(3L));

                            hellogany wrote:

                            and also while quittin the application, the excel file need to be saved....

                            So you might want to call Save() or SaveAs().

                            "Love people and use things, not love things and use people." - Unknown

                            "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                            H Offline
                            H Offline
                            hellogany
                            wrote on last edited by
                            #14

                            thanks for the reply. i have to find a value in the excel cell and should replace a new value in that cell Can u help me on this issue? gany

                            D 1 Reply Last reply
                            0
                            • H hellogany

                              thanks for the reply. i have to find a value in the excel cell and should replace a new value in that cell Can u help me on this issue? gany

                              D Offline
                              D Offline
                              David Crow
                              wrote on last edited by
                              #15

                              hellogany wrote:

                              i have to find a value in the excel cell...

                              Look at the Range::Find() method.

                              "Love people and use things, not love things and use people." - Unknown

                              "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                              1 Reply Last reply
                              0
                              • D David Crow

                                hellogany wrote:

                                HI i want to set a particular cell to RED color ??

                                Range range = sheet.GetRange(COleVariant("A1"), COleVariant("A1"));
                                Interior interior = range.GetInterior();
                                interior.SetColorIndex(COleVariant(3L));

                                hellogany wrote:

                                and also while quittin the application, the excel file need to be saved....

                                So you might want to call Save() or SaveAs().

                                "Love people and use things, not love things and use people." - Unknown

                                "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                                T Offline
                                T Offline
                                thready
                                wrote on last edited by
                                #16

                                DavidCrow wrote:

                                Range range = sheet.GetRange(COleVariant("A1"), COleVariant("A1"));Interior interior = range.GetInterior();interior.SetColorIndex(COleVariant(3L));

                                I've been looking all over for how to change the color - thanks to you it works - but just a simple question- Why is 3L = red? How does this color index thing work? Is there a way to use RGB with the 3 params to get any color? Thank you again! Mike

                                D 1 Reply Last reply
                                0
                                • T thready

                                  DavidCrow wrote:

                                  Range range = sheet.GetRange(COleVariant("A1"), COleVariant("A1"));Interior interior = range.GetInterior();interior.SetColorIndex(COleVariant(3L));

                                  I've been looking all over for how to change the color - thanks to you it works - but just a simple question- Why is 3L = red? How does this color index thing work? Is there a way to use RGB with the 3 params to get any color? Thank you again! Mike

                                  D Offline
                                  D Offline
                                  David Crow
                                  wrote on last edited by
                                  #17

                                  thready wrote:

                                  Why is 3L = red?

                                  Because that's what the designer of that type library decided.

                                  thready wrote:

                                  How does this color index thing work?

                                  I'm not sure what you are asking here. Just use the OLE/COM Object Viewer to find out the other values.

                                  "Love people and use things, not love things and use people." - Unknown

                                  "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups