// 建立XLS数据连接
BOOL CExportToExcel::CreateXlsConnection(const CString& strPath, _ConnectionPtr& ipConnection)
{
if (strPath.IsEmpty())
{
SendAnErrorMessage(_T("路径为空"));
return FALSE;
}
ipConnection = NULL;
try
{
::CoInitialize(NULL);
CString strConn = _T("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=");
strConn += strPath;
strConn += _T(";Extended Properties = \"Excel 5.0;HDR = Yes;IMEX=0\";Persist Security Info=false");
BSTR bstrConnectionString = strConn.AllocSysString();
HRESULT hr = m_ipXlsConnection.CreateInstance("ADODB.Connection");
if (FAILED(hr))
{
return FALSE;
}
hr = ipConnection->Open(bstrConnectionString, "", "", adModeUnknown);
if (FAILED(hr))
{
return FALSE;
}
_bstr_t bstrCreateSql = "CREATE TABLE TESTTABLE(ID INT, USER_NAME varchar(20))";
ipConnection->Execute(bstrCreateSql, NULL, adCmdText);
ipConnection->Close();
::CoUninitialize();
}
catch (...)
{
return FALSE;
}
return TRUE;
}