Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database data = doc.Database;
            Editor ed = doc.Editor;
            AcadApplication AcadApp = (AcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application");
            AcadDocument AcadDoc = AcadApp.ActiveDocument;
            System.Data.DataTable dt = new System.Data.DataTable();

            using (Transaction trans = data.TransactionManager.StartTransaction())
            {
                object returnObj, pickPoint;
                string pickPrompt = "请选择Excel数据对象或者CAD表格对象:";
                try
                {
                    AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
                }
                catch (System.Exception)
                {
                    ed.WriteMessage("未选择对象,命令结束!");
                    return;
                }
                
                AcadObject returnCADObj = (AcadObject)returnObj;
                AcRowType acRowType = new AcRowType();

                if (returnCADObj.ObjectName == "AcDbTable")
                {
                    AcadTable cadTable = (AcadTable)returnCADObj;
                    if ((cadTable.Columns > 0) && (cadTable.Rows > 0))
                    {
                        dt.Columns.Clear();
                        #region  为DataTable添加列标题
                        int title_row = 0;
                        for (int j = 0; j < cadTable.Rows; j++)
                        {
                            acRowType = cadTable.GetRowType(j);

                            //如果获取的行标题
                            if (acRowType.ToString() == "acTitleRow")
                            {
                                title_row = j + 1;
                                break;
                            }
                        }
                        for (int i = 0; i < cadTable.Columns; i++)
                        {
                            dt.Columns.Add(cadTable.GetText(title_row, i));
                        }
                        #endregion

                        #region  为DataTable添加数据
                        int rowIndex = 0;
                        for (int i = 0; i < cadTable.Rows; i++)
                        {
                            acRowType = cadTable.GetRowType(i);
                            //如果获取的行不是数据行(而是表头或者标题或者位置类型)
                            if (acRowType.ToString() != "acDataRow")
                                continue;
                            dt.Rows.Add();
                            for (int j = 0; j < cadTable.Columns; j++)
                            {
                                dt.Rows[rowIndex][j] = cadTable.GetText(i, j);
                            }
                            rowIndex += 1;
                        }
                        #endregion
                    }
                    else
                        ed.WriteMessage("选择错误,请重新选择!");
	      }

            

本文转载:CSDN博客