VC将数据写入EXCEL出错,帮忙看看

lang14 2012-04-09 08:49:28
在BOOL CVCExcelApp::InitInstance()之中,int nResponse = dlg.DoModal()语句之后加入如下代码:
if(!AfxOleInit())///初始化COM库
{
AfxMessageBox("初始化COM失败");
return FALSE;
}
为了能调用Excel的接口我们打开MFC ClassWizard->Automation->Add Class->From a type library,选择[Excel的安装路径]\EXCEL.exe,然后把所有的类都添加进去,头文件为excel.h,源文件为excel.cpp。当然,你也可以只把一些比较常用的类如_Application、Workbooks、_Workbook、Worksheets、_Worksheet、Range加进去,因为网上流传的绝大部分教程都只添加这几个类,这完全根据你个人的需要。但在本例中我们至少还要用到Interior类(设置底色),Font类(设置字体),而且这样做又简单又方便扩展功能,不管三七二十一全部弄进去吧!但这样做会有一个问题,有可能产生类的名字冲突,例如本来你自己写了一个类叫Font,当你全部添加时又再次加入了Font类,这样就重复定义了,不过可以通过名字空间来解决这个问题。再在VCExcelDlg.cpp文件的头部添加(如果系统已经自动添加就不要重复添加了):
#include "VCExcel.h"
#include "comdef.h"
这样一来我们的程序就可以自由调用EXCEL了,一切准备就绪。

我按了上面步骤进行添加excel类了,但是为什么会运行错误呢?我是在现有的工程添加类的,错误为:
Compiling...
excel.cpp
c:\documents and settings\chenbikui\桌面\testmenu\excel.h(7879) : warning C4099: 'Parameter' : type name first seen using 'struct' now seen using 'class'
d:\work\汽车仪表检测\soft\testmenu\debug\msado15.tlh(97) : see declaration of 'Parameter'
c:\documents and settings\chenbikui\桌面\testmenu\excel.h(7909) : error C2011: 'Parameters' : 'struct' type redefinition
c:\documents and settings\chenbikui\桌面\testmenu\excel.h(10594) : error C2011: 'Errors' : 'struct' type redefinition
c:\documents and settings\chenbikui\桌面\testmenu\excel.h(10615) : error C2011: 'Error' : 'struct' type redefinition
C:\Documents and Settings\chenbikui\桌面\testmenu\excel.cpp(43000) : error C2039: 'GetApplication' : is not a member of 'Parameters'
d:\work\汽车仪表检测\soft\testmenu\debug\msado15.tlh(2035) : see declaration of 'Parameters'
C:\Documents and Settings\chenbikui\桌面\testmenu\excel.cpp(43003) : error C2065: 'InvokeHelper' : undeclared identifier
C:\Documents and Settings\chenbikui\桌面\testmenu\excel.cpp(43007) : error C2039: 'GetCreator' : is not a member of 'Parameters'
d:\work\汽车仪表检测\soft\testmenu\debug\msado15.tlh(2035) : see declaration of 'Parameters'
C:\Documents and Settings\chenbikui\桌面\testmenu\excel.cpp(43014) : error C2039: 'GetParent' : is not a member of 'Parameters'
d:\work\汽车仪表检测\soft\testmenu\debug\msado15.tlh(2035) : see declaration of 'Parameters'
C:\Documents and Settings\chenbikui\桌面\testmenu\excel.cpp(43021) : error C2039: 'Add' : is not a member of 'Parameters'
d:\work\汽车仪表检测\soft\testmenu\debug\msado15.tlh(2035) : see declaration of 'Parameters'
C:\Documents and Settings\chenbikui\桌面\testmenu\excel.cpp(43032) : error C2509: 'GetCount' : member function not declared in 'Parameters'
d:\work\汽车仪表检测\soft\testmenu\debug\msado15.tlh(2035) : see declaration of 'Parameters'
C:\Documents and Settings\chenbikui\桌面\testmenu\excel.cpp(43039) : error C2063: 'Item' : not a function
C:\Documents and Settings\chenbikui\桌面\testmenu\excel.cpp(43039) : error C2040: 'Item' : 'struct IDispatch *(const struct tagVARIANT &)' differs in levels of indirection from 'class _com_ptr_t<class _com_IIID<struct _Parameter,&struct __s_GUID _GU
ID_0000050c_0000_0010_8000_00aa006d2ea4> >'
C:\Documents and Settings\chenbikui\桌面\testmenu\excel.cpp(43039) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
Creating browse info file...
BSCMAKE: error BK1506 : cannot open file '.\Debug\excel.sbr': No such file or directory
Error executing bscmake.exe.



假如我新建一个工程,按如上方法添加excel类的时候没有报错,不知道上面的错误是什么回事,请大牛们帮忙,多谢!!!
...全文
343 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
katiexie 2012-04-14
  • 打赏
  • 举报
回复
你可能使用了ADO的,与ADO中的一些类名冲突了
  • 打赏
  • 举报
回复
http://www.cnblogs.com/slaughter/archive/2008/05/07/1187223.html

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧