1 Install excel version of office2007 or above
2. Add 7 basic classes to the project project (Excel, as an OLE/COM library plug-in, defines various interfaces for interaction, these interfaces are cross-language interfaces. VC can import these interfaces and use the interfaces to Excel operations), byIn this article, I only care about reading the data in the Excel table, and mainly focus on the 7 connections _Application, Workbooks, _Workbook, Worksheets, _Worksheet, Range, Font0.
The steps for VS2012 to import the interface of the OLE/COM component are: Project -> Class Wizard -> Add Class -> MFC class in the type library (MFC class in TypeLib), first select the path where the component to be imported is located, that is, Excel. The path where the exe is located, and then select the interface in the Excel type library to be imported. The component path is generally C:\Program Files\Microsoft Office\Office15\EXCEL.exe; the format is similar.
After importing, the related classes will be automatically imported into the project. When using, you need to add their header files. The main header files are:
#include "CApplication.h"
#include "CWorkbook.h"
#include "CWorkbooks.h"
#include "CWorksheet.h"
#include "CWorksheets.h"
#include "CRange.h"
#include "CFont0.h"
3. After importing, you need to comment out "#import "C:\\Program Files\\Microsoft Office\\Office12\\EXCEL.EXE" no_namespace", and then add the header file: #include <afxdisp. h> Go to the 7 files above.
4. If there is an error error C2059, double-click error C2059, change VARIANT DialogBox() to VARIANT _DialogBox(), compile again, pass! !
void CMFCApplicationWriteExcelDlg::OnBnClickedButton3() { /* 1 Install excel version of office2007 or above 2. Add 7 basic classes to the project (Excel, as an OLE/COM library plug-in, defines various interfaces for interaction, These interfaces are cross-language interfaces. VC can import these interfaces and operate Excel through the interfaces), by In this article, I only care about reading the data in the Excel table, mainly focusing on 7 connections: _Application, Workbooks, _Workbook、Worksheets、_Worksheet、Range、Font0。 The steps for VS2012 to import the interface of the OLE/COM component are: Project -> Class Wizard -> Add Class -> MFC Class in Type Library (MFC class in TypeLib), first select the path where the component to be imported is located, that is, the path where Excel.exe is located, and then Select the interface in the Excel type library to import. The component path is generally C:\Program Files\Microsoft Office \Office15\EXCEL.exe; the format is similar. After importing, the related classes will be automatically imported into the project. When using, you need to add their header files. The main header files are: #include "CApplication.h" #include "CWorkbook.h" #include "CWorkbooks.h" #include "CWorksheet.h" #include "CWorksheets.h" #include "CRange.h" #include "CFont0.h" 3. After importing, you need "#import "C:\\Program Files\\Microsoft Office\\Office12\\EXCEL.EXE" no_namespace", and then add the header file: #include <afxdisp.h> to the above 7 files. 4. If there is an error error C2059, double-click error C2059, and change VARIANT DialogBox() to VARIANT _DialogBox() Compile again, pass! ! */ //Use the first sheet by default int nSheetNumber=1; //create object CApplication app; if( !app.CreateDispatch(_T("Excel.Application"))) { AfxMessageBox("Office 2007 or above is not installed, the excel file cannot be operated"); return ; } //define other internal objects CWorkbooks books; CWorkbook book; CWorksheets sheets; CWorksheet sheet; CFont0 font; CRange range; CString FieldName,FieldValue; COleVariant covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR); books=app.get_Workbooks(); //You need to create an excel file on the c drive, or you can change it to the excel file that comes with the system // book=books.Add(COleVariant("c:\\aa.xlsx")); book = books.Add(covOptional); book.put_Title("Mybooks"); sheets=book.get_Sheets(); sheet=sheets.get_Item(COleVariant((short)nSheetNumber)); sheet.put_Name("wxpSheet"); //--------------------- app.put_Visible(TRUE);//Visibility app.put_UserControl(TRUE);//Read only //set title FieldName="a1"; FieldValue="DataA"; range=sheet.get_Range(COleVariant(FieldName),COleVariant(FieldName)); range.put_Value2(COleVariant(FieldValue)); FieldName="B1"; FieldValue="DataB"; range=sheet.get_Range(COleVariant(FieldName),COleVariant(FieldName)); range.put_Value2(COleVariant(FieldValue)); //Data input---------------------------------------------- --------- for(int row=1;row<10;++row) { // data for each row for(int col=0;col<10;++col) { //Place your own specified data ----------------------------- FieldName.Format("%c%d",'a'+col,(row+1)); FieldValue.Format("(%d,%d)",row+1,col+1); range=sheet.get_Range(COleVariant(FieldName),COleVariant(FieldName)); // range.put_Value2(COleVariant(FieldValue)); range.put_Formula(_variant_t("=RAND()*100")); range.put_NumberFormat(_variant_t("00.00")); //Set the line color if(row %2==0) { font=range.get_Font(); font.put_Bold(COleVariant((short)TRUE)); font.put_Color(COleVariant((long)RGB(255, 0, 0))); // font.put_Name(COleVariant(_T("bold"))); } }//end of col }//end of row //Design calculation formulas, all formulas in excel can be used ------------------------- FieldName="a11"; range=sheet.get_Range(COleVariant(FieldName),COleVariant(FieldName)); range.put_Formula(COleVariant(_T("=sum(a2:a10"))); //Adaptive column width -------------------------------- CRange cols; cols=range.get_EntireColumn(); cols.AutoFit(); //::Sleep(5000); //Read content --------------------------------------------- -------- FieldName="a11"; range = sheet.get_Range(COleVariant(FieldName),COleVariant(FieldName)); COleVariant cov = range.get_Value2(); CString str="success"; the.ChangeType (VT_BSTR); str = cov.bstrVal; AfxMessageBox(str); //------------------------------------------------------------------ //Close the excel file and decide as needed books.Close(); app.Quit(); books.ReleaseDispatch(); app.ReleaseDispatch(); }