public void SetDataLink() // This method can have any name { // Put your command code here Database db = Application.DocumentManager.MdiActiveDocument.Database; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; //获取目录表格的容量 int NumberPerPage = 1; PromptIntegerOptions GetNumberOption = new PromptIntegerOptions("\n输入每张表格的最大容量"); GetNumberOption.AllowNegative = false; GetNumberOption.AllowZero = false; PromptIntegerResult GetNumberResult = ed.GetInteger(GetNumberOption); if (GetNumberResult.Status == PromptStatus.OK) { NumberPerPage = GetNumberResult.Value; } else { return; } //获取图纸目录数据文件 string DataFile = ""; PromptOpenFileOptions fileoption = new PromptOpenFileOptions("\n输入链接数据文件路径"); fileoption.InitialDirectory = System.IO.Path.GetDirectoryName(db.Filename); fileoption.Filter = "Excel Documents (*.xlsx) |*.xlsx"; PromptFileNameResult DataFileResult = ed.GetFileNameForOpen(fileoption); if (DataFileResult.Status == PromptStatus.OK) { DataFile = DataFileResult.StringResult; } else { return; } //获取数据表范围信息 string SheetName = "图纸目录"; string StartCol = "A"; string EndCol = "E"; int StartRow = 2; PromptResult GetSheetName = ed.GetString("\n输入链接数据表名称"); if (GetSheetName.Status == PromptStatus.OK) { SheetName = GetSheetName.StringResult; } else { return; } PromptResult GetStartCol = ed.GetString("\n输入数据起始列"); if (GetStartCol.Status == PromptStatus.OK) { StartCol = GetStartCol.StringResult; } else { return; } PromptResult GetEndCol = ed.GetString("\n输入数据结束列"); if (GetEndCol.Status == PromptStatus.OK) { EndCol = GetEndCol.StringResult; } else { return; } PromptIntegerResult GetStartRow = ed.GetInteger("\n输入数据起始行"); if (GetStartRow.Status == PromptStatus.OK) { StartRow = GetStartRow.Value; } else { return; } using (Transaction Trans = db.TransactionManager.StartTransaction()) { DBDictionary Layouts = Trans.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary; ArrayList Layoutlist = new ArrayList(); foreach (DBDictionaryEntry item in Layouts) { if (item.Key != "Model") { Layoutlist.Add(item.Key); } } //int NumberOfList = Layoutlist.Count; ArrayList TableIDs = new ArrayList(); foreach (string name in Layoutlist) { TypedValue[] Filter = new TypedValue[] { new TypedValue((int)DxfCode.Operator, "<and"), new TypedValue((int)DxfCode.LayoutName, name), new TypedValue((int)DxfCode.Start, "ACAD_TABLE"), new TypedValue((int)DxfCode.Operator, "and>"), }; PromptSelectionResult selresult = ed.SelectAll(new SelectionFilter(Filter)); if (selresult.Status == PromptStatus.OK) { ObjectId[] ids = selresult.Value.GetObjectIds(); TableIDs.Add(ids[0]); } } int NumberOfTables = TableIDs.Count; /* * ed.WriteMessage("\nLayout:{0}", Layoutlist.Count); * foreach(string name in Layoutlist) * { * ed.WriteMessage("\nLayoutname:{0}", name); * } * ed.WriteMessage("\nTables:{0}", TableIDs.Count); */ DataLinkManager dlm = db.DataLinkManager; try { for (int i = 0; i < NumberOfTables; i++) { Autodesk.AutoCAD.DatabaseServices.DataLink dl = new Autodesk.AutoCAD.DatabaseServices.DataLink(); dl.DataAdapterId = "AcExcel"; dl.Name = SheetName + (i + 1).ToString(); dl.Description = SheetName + "数据链接" + (i + 1).ToString(); string location = string.Format("!{0}!{1}{2}:{3}{4}", SheetName, StartCol, (StartRow + i * NumberPerPage), EndCol, ((1 + i) * NumberPerPage) + StartRow - 1); dl.ConnectionString = DataFile + location; dl.DataLinkOption = DataLinkOption.PersistCache; dl.UpdateOption |= (int)UpdateOption.AllowSourceUpdate | (int)UpdateOption.SkipFormat; ObjectId dlId = dlm.AddDataLink(dl); ed.WriteMessage("\n链接字符串:{0}", dl.ConnectionString); Trans.AddNewlyCreatedDBObject(dl, true); Table tb = (Table)Trans.GetObject((ObjectId)TableIDs[i], OpenMode.ForWrite); tb.Cells[1, 0].DataLink = dlId; tb.GenerateLayout(); } Trans.Commit(); } catch (Autodesk.AutoCAD.Runtime.Exception Ex) { ed.WriteMessage("\n出错啦!" + Ex.ToString()); } finally { Trans.Dispose(); } } }
static public void CADTablebyExcelSheet() { const string dlName = "从Excel导入表格"; Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; // 文件打开窗口 OpenFileDialog ofd = new OpenFileDialog( "选择需要链接的Excel表格文档!", null, "xls;xlsx", "Excel链接到CAD", OpenFileDialog.OpenFileDialogFlags.DoNotTransferRemoteFiles ); System.Windows.Forms.DialogResult dr = ofd.ShowDialog(); if (dr != System.Windows.Forms.DialogResult.OK) { return; } ed.WriteMessage("\n选择到的文件为\"{0}\".", ofd.Filename); PromptPointResult ppr = ed.GetPoint("\n请选择表格插入点: "); if (ppr.Status != PromptStatus.OK) { return; } // 数据链接管理对象 DataLinkManager dlm = db.DataLinkManager; // 判断数据链接是否已经存在 如果存在移除 ObjectId dlId = dlm.GetDataLink(dlName); if (dlId != ObjectId.Null) { dlm.RemoveDataLink(dlId); } // 创建并添加新的数据链接 DataLink dl = new DataLink(); dl.DataAdapterId = "AcExcel"; dl.Name = dlName; dl.Description = "Excel fun with Through the Interface"; dl.ConnectionString = ofd.Filename; dl.UpdateOption |= (int)UpdateOption.AllowSourceUpdate; dlId = dlm.AddDataLink(dl); // 开启事务处理 using (Transaction trans = db.TransactionManager.StartTransaction()) { trans.AddNewlyCreatedDBObject(dl, true); BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; // 新建表格对象 Table tb = new Table(); tb.TableStyle = db.Tablestyle; tb.Position = ppr.Value; tb.SetDataLink(0, 0, dlId, true); tb.GenerateLayout(); BlockTableRecord btr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord; btr.AppendEntity(tb); trans.AddNewlyCreatedDBObject(tb, true); trans.Commit(); } // 强制恢复显示表格 ed.Regen(); }