Home > Software engineering >  Vc how to obtain open the EXCEL file object?
Vc how to obtain open the EXCEL file object?

Time:10-11

Consult in vc + +, I use the following code, first of all determine whether EXCEL file access the object to open if opened the book and close it, if there is no open open open,
HANDLE hFile=CreateFile (pathname, FILE_ALL_ACCESS FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
If (hFile==INVALID_HANDLE_VALUE)
{
Retrieves the hr=GetLastError ();
If (ERROR_ACCESS_DENIED==hr | | ERROR_SHARING_VIOLATION==hr)
{
//file occupied

}
}
The else
{
//file is not open
The CloseHandle (hFile);
}

I don't know how to get the open object, is to open the EXCEL file object assigned to book objects, also can't operate it, can't close it, after the file was taken, and close it, how do you write the code

CodePudding user response:

Master, please

CodePudding user response:

HFile is corresponding to the file handle,

CodePudding user response:

Clsids clsids;
Retrieves the hr;

//the Option 1. Get clsids from ProgID using CLSIDFromProgID.
LPCOLESTR progID=L "Excel. Application";
Hr=CLSIDFromProgID (progID, & amp; Clsids);
If (FAILED (hr))
{
MessageBox (_T (" start the service failure "), NULL, NULL);
return 1;
}

//the Option 2. Build the clsids directly.
//Start the server and get the IDispatch interface
IDispatch * pExcelApp=NULL;
Hr=CoCreateInstance (///- or - CoCreateInstanceEx CoGetObject
Clsids,//clsids of the server
NULL,
CLSCTX_LOCAL_SERVER,//Word. The Application is a local server
IID_IDispatch,//Query the IDispatch interface
(void * *) & amp; PExcelApp);//the Output

If (FAILED (hr))
{
return 1;
}

//Make Excel cause. (i.e. for Application. The Visible=0)
{
The VARIANT x;
X.v t=VT_I4;
X.l Val=1;
AutoWrap (DISPATCH_PROPERTYPUT, NULL, pExcelApp, L "Visible", 1, x);
}

//the open
IDispatch * pBooks=NULL;
{
The VARIANT result;
VariantInit (& amp; The result);
AutoWrap (DISPATCH_PROPERTYGET, & amp; The result, pExcelApp, L "Workbooks, 0);
PBooks=result. PdispVal;
}

//Call Workbooks. The Open () to get a book
IDispatch * pbook=NULL;
{
M_strFileName=_T (" excel file path ");
The VARIANT result;
The VARIANT vtFileName;
VtFileName. N=VT_BSTR;
VtFileName. BstrVal=SysAllocString (m_strFileName);
VariantInit (& amp; The result);
AutoWrap (DISPATCH_METHOD, & amp; The result, Workbooks, L "Open", 1, vtFileName);
Pbook=result. PdispVal;
}

CodePudding user response:

So complicated look not to understand
  • Related