如何实现CAD中与Excel关联的操作
本帖最后由 作者 于 2008-6-30 17:35:15 编辑 <br /><br /> <p dir="ltr" style="MARGIN-RIGHT: 0px;">在帮助文档中有个关于Data link的例子</p><ol><li><div class="codeLine" style="MARGIN-RIGHT: 0px;">// Create a data link and set it to the cell(2,2) of a selected table.</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;">void createAndSetDataLink()</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;">{</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> // Select a table</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> AcDbTable *pTbl = NULL;</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> if(NULL == (pTbl =</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> AcDbTable::cast(selectEntity(_T("\nSelect a table: "))))) </div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> {</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> acutPrintf(ACRX_T("\nSelected entity was not a table!"));</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> return;</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> }</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> // Get an Excel file</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> //</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> struct resbuf *result; </div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> int rc; </div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> if ( (result = acutNewRb(RTSTR)) == NULL)</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> {</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> pTbl->close();</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> acutPrintf(ACRX_T("\nUnable to allocate buffer!")); </div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> return;</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> }</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> result->resval.rstring=NULL; </div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> </div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> rc = acedGetFileD(ACRX_T("Excel File"), // Title</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> ACRX_T("c:/temp"), // Default pathname </div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> ACRX_T("xls"), //Default extension</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> 16, // Control flags</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> result); // The path selected by the user.</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> if (rc != RTNORM) </div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> {</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> pTbl->close();</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> acutPrintf(ACRX_T("\nError in selecting an EXCEL file!")); </div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> return;</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> }</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> // Retrieve the file name from the ResBuf.</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> ACHAR fileName;</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> _tcscpy(fileName, result->resval.rstring);</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> rc = acutRelRb(result);</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> static ACHAR sMyDataLink = ACRX_T("MyDataLinkTest");</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> </div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> // Get the Data Link Manager.</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> Acad::ErrorStatus es;</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> AcDbDataLinkManager* pDlMan = acdbHostApplicationServices()->workingDatabase()->getDataLinkManager();</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> assert(pDlMan);</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> </div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> AcDbObjectId idDL;</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> AcDbDataLink *pDL = NULL; </div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> // Check if a Data Link with the name already exists. If so, remove it.</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> if( pDlMan->getDataLink(sMyDataLink, pDL, AcDb::kForRead) == Acad::eOk && pDL)</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> {</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> pDL->close();</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> es = pDlMan->removeDataLink(sMyDataLink, idDL);</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> if( es != Acad::eOk )</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> {</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> pTbl->close();</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> acutPrintf(ACRX_T("\nError in removing the Data Link!"));</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> return;</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> }</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> }</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> </div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> // Create the Data Link with the name.</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> es = pDlMan->createDataLink(ACRX_T("AcExcel"), sMyDataLink, ACRX_T("This is a test for Excel type data link."), fileName, idDL);</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> if( es != Acad::eOk )</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> {</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> pTbl->close();</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> acutPrintf(ACRX_T("\nError in creating Data Link!\nPlease check if there is a sheet named 'Sheet1' in the XLS file."));</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> return;</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> }</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> </div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> // Open the Data Link.</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> es = acdbOpenObject<AcDbDataLink>(pDL, idDL, AcDb::kForWrite);</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> if ( es != Acad::eOk || !pDL )</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> {</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> pTbl->close();</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> acutPrintf(ACRX_T("\nError in opening the Data Link object!")); </div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> return;</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> }</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> </div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> // Set options of the Data Link</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> es = pDL->setOption(AcDb::kDataLinkOptionPersistCache);</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> es = pDL->setUpdateOption(pDL->updateOption() | AcDb::kUpdateOptionAllowSourceUpdate);</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> </div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> // Close the Data Link.</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> pDL->close();</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> // Set data link to the table object at cell(2,2).</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> es = pTbl->setDataLink(2, 2, idDL, true);</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> if( es != Acad::eOk )</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> {</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> pTbl->close();</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> 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."));</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> return;</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> }</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> // Don't forget to close the table object.</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;"> es = pTbl->close();</div></li><li><div class="codeLine" style="MARGIN-RIGHT: 0px;">}</div></li></ol><p class="codeLine" style="MARGIN-RIGHT: 0px;">这里AcDbTable::cast(selectEntity(_T("\nSelect a table: "))))) </p><p class="codeLine" style="MARGIN-RIGHT: 0px;">selectEntity函数没有定义,不知道要定义成什么内容合适。</p><p class="codeLine" style="MARGIN-RIGHT: 0px;">目前已自定义了个selectEntity函数。实现了从外部Excel文件中读取数据到CAD下绘制的表格中。</p><p class="codeLine" style="MARGIN-RIGHT: 0px;"> </p><p class="codeLine" style="MARGIN-RIGHT: 0px;">现在还需要实现将CAD下表格中的书籍读到外部Excel中,并保存。有什么好的实现方法呢?</p><!--Element not supported - Type: 8 Name: #comment-->
页:
[1]