Through the Interface: Updating an AutoCAD table linked to an Excel spreadsheet using .NET

April 2015

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    


« Creating an AutoCAD table linked to an Excel spreadsheet using .NET | Main | Updating an Excel spreadsheet from a linked AutoCAD table using .NET »

August 24, 2007

Updating an AutoCAD table linked to an Excel spreadsheet using .NET

Thanks to Viru Aithal, from DevTech India, for providing the code for this post (I converted the C# code below from some C++ he had sent to a developer).

In the last post we showed how to create a table linked to an Excel spreadsheet using .NET in AutoCAD 2008. AutoCAD does a great job of looking for changes in the Excel spreadsheet, and asking whether you want to update the linked table:


There may be times, however, when you want to force the update programmatically, whether from the spreadsheet to the table ot vice-versa. In this post we'll show the code to update the table from the spreadsheet, and in the next post we'll see some code to update the spreadsheet from the table (should it have been unlocked and edited).

Here's the C# code:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

namespace LinkToExcel


  public class Commands



    static public void UpdateTableFromSpreadsheet()


      Document doc =


      Database db = doc.Database;

      Editor ed = doc.Editor;

      PromptEntityOptions opt =

        new PromptEntityOptions(

          "\nSelect table to update: "



        "\nEntity is not a table."


      opt.AddAllowedClass(typeof(Table), false);

      PromptEntityResult per =


      if (per.Status != PromptStatus.OK)


      Transaction tr =


      using (tr)




          DBObject obj =





          Table tb = (Table)obj;

          // It should always be a table

          // but we'll check, just in case

          if (tb != null)


            // The table must be open for write


            // Update the data link from the spreadsheet

            ObjectId dlId = tb.GetDataLink(0, 0);

            DataLink dl =









            // And the table from the data link








            "\nUpdated the table from the spreadsheet."



        catch (Exception ex)



            "\nException: {0}",








When you run the S2T (for Spreadsheet-to-Table) command, you will be prompted to select a table. The code retrieves the link information from the table and then requests the data link to pull down new data from the spreadsheet before updating the table. Next time we'll look at the code for T2S...


TrackBack URL for this entry:

Listed below are links to weblogs that reference Updating an AutoCAD table linked to an Excel spreadsheet using .NET:

blog comments powered by Disqus


10 Random Posts