Retrieves the hr;//retrieves the function return value
Hr=CoInitialize (NULL);//CoInitialize to tell Windows to create a com object in the form of single thread
If (FAILED (hr))
{
AfxMessageBox (_T (" Failed to call Coinitialize () "));
}
CFileDialog filedlg (TRUE, L "*. XLS", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L ".xls files (*. XLS) | *. XLS ");
Filedlg. M_ofn. LpstrTitle=L "open file";
Cstrings strFilePath;
If (IDOK==filedlg. DoModal ())
{
StrFilePath=filedlg. GetPathName ();
}
The else
{
return;
}
CApplication app1.
CWorkbooks books;
CWorkbook book;
CWorksheets sheets;
CWorksheet sheet;
CRange range;
CRange iCell;
LPDISPATCH lpDisp;
COleVariant vResult;//COleVariant class is the encapsulation of the VARIANT structure
GetDlgItem (IDC_READ) - & gt; The EnableWindow (FALSE);
COleVariant covOptional ((long) DISP_E_PARAMNOTFOUND VT_ERROR);
if (! . CreateDispatch (_T (" Excel. Application "), NULL))
{
AfxMessageBox (_T (" Excel server could not be started!" ));
return;
}
Books. AttachDispatch (. Get_Workbooks ());
LpDisp=books. The Open (strFilePath, covOptional, covOptional,
CovOptional, covOptional, covOptional, covOptional, covOptional,
CovOptional, covOptional, covOptional, covOptional, covOptional,
CovOptional, covOptional);
//get the Workbook
Book. AttachDispatch (lpDisp);
//get the Worksheets
Sheets. AttachDispatch (book. Get_Worksheets ());
//sheet=sheets. Get_Item (COleVariant ((short) 1));
//get the current active sheet
//if a cell is in edit status, this operation can't return, will have been waiting for
LpDisp=book. Get_ActiveSheet ();
Sheet. AttachDispatch (lpDisp);
//read the value of the first cell
Range. AttachDispatch (sheet. Get_Cells ());
Range. AttachDispatch (range. Get_Item (COleVariant (2) (long), COleVariant ((long), 1)). PdispVal);//the first variable is the row, the second variable is column, that is, the second line of the first column
VResult=range. Get_Value2 ();
Cstrings STR.
If (vResult. N==VT_BSTR)//string
{
STR=vResult. BstrVal;
}
Else if (vResult. N==VT_R8)//8 byte number
{
STR. The Format (_T (" % f "), vResult. DblVal);
}
//. Put_Visible (TRUE);
//. Put_UserControl (TRUE);
Books. The Close ();
. The Quit ();
//release object
Range. ReleaseDispatch ();
Sheet. ReleaseDispatch ();
Sheets. ReleaseDispatch ();
Book. ReleaseDispatch ();
Books. ReleaseDispatch ();
. ReleaseDispatch ();
//OnOK ();
MessageBox(str);
CodePudding user response:
Used to share my class, the function is completeLink: https://pan.baidu.com/s/1VvK-9qQkQqBrshGtfZ4Pew
The extracted code: LGPL
CodePudding user response:
I tried, no problem, your code a little changed:
CFileDialog filedlg (TRUE, L "*. XLSX", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L "Xls file (*. XLSX) | *. XLSX");
In fact it is best to:
CFileDialog DLG (
TRUE,
_T (". XLSX "),
NULL,
OFN_HIDEREADONLY | OFN_FILEMUSTEXIST,
_T (" XLSX files (*. XLSX) | *. XLSX | All files (*. *) | *. * | | ")
);
CodePudding user response:
Ah oh, you came late and share files have been deleted, early next time yo,