找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 558|回复: 0

Exporting Table contents

[复制链接]

已领礼包: 40个

财富等级: 招财进宝

发表于 2021-1-9 22:06:34 | 显示全部楼层 |阅读模式

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

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

×


问题:
I would like to save the table contents to a file. How can I do this ?

解答:
AutoCAD command “TABLEEXPORT” can be used to save the contents of a table in the CSV format. One way to do this is to send this command using "SendStringToExecute".

You can also do the same thing without sending a command by traversing through the table contents as shown in the following code snippet:

  1.         [CommandMethod("TabExp")]

  2.         public void commandMethodTest()

  3.         {

  4.             Document activeDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

  5.             Database db = activeDoc.Database;

  6.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;



  7.             PromptEntityOptions peo = new PromptEntityOptions("\nSelect a table : ");

  8.             peo.SetRejectMessage("\nMust be a table...");

  9.             peo.AddAllowedClass(typeof(Table), true);



  10.             PromptEntityResult per = ed.GetEntity(peo);



  11.             if (per.Status != PromptStatus.OK)

  12.                 return;



  13.             ObjectId oid = per.ObjectId;



  14.             String fileName = String.Empty;

  15.             SaveFileDialog sfd = new SaveFileDialog("Save As CSV", "Test", "csv", "Export Table As CSV", SaveFileDialog.SaveFileDialogFlags.AllowAnyExtension);

  16.             if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.OK)

  17.             {

  18.                 return;

  19.             }

  20.             fileName = sfd.Filename;



  21.             StreamWriter sw = new StreamWriter(fileName);



  22.             using (Transaction tr = db.TransactionManager.StartTransaction())

  23.             {

  24.                 Table table = (Table)tr.GetObject(oid, OpenMode.ForRead);



  25.                 int Rows = table.Rows.Count;

  26.                 int Cols = table.Columns.Count;



  27.                 for (int row = 0; row < Rows; row++)

  28.                 {

  29.                     String sRow = String.Empty;

  30.                     for (int col = 0; col < Cols; col++)

  31.                     {

  32.                         if(sRow == String.Empty)

  33.                             sRow = String.Format("{0}", table.Cells[row, col].GetTextString(FormatOption.FormatOptionNone));

  34.                         else

  35.                             sRow = String.Format("{0}, {1}", sRow, table.Cells[row, col].GetTextString(FormatOption.FormatOptionNone));

  36.                     }

  37.                     sw.WriteLine(sRow);

  38.                 }

  39.                 tr.Commit();

  40.             }



  41.             ed.WriteMessage("\nTable contents exported to " + fileName + " in CSV format.");

  42.             sw.Close();

  43.         }



论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-3-29 21:25 , Processed in 0.281978 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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