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#
  4. Excel with Early and LateBinding

Excel with Early and LateBinding

Scheduled Pinned Locked Moved C#
5 Posts 2 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.
  • M Offline
    M Offline
    MumbleB
    wrote on last edited by
    #1

    Hi Guys. I have a slight problem. My application usses EarlyBinding.where I reference Excel 12. Problem I'm having is that most of the machines targeted have Excel 10 or Excel 11 installed so when installing the App or running it I get a GAC error. This I know is that the versions are not backward compatable. My question is, how do I go from earlybinding to LateBinding with as little changes possible. Below I instanciate my Excel Workbook etc. My code is doing loads of stuff and I wouldn't want to rewrite all of it.

            Microsoft.Office.Interop.Excel.Application objExcel = 
                new Microsoft.Office.Interop.Excel.Application();
            objExcel.Visible = false;
            Microsoft.Office.Interop.Excel.Workbook objBook = 
                objExcel.Workbooks.Add(System.Reflection.Missing.Value);
            Microsoft.Office.Interop.Excel.Worksheet objSheet = 
                (Microsoft.Office.Interop.Excel.Worksheet)
                objBook.Worksheets.get\_Item(1);
            Microsoft.Office.Interop.Excel.Range objRange;
    

    Any advice would be appreciated.

    Excellence is doing ordinary things extraordinarily well.

    P 1 Reply Last reply
    0
    • M MumbleB

      Hi Guys. I have a slight problem. My application usses EarlyBinding.where I reference Excel 12. Problem I'm having is that most of the machines targeted have Excel 10 or Excel 11 installed so when installing the App or running it I get a GAC error. This I know is that the versions are not backward compatable. My question is, how do I go from earlybinding to LateBinding with as little changes possible. Below I instanciate my Excel Workbook etc. My code is doing loads of stuff and I wouldn't want to rewrite all of it.

              Microsoft.Office.Interop.Excel.Application objExcel = 
                  new Microsoft.Office.Interop.Excel.Application();
              objExcel.Visible = false;
              Microsoft.Office.Interop.Excel.Workbook objBook = 
                  objExcel.Workbooks.Add(System.Reflection.Missing.Value);
              Microsoft.Office.Interop.Excel.Worksheet objSheet = 
                  (Microsoft.Office.Interop.Excel.Worksheet)
                  objBook.Worksheets.get\_Item(1);
              Microsoft.Office.Interop.Excel.Range objRange;
      

      Any advice would be appreciated.

      Excellence is doing ordinary things extraordinarily well.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You can do this using something like this:

      // Instantiate Excel using the prog id.
      object xl = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application"));
      // Get the workbooks collection
      object books = xl.GetType().InvokeMember(
      "Workbooks", BindingFlags.GetProperty, null, xl, null);
      // Add a workbook
      object book = books.GetType().InvokeMember( "Add",
      BindingFlags.InvokeMethod, null, books, null );
      // Get a reference to the worksheets
      object sheets = book.GetType().InvokeMember( "Worksheets",
      BindingFlags.GetProperty,null, book, null );
      // and select the first sheet.
      object[] parameters;
      parameters = new object[1];
      parameters[0] = 1;
      object sheet = sheets.GetType().InvokeMember( "Item",
      BindingFlags.GetProperty, null, sheets, parameters );
      // get the "range" A1.
      parameters = new object[2];
      parameters[0] = "A1";
      parameters[1] = Missing.Value;
      object range = sheet.GetType().InvokeMember( "Range",
      BindingFlags.GetProperty, null, sheet, parameters );
      // And set the value of it to 1.
      parameters = new object[1];
      parameters[0] = "1";
      range.GetType().InvokeMember( "Value",
      BindingFlags.SetProperty, null, range, parameters );

      // Now, set Excel to visible.
      parameters = new object[1];
      parameters[0] = true;
      xl.GetType().InvokeMember( "Visible",
      BindingFlags.SetProperty, null, xl, parameters );

      I've just knocked this together in Notepad, so I apologise if it's not 100% perfect.

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      M 1 Reply Last reply
      0
      • P Pete OHanlon

        You can do this using something like this:

        // Instantiate Excel using the prog id.
        object xl = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application"));
        // Get the workbooks collection
        object books = xl.GetType().InvokeMember(
        "Workbooks", BindingFlags.GetProperty, null, xl, null);
        // Add a workbook
        object book = books.GetType().InvokeMember( "Add",
        BindingFlags.InvokeMethod, null, books, null );
        // Get a reference to the worksheets
        object sheets = book.GetType().InvokeMember( "Worksheets",
        BindingFlags.GetProperty,null, book, null );
        // and select the first sheet.
        object[] parameters;
        parameters = new object[1];
        parameters[0] = 1;
        object sheet = sheets.GetType().InvokeMember( "Item",
        BindingFlags.GetProperty, null, sheets, parameters );
        // get the "range" A1.
        parameters = new object[2];
        parameters[0] = "A1";
        parameters[1] = Missing.Value;
        object range = sheet.GetType().InvokeMember( "Range",
        BindingFlags.GetProperty, null, sheet, parameters );
        // And set the value of it to 1.
        parameters = new object[1];
        parameters[0] = "1";
        range.GetType().InvokeMember( "Value",
        BindingFlags.SetProperty, null, range, parameters );

        // Now, set Excel to visible.
        parameters = new object[1];
        parameters[0] = true;
        xl.GetType().InvokeMember( "Visible",
        BindingFlags.SetProperty, null, xl, parameters );

        I've just knocked this together in Notepad, so I apologise if it's not 100% perfect.

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        M Offline
        M Offline
        MumbleB
        wrote on last edited by
        #3

        Cool, looks good though. My concern is when I start writing the data, for example see below portion of code.

        objSheet.Cells[iR, iC] = CLIENTSURNAME.Trim();
        objSheet.Cells[iR, iC + 1] = CLIENTFORENAMES.Trim();
        objSheet.Cells[iR, iC + 2] = TITLE.Trim();
        objSheet.Cells[iR, iC + 3] = "'" + ADDRESSLINE1.Trim();
        objSheet.Cells[iR, iC + 4] = "'" + ADDRESSLINE2.Trim();
        objSheet.Cells[iR, iC + 5] = "'" + ADDRESSLINE3.Trim();
        objSheet.Cells[iR, iC + 6] = "'" + ADDRESSLINE4.Trim();
        objSheet.Cells[iR, iC + 7] = "'" + POSTALCODE.Trim();
        objSheet.Cells[iR, iC + 8] = LEVEL;

        would I need to do the same as your example above

        Pete O'Hanlon wrote:

        // get the "range" A1.parameters = new object[2];parameters[0] = "A1";parameters[1] = Missing.Value;object range = sheet.GetType().InvokeMember( "Range", BindingFlags.GetProperty, null, sheet, parameters );

        Excellence is doing ordinary things extraordinarily well.

        P 1 Reply Last reply
        0
        • M MumbleB

          Cool, looks good though. My concern is when I start writing the data, for example see below portion of code.

          objSheet.Cells[iR, iC] = CLIENTSURNAME.Trim();
          objSheet.Cells[iR, iC + 1] = CLIENTFORENAMES.Trim();
          objSheet.Cells[iR, iC + 2] = TITLE.Trim();
          objSheet.Cells[iR, iC + 3] = "'" + ADDRESSLINE1.Trim();
          objSheet.Cells[iR, iC + 4] = "'" + ADDRESSLINE2.Trim();
          objSheet.Cells[iR, iC + 5] = "'" + ADDRESSLINE3.Trim();
          objSheet.Cells[iR, iC + 6] = "'" + ADDRESSLINE4.Trim();
          objSheet.Cells[iR, iC + 7] = "'" + POSTALCODE.Trim();
          objSheet.Cells[iR, iC + 8] = LEVEL;

          would I need to do the same as your example above

          Pete O'Hanlon wrote:

          // get the "range" A1.parameters = new object[2];parameters[0] = "A1";parameters[1] = Missing.Value;object range = sheet.GetType().InvokeMember( "Range", BindingFlags.GetProperty, null, sheet, parameters );

          Excellence is doing ordinary things extraordinarily well.

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Yup. It's not pretty. I'd write some helper methods to do this if I were you.

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          M 1 Reply Last reply
          0
          • P Pete OHanlon

            Yup. It's not pretty. I'd write some helper methods to do this if I were you.

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            M Offline
            M Offline
            MumbleB
            wrote on last edited by
            #5

            Yeah, this could end up being a right mess then..... :(( .....I have read somewhere that if I use the earliest version and develop on that then I could use earlybinding. I guess though that it would depend if they use the same methods hey? :(( :(( :(( The Nearest Bridge is looking pretty good from my office window right now!!!!

            Excellence is doing ordinary things extraordinarily well.

            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