The COM API call is the positive solution, but you need to write the COM interface class, the Excel object library has too many interfaces ... However, it can be generated automatically with tools.
I also recently used VC operation Excel, in the VC can do so, in any one CPP file add the following three lines:
1#import"C:\Program Files\Common Files\Microsoft Shared\office11\mso. DLL"Rename"RGB","RGB") Rename ("DocumentProperties","document_properties")2#import"C:\Program Files\Common Files\Microsoft Shared\vba\vba6\vbe6ext. OLB"3#import"D:\Program Files\Microsoft Office\office11\excel. EXE"Rename"RGB","RGB") Rename ("DialogBox","Dialog_box")
and then compile.
Note that this does not make sense, but it does not matter, in the debug directory you will see MSO.TLH, Mso.tli, VB6EXT.TLH, Vb6ext.tli, EXCEL.TLH, Excel.tli six files, they are the VC compiler automatically generated by type library information interface type definition and some auxiliary classes. Copy them to your source code directory, and then open VB6EXT.TLH to make changes:
1 #include <comdef.h>2"mso.tlh"// Join this line 3 4namespace5{6 using namespace Office; // Join this line 7 ...
Then modify the Excel.tlh file:
1#include <comdef.h>2#include"MSO.TLH" //Join this line3#include"VBE6EXT.TLH" //Join this line4 5 namespaceExcel6 {7 using namespaceVbide;//Join this line
At this time, the three lines #import deleted, and replaced by the # include "EXCEL.TLH" can be used:
1#include"stdafx.h"2#include"EXCEL.TLH"3#include <iostream>4 5 int_tmain (intARGC, _tchar*argv[])6 7 {8CoInitializeEx (0, coinit_apartmentthreaded);9 excel::_applicationptr xApp;Ten xapp.createinstance (__uuidof (excel::application)); OneExcel::_workbookptr Xbook = Xapp->workbooks->open (L"test.xsl"); AExcel::_worksheetptr Xsheet = xbook->ActiveSheet; - -_bstr_t Text = Xsheet->range[l"A1"][vtmissing]->text;//Read cell contents, _bstr_t encapsulates COM wide string BSTR theStd::cout << static_cast<Char Const*> (text);//The _bstr_t class defines a type conversion to char const * that converts a Unicode string into a multibyte-encoded string of the current code page - return 0; -}
Read Excel as a database directly from ODBC
1#include <odbcinst.h>2#include <afxdb.h>3 4 5CString Strxlsfilename ="X:\\xxx.xls";6 CString strSQL;7Strsql.format (L"driver={microsoft EXCEL DRIVER (*.xls)};D sn= "; Firstrowhasnames=1; Readonly=false; Create_db=\ "%s\";D bq=%s", Strxlsfilename, strxlsfilename);8 9CDatabase *pcdatabase =NewCDatabase; TenPcdatabase->OpenEx (strSQL, cdatabase::noodbcdialog); One ACString strSheetName ="the name of the table you want to manipulate"; - CString strSQL; -Strsql.format (L"SELECT * FROM [%s$a1:iv65536]", strSheetName); the -CRecordset *pcrecordset =NewCRecordset (pcdatabase); -Pcrecordset->Open (crecordset::forwardonly, strSQL, crecordset::readonly); - + CString Strfield; -Pcrecordset->getfieldvalue (( Short)0, Strfield);//reads the cell contents of the 1th column of row 1th +Pcrecordset->getfieldvalue (( Short)3, Strfield);//1th Row 4th column APcrecordset->movenext ();//The cursor points to the next line, and then getfieldvalue to read the contents of the next line.
C + + reads data from Excel-specific columns