明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 4189|回复: 0

如何实现CAD中与Excel关联的操作

[复制链接]
发表于 2008-6-30 17:15:00 | 显示全部楼层 |阅读模式
本帖最后由 作者 于 2008-6-30 17:35:15 编辑

在帮助文档中有个关于Data link的例子

  1. // Create a data link and set it to the cell(2,2) of a selected table.
  2. void createAndSetDataLink()
  3. {
  4.  // Select a table
  5.  AcDbTable *pTbl = NULL;
  6.  if(NULL == (pTbl =
  7.  AcDbTable::cast(selectEntity(_T("\nSelect a table: ")))))
  8.  {
  9.   acutPrintf(ACRX_T("\nSelected entity was not a table!"));
  10.   return;
  11.  }
  12.  // Get an Excel file
  13.  //
  14.  struct resbuf *result;
  15.  int rc;
  16.  if ( (result = acutNewRb(RTSTR)) == NULL)
  17.  {
  18.   pTbl->close();
  19.   acutPrintf(ACRX_T("\nUnable to allocate buffer!"));
  20.   return;
  21.  }
  22.  result->resval.rstring=NULL;
  23.  
  24.  rc = acedGetFileD(ACRX_T("Excel File"), // Title
  25.       ACRX_T("c:/temp"), // Default pathname  
  26.       ACRX_T("xls"), //Default extension
  27.       16,     // Control flags
  28.       result); // The path selected by the user.
  29.  if (rc != RTNORM)
  30.  {
  31.   pTbl->close();
  32.   acutPrintf(ACRX_T("\nError in selecting an EXCEL file!"));
  33.   return;
  34.  }
  35.  // Retrieve the file name from the ResBuf.
  36.  ACHAR fileName[MAX_PATH];
  37.  _tcscpy(fileName, result->resval.rstring);
  38.  rc = acutRelRb(result);
  39.  static ACHAR sMyDataLink[MAX_PATH] = ACRX_T("MyDataLinkTest");
  40.  
  41.  // Get the Data Link Manager.
  42.  Acad::ErrorStatus es;
  43.  AcDbDataLinkManager* pDlMan = acdbHostApplicationServices()->workingDatabase()->getDataLinkManager();
  44.  assert(pDlMan);
  45.  
  46.  AcDbObjectId idDL;
  47.  AcDbDataLink *pDL = NULL; 
  48.  // Check if a Data Link with the name already exists. If so, remove it.
  49.  if( pDlMan->getDataLink(sMyDataLink, pDL, AcDb::kForRead) == Acad::eOk && pDL)
  50.  {
  51.   pDL->close();
  52.   es = pDlMan->removeDataLink(sMyDataLink, idDL);
  53.   if( es != Acad::eOk )
  54.   {
  55.    pTbl->close();
  56.    acutPrintf(ACRX_T("\nError in removing the Data Link!"));
  57.    return;
  58.   }
  59.  }
  60.  
  61.  // Create the Data Link with the name.
  62.  es = pDlMan->createDataLink(ACRX_T("AcExcel"), sMyDataLink, ACRX_T("This is a test for Excel type data link."), fileName, idDL);
  63.  if( es != Acad::eOk )
  64.  {
  65.   pTbl->close();
  66.   acutPrintf(ACRX_T("\nError in creating Data Link!\nPlease check if there is a sheet named 'Sheet1' in the XLS file."));
  67.   return;
  68.  }
  69.  
  70.  // Open the Data Link.
  71.  es = acdbOpenObject<AcDbDataLink>(pDL, idDL, AcDb::kForWrite);
  72.  if ( es != Acad::eOk || !pDL )
  73.  {
  74.   pTbl->close();
  75.   acutPrintf(ACRX_T("\nError in opening the Data Link object!"));
  76.   return;
  77.  }
  78.  
  79.  //  Set options of the Data Link
  80.  es = pDL->setOption(AcDb::kDataLinkOptionPersistCache);
  81.  es = pDL->setUpdateOption(pDL->updateOption() | AcDb::kUpdateOptionAllowSourceUpdate);
  82.  
  83.  // Close the Data Link.
  84.  pDL->close();
  85.  // Set data link to the table object at cell(2,2).
  86.  es = pTbl->setDataLink(2, 2, idDL, true);
  87.  if( es != Acad::eOk )
  88.  {
  89.   pTbl->close();
  90.   acutPrintf(ACRX_T("\nError in setting Data Link to the selected table!\nPlease check if there is a sheet named 'Sheet1' in the XLS file."));
  91.   return;
  92.  }
  93.  // Don't forget to close the table object.
  94.  es = pTbl->close();
  95. }

这里AcDbTable::cast(selectEntity(_T("\nSelect a table: ")))))

selectEntity函数没有定义,不知道要定义成什么内容合适。

目前已自定义了个selectEntity函数。实现了从外部Excel文件中读取数据到CAD下绘制的表格中。

 

现在还需要实现将CAD下表格中的书籍读到外部Excel中,并保存。有什么好的实现方法呢?

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

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 16:21 , Processed in 0.161644 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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