当前位置:   article > 正文

C#CAD二次开发读取CAD表格数据_autocad.net读取excel

autocad.net读取excel
  1. Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  2. Database data = doc.Database;
  3. Editor ed = doc.Editor;
  4. AcadApplication AcadApp = (AcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application");
  5. AcadDocument AcadDoc = AcadApp.ActiveDocument;
  6. System.Data.DataTable dt = new System.Data.DataTable();
  7. using (Transaction trans = data.TransactionManager.StartTransaction())
  8. {
  9. object returnObj, pickPoint;
  10. string pickPrompt = "请选择Excel数据对象或者CAD表格对象:";
  11. try
  12. {
  13. AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
  14. }
  15. catch (System.Exception)
  16. {
  17. ed.WriteMessage("未选择对象,命令结束!");
  18. return;
  19. }
  20. AcadObject returnCADObj = (AcadObject)returnObj;
  21. AcRowType acRowType = new AcRowType();
  22. if (returnCADObj.ObjectName == "AcDbTable")
  23. {
  24. AcadTable cadTable = (AcadTable)returnCADObj;
  25. if ((cadTable.Columns > 0) && (cadTable.Rows > 0))
  26. {
  27. dt.Columns.Clear();
  28. #region 为DataTable添加列标题
  29. int title_row = 0;
  30. for (int j = 0; j < cadTable.Rows; j++)
  31. {
  32. acRowType = cadTable.GetRowType(j);
  33. //如果获取的行标题
  34. if (acRowType.ToString() == "acTitleRow")
  35. {
  36. title_row = j + 1;
  37. break;
  38. }
  39. }
  40. for (int i = 0; i < cadTable.Columns; i++)
  41. {
  42. dt.Columns.Add(cadTable.GetText(title_row, i));
  43. }
  44. #endregion
  45. #region 为DataTable添加数据
  46. int rowIndex = 0;
  47. for (int i = 0; i < cadTable.Rows; i++)
  48. {
  49. acRowType = cadTable.GetRowType(i);
  50. //如果获取的行不是数据行(而是表头或者标题或者位置类型)
  51. if (acRowType.ToString() != "acDataRow")
  52. continue;
  53. dt.Rows.Add();
  54. for (int j = 0; j < cadTable.Columns; j++)
  55. {
  56. dt.Rows[rowIndex][j] = cadTable.GetText(i, j);
  57. }
  58. rowIndex += 1;
  59. }
  60. #endregion
  61. }
  62. else
  63. ed.WriteMessage("选择错误,请重新选择!");
  64. }

                
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/114301?site
推荐阅读
相关标签
  

闽ICP备14008679号