首页 技术 正文
技术 2022年11月21日
0 收藏 515 点赞 3,262 浏览 2753 个字

本例实现的主要功能是读取excel文档中的内容,其次是将按照读取的信息在当前cad图纸中添加相应的实体。下面先介绍实现代码:

    CString excelPath; //外部excel文档的地址
UpdateData();//将对话框中的数据复制给变量
if (m_excelPath == _T(""))
{
MessageBox(_T("地址不能为空,请选择Excel文档!"));
return;
}
else
{
excelPath = m_excelPath;//m_excelPath是与地址对话框绑定的变量
} //设置基点,作为自动排布的所有实体的基点
AcGePoint3d ptBase;
ptBase.x = m_pointX;
ptBase.y = m_pointY;
ptBase.z = m_pointZ; //打开指定的Excel文档
CoInitialize(NULL);//初始化
if (!m_excel.pApp.CreateDispatch(_T("Excel.Application")))
{
MessageBox(_T("创建Excel服务失败!"));
exit();
}
m_excel.pApp.put_Visible(FALSE);
m_excel.pBooks.AttachDispatch(m_excel.pApp.get_Workbooks(), true);
m_excel.pBook.AttachDispatch(m_excel.pBooks.Open(excelPath, vtMissing, vtMissing, vtMissing,
vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing,
vtMissing, vtMissing, vtMissing));
m_excel.pSheets.AttachDispatch(m_excel.pBook.get_Worksheets(), true);
m_excel.pSheet.AttachDispatch(m_excel.pSheets.get_Item(_variant_t("sheet1")), true);
m_excel.pRange.AttachDispatch(m_excel.pSheet.get_Cells(), true); //获得Excel文档中已经使用的行数和列数
m_excel.pRange = m_excel.pSheet.get_UsedRange();//获得列数
m_excel.pRange = m_excel.pRange.get_Columns();
int colNum = m_excel.pRange.get_Count(); m_excel.pRange = m_excel.pSheet.get_UsedRange();//获得行数
m_excel.pRange = m_excel.pRange.get_Rows();
int RowNum = m_excel.pRange.get_Count(); //读取Excel中指定单元的数据
for (int i = ; i <= RowNum; i++)
{
CString ptX = m_excel.GetCString(, i);
CString ptY = m_excel.GetCString(, i);
CString ptZ = m_excel.GetCString(, i);
CString rad = m_excel.GetCString(, i); //按照excel文档中的数据转化成中心点
AcGePoint3d ptExcel;
ptExcel = strToDouble(ptX, ptY, ptZ);//将字符串型数据转换成双精度型,其中strToDouble()函数是自己所写,这里就不另外给出
double raduis ;
raduis = strToDouble(rad);//将字符串型rad转换成双精度型 //根据Excel中的图形类型,在模型空间添加相应的实体
CString grapfType = m_excel.GetCString(,i);
if (grapfType == _T("圆"))
{
//根据选择的基点设定圆的中心点
AcGePoint3d ptCenter;
ptCenter.x = ptExcel.x + ptBase.x;
ptCenter.y = ptExcel.y + ptBase.y;
ptCenter.z = ptExcel.z + ptBase.z;
AcGeVector3d vec(,,);
//定义实体圆
AcDbCircle *pCir = new AcDbCircle(ptCenter, vec, raduis);
PostToModelSpace(pCir);
}
if (grapfType == _T("直线"))
{
CString endPointX = m_excel.GetCString(, i);
CString endPointY = m_excel.GetCString(, i);
CString endPointZ = m_excel.GetCString(, i);
AcGePoint3d ptMid = strToDouble(endPointX, endPointY, endPointZ);
//按照选择的基点设置起点和终点
AcGePoint3d startPoint;//起点
startPoint.x = ptExcel.x + ptBase.x;
startPoint.y = ptExcel.y + ptBase.y;
startPoint.z = ptExcel.z + ptBase.z;
AcGePoint3d endPoint;//终点
endPoint.x = ptMid.x + ptBase.x;
endPoint.y = ptMid.y + ptBase.y;
endPoint.z = ptMid.z + ptBase.z; //将直线添加到模型空间
AcDbLine *pLine = new AcDbLine(startPoint, endPoint);
PostToModelSpace(pLine);
}
}
//完成所有操作后一定要清除掉excel文档相关的内容
m_excel.pRange.ReleaseDispatch();
m_excel.pSheet.ReleaseDispatch();
m_excel.pSheets.ReleaseDispatch();
m_excel.pBook.ReleaseDispatch();
m_excel.pBooks.ReleaseDispatch();
m_excel.pApp.Quit();
m_excel.pApp.ReleaseDispatch();

这里只是一个简单的例子,对于excel文档的读取基本操作都有涉及到。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918