用C/C++ 读取一个存储在EXCEL中 (.xls)格式的矩阵二维数组,里面存储的整数。
可以用ADO来读取。先导入msado15.dll库。
参考如下代码:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__9DD2CDAB_EAB0_430E_A985_2215981EA616__INCLUDED_)
#define AFX_STDAFX_H__9DD2CDAB_EAB0_430E_A985_2215981EA616__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <afx.h>
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
//#import "C:\\Program Files\\common files\\system\\ado\\msado15.dll" no_namespace rename("EOF","adoEOF")
#include <iostream>
// TODO: reference additional headers your program requires here
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__9DD2CDAB_EAB0_430E_A985_2215981EA616__INCLUDED_)
//--------------------
// READ1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
using namespace std;
int main(int argc, char* argv[])
{
//printf("Hello World!\n");
HRESULT iniOK;
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRs;
iniOK = CoInitialize(NULL);
if(iniOK == S_OK)
{
m_pConnection.CreateInstance(__uuidof(Connection));
try
{
m_pConnection->Open("Provider=MSDASQL;Driver={Microsoft Excel Driver (*.xls)};DBQ=D:\\test.xls;ReadOnly=false;","","",adModeUnknown);
//cout << "_com_ OK"<<endl;
}
catch(_com_error e)
{
cout << "_com_ error"<<endl;
}
}
else
cout << "init Error"<<endl;
CString ss,s1,s2,s3;
int i=0;
try
{
m_pRs = m_pConnection->Execute((_bstr_t)("select * from [Sheet1$]"),NULL,adCmdText);
while(!m_pRs->adoEOF)
{
s1=(char*)(_bstr_t)m_pRs->GetCollect("第一列");
s2=(char*)(_bstr_t)m_pRs->GetCollect("第二列");
s3=(char*)(_bstr_t)m_pRs->GetCollect("第三列");
ss = s1 + "\t" +s2 + "\t" + s3;
cout << (LPCTSTR)ss << endl;
i++;
m_pRs->MoveNext();
}
}
catch(_com_error e)
{
cout << "_select_ error" <<endl;
}
cout << "records:" << i <<endl;
return 0;
}
在VC6.0下编译通过:
你好 非常感谢你的回答!
CString ss,s1,s2,s3;
假如是一个100X100的矩阵呢 那岂不是要S100~
你是说有100列?一般的EXCEL表也没有这么多的吧。
可以考虑用循环,每别读出每个列!~
广告 您可能关注的内容 |