如何通过COM对象读写Excel连同Excel实例手动操作
How to read and write Excel via COM object together with Excel instance manual operating
我正在尝试通过 COM 读写 Excel。
同时,我也想手动修改单元格的值。
似乎存在访问冲突。
我怎样才能检测到这种冲突并避免它/
以下是我的代码示例。
在该示例中,do-loop 将继续将单元格 A1 的值复制到单元格 A2。
当我想在 COM 运行 期间手动修改值 A1 时,发生错误并退出程序。
//MicroSoft Office Objects
#import "C:\Program Files (x86)\Common Files\microsoft shared\OFFICE12\mso.dll"\
rename("DocumentProperties","DocumentPropertiesXL")\
rename("RGB","RBGXL")
//Microsoft VBA Objects
#import "C:\Program Files (x86)\Common Files\microsoft shared\VBA\VBA6\vbe6ext.olb"
//EXCEL application objects
#import "C:\Program Files (x86)\Microsoft Office\Office12\excel.exe" \
rename("DialogBox","DialogBoxXL") \
rename("RGB","RGBCL") \
rename("DocumentProperties","DocumentPropertiesXL")\
rename("ReplaceText","ReplaceTextXL") \
rename("CopyFile","CopyFileXL") \
exclude("IFont","IPicture") no_dual_interfaces
#include "stdafx.h"
#include <iostream>
int main(int argc, _TCHAR* argv[])
{
//A try block is used to trap any errors in communication
try
{
//Initialize COM interface
CoInitialize(NULL);
Excel::_ApplicationPtr XL;
//Start the Excel Application
XL.CreateInstance(L"Excel.Application");
//Make the Excel Application visible, so that we can see it!
XL->Visible=true;
Excel::_WorkbookPtr pWorkbook;
pWorkbook=XL->Workbooks->Open(L"D:\test.xls");
//Get a pointer to the active worksheet
Excel::_WorksheetPtr pSheet = pWorkbook->Sheets->Item[L"Sheet1"];
//Get a pointer to the cells on the active worksheet
Excel::RangePtr pRange = pSheet->Cells;
pRange->Item[1][1]=20;
int a;
//perform a loop to copy value in A1 to A2
do
{
a=pRange->Item[1][1];
std::cout<<"a="<<a<<std::endl;
pRange->Item[2][1]=a;
}while(a>0);
}
//If a communication error is thrown, catch it and complain
catch(_com_error)
{
std::cout<<"COM error "<<std::endl;
}
//Finally Uninitialise the COM interface
CoUninitialize();
//Finish the C++ program
return 0;
}
我已经通过直接使用COM而不是OLE解决了这个问题
阅读 freddie1 的文章后 http://www.cplusplus.com/forum/windows/125996/
谢谢。
我正在尝试通过 COM 读写 Excel。 同时,我也想手动修改单元格的值。 似乎存在访问冲突。 我怎样才能检测到这种冲突并避免它/
以下是我的代码示例。 在该示例中,do-loop 将继续将单元格 A1 的值复制到单元格 A2。 当我想在 COM 运行 期间手动修改值 A1 时,发生错误并退出程序。
//MicroSoft Office Objects
#import "C:\Program Files (x86)\Common Files\microsoft shared\OFFICE12\mso.dll"\
rename("DocumentProperties","DocumentPropertiesXL")\
rename("RGB","RBGXL")
//Microsoft VBA Objects
#import "C:\Program Files (x86)\Common Files\microsoft shared\VBA\VBA6\vbe6ext.olb"
//EXCEL application objects
#import "C:\Program Files (x86)\Microsoft Office\Office12\excel.exe" \
rename("DialogBox","DialogBoxXL") \
rename("RGB","RGBCL") \
rename("DocumentProperties","DocumentPropertiesXL")\
rename("ReplaceText","ReplaceTextXL") \
rename("CopyFile","CopyFileXL") \
exclude("IFont","IPicture") no_dual_interfaces
#include "stdafx.h"
#include <iostream>
int main(int argc, _TCHAR* argv[])
{
//A try block is used to trap any errors in communication
try
{
//Initialize COM interface
CoInitialize(NULL);
Excel::_ApplicationPtr XL;
//Start the Excel Application
XL.CreateInstance(L"Excel.Application");
//Make the Excel Application visible, so that we can see it!
XL->Visible=true;
Excel::_WorkbookPtr pWorkbook;
pWorkbook=XL->Workbooks->Open(L"D:\test.xls");
//Get a pointer to the active worksheet
Excel::_WorksheetPtr pSheet = pWorkbook->Sheets->Item[L"Sheet1"];
//Get a pointer to the cells on the active worksheet
Excel::RangePtr pRange = pSheet->Cells;
pRange->Item[1][1]=20;
int a;
//perform a loop to copy value in A1 to A2
do
{
a=pRange->Item[1][1];
std::cout<<"a="<<a<<std::endl;
pRange->Item[2][1]=a;
}while(a>0);
}
//If a communication error is thrown, catch it and complain
catch(_com_error)
{
std::cout<<"COM error "<<std::endl;
}
//Finally Uninitialise the COM interface
CoUninitialize();
//Finish the C++ program
return 0;
}
我已经通过直接使用COM而不是OLE解决了这个问题 阅读 freddie1 的文章后 http://www.cplusplus.com/forum/windows/125996/
谢谢。