COM组件把excel的xls转为csv格式

ArgenBarbie 2014-04-15 08:40:14
HRESULT ConverXLSToCSV(const char *pSrcFileName, const char *pDesFileName)
{
//单线程方式创建COM对象
::CoInitialize(0);
if(1)
{
//初始化EXCEL对象
CComDispatchDriver execlAppDsp;
HRESULT hr = CoCreateInstance(L"Excel.Application", 0, CLSCTX_LOCAL_SERVER|CLSCTX_INPROC_SERVER);
//获取工作页,打开XLS文件
CComVariant vWorkbooks;
hr = execlAppDsp.GetPropertyByName(L"Workbooks", &vWorkbooks);
CComDispatchDriver booksDsp(vWorkbooks.pdispVal);
CComVariant vSrcFileName(pSrcFileName);
hr = booksDsp.Invoke1(L"Open", &vSrcFileName);
//关闭警告对话框
CComVariant vDisplay(L"FALSE");
hr = execlAppDsp.PutPropertyByName(L"DisplayAlerts", &vDisplay);
//打开活动页,另存为CSV文件
CComVariant vActBook;
hr = execlAppDsp.GetPropertyByName(L"ActiveWorkbook", &vActBook);
CComDispatchDriver activeBookDsp(vActBook.pdispVal);
CComVariant vDesFileName(pDesFileName);
CComVariant vFileType("6");
hr = activeBookDsp.Invoke2(L"SaveAs", &vDesFileName, &vFileType);
//关闭EXCEL
execlAppDsp.Invoke0(L"Quit");
}
//释放
::CoUninitialize();
return S_OK;
}

其中HRESULT hr = CoCreateInstance(L"Excel.Application", 0, CLSCTX_LOCAL_SERVER|CLSCTX_INPROC_SERVER);这条语句少了几个参数,请问怎么加呀?
...全文
264 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-04-15
  • 打赏
  • 举报
回复
在Excel 2003中开始记录宏,手动完成所需功能,结束记录宏,按Alt+F11键,查看刚才记录的宏对应的VBA代码。
buyong 2014-04-15
  • 打赏
  • 举报
回复
引用 2 楼 ArgenBarbie 的回复:
[quote=引用 1 楼 luciferisnotsatan 的回复:] 查MSDN呀 STDAPI CoCreateInstance( REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID riid, LPVOID * ppv );
我不知道最后的两个参数REFIID riid, LPVOID * ppv要怎么写[/quote] 0, NULL
luciferisnotsatan 2014-04-15
  • 打赏
  • 举报
回复
Remarks The CoCreateInstance helper function provides a convenient shortcut by connecting to the class object associated with the specified CLSID, creating an uninitialized instance, and releasing the class object. As such, it encapsulates the following functionality: Copy Code CoGetClassObject(rclsid, dwClsContext, NULL, IID_IClassFactory, &pCF); hresult = pCF->CreateInstance(pUnkOuter, riid, ppvObj) pCF->Release(); It is convenient to use CoCreateInstance when you need to create only a single instance of an object on the local machine. If you are creating an instance on remote machine, call CoCreateInstanceEx. When you are creating multiple instances, it is more efficient to obtain a pointer to the class object's IClassFactory interface and use its methods as needed. In the latter case, you should use the CoGetClassObject function. In the CLSCTX enumeration, you can specify the type of server used to manage the object. The constants can be CLSCTX_INPROC_SERVER, CLSCTX_INPROC_HANDLER, CLSCTX_LOCAL_SERVER, or any combination of these values. The constant CLSCTX_ALL is defined as the combination of all three. For more information about the use of one or a combination of these constants, refer to CLSCTX.
ArgenBarbie 2014-04-15
  • 打赏
  • 举报
回复
引用 1 楼 luciferisnotsatan 的回复:
查MSDN呀 STDAPI CoCreateInstance( REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID riid, LPVOID * ppv );
我不知道最后的两个参数REFIID riid, LPVOID * ppv要怎么写
luciferisnotsatan 2014-04-15
  • 打赏
  • 举报
回复
查MSDN呀 STDAPI CoCreateInstance( REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID riid, LPVOID * ppv ); Parameters rclsid [in] CLSID associated with the data and code that will be used to create the object. pUnkOuter [in] If NULL, indicates that the object is not being created as part of an aggregate. If non-NULL, pointer to the aggregate object's IUnknown interface (the controlling IUnknown). dwClsContext [in] Context in which the code that manages the newly created object will run. The values are taken from the enumeration CLSCTX. riid [in] Reference to the identifier of the interface to be used to communicate with the object. ppv [out] Address of pointer variable that receives the interface pointer requested in riid. Upon successful return, *ppv contains the requested interface pointer. Upon failure, *ppv contains NULL. Return Values S_OK An instance of the specified object class was successfully created. REGDB_E_CLASSNOTREG A specified class is not registered in the registration database. Also can indicate that the type of server you requested in the CLSCTX enumeration is not registered or the values for the server types in the registry are corrupt. CLASS_E_NOAGGREGATION This class cannot be created as part of an aggregate. E_NOINTERFACE The specified class does not implement the requested interface, or the controlling IUnknown does not expose the requested interface.
ArgenBarbie 2014-04-15
  • 打赏
  • 举报
回复
CComDispatchDriver execlAppDsp; IDispatch *pDispExcel;// Dispatch interface CLSID clsidExcel; HRESULT hr; hr=CLSIDFromProgID(L"Excel.Application", &clsidExcel); if (FAILED(hr)) { MessageBox(NULL, "Excel not registered.", "Error", MB_OK); return ; } hr= CoCreateInstance(clsidExcel, 0, CLSCTX_LOCAL_SERVER|CLSCTX_INPROC_SERVER,IID_IDispatch,(void**)&pDispExcel); execlAppDsp=pDispExcel;

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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