找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1102|回复: 3

[每日一码] Creating an AutoCAD table linked to an Excel spreadsheet using .NET

[复制链接]

已领礼包: 6个

财富等级: 恭喜发财

发表于 2016-12-23 13:29:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

In the last post I promised to tackle this issue, and so here we are again. :-)
Note: the code in this post relies on enhanced table functionality introduced in AutoCAD 2008, so please don't get frustrated trying to make this work in previous versions.
The following C# code follows on from yesterday's, taking the spreadsheet selected by the user and linking it to a newly-created table in the active AutoCAD drawing. I haven't bothered with line numbering in the below code, as it follows on almost exactly from the code shown last time (aside from renaming the namespace, the command and the function, as well as adding a string constant at the top of the function implementation).


Here's what happens when you run the TFS command and select your favourite XLS for linking (I used mass-balance.xls from AutoCAD 2008's Sample\Mechanical Sample folder):


At this stage I haven't focused at all on formating - this is just coming in "as is", without any adjustment of cell alignments, column widths or row heights.
I chose to hardcode the name of the Data Link we use for the spreadsheet. You can run the DATALINK command to check on it, after the command has executed:


It doesn't seem to be an issue if you repeat the command and bring in a different spreadsheet using the same link - the link appears to continue (although I haven't performed exhaustive testing). If it does prove to be a problem it should be simple enough to create a unique Data Link per spreadsheet imported (or even per time the command is run).


Posted on August 22, 2007 at 05:40 PM in AutoCAD, AutoCAD .NET, Excel, Tables | Permalink



using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;

namespace LinkToExcel
{
  public class Commands
  {
    [CommandMethod("TFS")]
    static public void TableFromSpreadsheet()
    {
      // Hardcoding the string
      // Could also select for it
      const string dlName =
        "Import table from Excel demo";

      Document doc =
        Application.DocumentManager.MdiActiveDocument;
      Database db = doc.Database;
      Editor ed = doc.Editor;

      OpenFileDialog ofd =
        new OpenFileDialog(
          "Select Excel spreadsheet to link",
          null,
          "xls; xlsx",
          "ExcelFileToLink",
          OpenFileDialog.OpenFileDialogFlags.
            DoNotTransferRemoteFiles
        );

      System.Windows.Forms.DialogResult dr =
        ofd.ShowDialog();

      if (dr != System.Windows.Forms.DialogResult.OK)
        return;

      ed.WriteMessage(
        "\nFile selected was \"{0}\".",
        ofd.Filename
      );

      PromptPointResult ppr =
        ed.GetPoint(
          "\nEnter table insertion point: "
        );
      if (ppr.Status != PromptStatus.OK)
        return;

      // Remove the Data Link, if it exists already

      DataLinkManager dlm = db.DataLinkManager;
      ObjectId dlId = dlm.GetDataLink(dlName);
      if (dlId != ObjectId.Null)
      {
        dlm.RemoveDataLink(dlId);
      }

      // Create and add the Data Link

      DataLink dl = new DataLink();
      dl.DataAdapterId = "AcExcel";
      dl.Name = dlName;
      dl.Description =
        "Excel fun with Through the Interface";
      dl.ConnectionString = ofd.Filename;
      dl.DataLinkOption =
        DataLinkOption.PersistCache;
      dl.UpdateOption |=
        (int)UpdateOption.AllowSourceUpdate;

      dlId = dlm.AddDataLink(dl);

      Transaction tr =
        doc.TransactionManager.StartTransaction();
      using (tr)
      {
        tr.AddNewlyCreatedDBObject(dl, true);

        BlockTable bt =
          (BlockTable)tr.GetObject(
            db.BlockTableId,
            OpenMode.ForRead
          );

        Table tb = new Table();
        tb.TableStyle = db.Tablestyle;
        tb.Position = ppr.Value;
        tb.SetDataLink(0, 0, dlId, true);
        tb.GenerateLayout();

        BlockTableRecord btr =
          (BlockTableRecord)tr.GetObject(
            db.CurrentSpaceId,
            OpenMode.ForWrite
          );

        btr.AppendEntity(tb);
        tr.AddNewlyCreatedDBObject(tb, true);
        tr.Commit();
      }

      // Force a regen to display the table
      ed.Regen();
    }
  }
}
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 2471个

财富等级: 金玉满堂

发表于 2016-12-24 15:24:46 | 显示全部楼层
学习!!!!!!!!!!!!!
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复

使用道具 举报

已领礼包: 3913个

财富等级: 富可敌国

发表于 2016-12-25 16:51:41 | 显示全部楼层
回复学习学习
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 1个

财富等级: 恭喜发财

发表于 2020-12-28 12:56:03 | 显示全部楼层
现在代码链接的是整个工作表,怎么实现链接Excel工作表,如“A1:H7”区域。求楼主解答。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-17 21:40 , Processed in 0.394422 second(s), 36 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表