excelvc編程
Ⅰ VC++(MFC)如何從對話框寫數據到Excel
MFC訪問EXCEL,那可是比較麻煩的了。給個以前做過的項目。是訪問資料庫的 你自己整理著看下 希望有幫助。
if(m_bDataBase) //有資料庫
{
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///創建Connection對象
if(SUCCEEDED(hr))
{
m_pConnection->ConnectionTimeout=3;///設置超時時間為3秒
CString szOpen = theApp.m_szDataMisDir;
if(szOpen.Right(1) != "\\")
szOpen += "\\";
//hr = m_pConnection->Open(Filepath,"","",adModeUnknown);
#ifndef _OFFICE97
szOpen = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+szOpen+"BiocaDatabase.mdb;";
#else
szOpen = "Provider=Microsoft.Jet.OLEDB.3.51;Data Source="+szOpen+"BiocaDatabase.mdb;";
#endif
hr = m_pConnection->Open((_bstr_t)szOpen,"","",adModeUnknown);
}
}
catch(_com_error e)//捕捉異常
{
CString temp;
if(m_bChinese) temp.Format("連接資料庫錯誤信息:%s",e.ErrorMessage());
else temp.Format("Connecting database failure:%s",e.ErrorMessage());
AfxMessageBox(temp);
m_pConnection = NULL;
}
//如果當前資料庫為空,則導入最近使用的資料庫
if(m_pConnection!=NULL)
{
CString szSQL = "SELECT * FROM TestData";
_RecordsetPtr pSearchRecordSet;
try
{
pSearchRecordSet.CreateInstance("ADODB.Recordset");
pSearchRecordSet->Open((_variant_t)szSQL,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
if(pSearchRecordSet->adoEOF)
{
if(!CopyFile(m_szDesFileName1,m_szDataMisDir+"\\BiocaDatabase.mdb",FALSE))
{
//判斷是否是裝完軟體後第一次運行
int RunNo=GetProfileInt("RunNo","No",0); //第一次運行
if(RunNo==0) WriteProfileInt("RunNo","No",1); //已經運行過了
else
{
if(m_bChinese) AfxMessageBox("導入資料庫失敗!");
else AfxMessageBox("Input database failure!");
}
}
}
}
catch(_com_error e)///捕捉異常
{
CString temp;
if(m_bChinese) temp.Format("導入資料庫出錯:%s",e.ErrorMessage());
else temp.Format("Input database failure:%s",e.ErrorMessage());
AfxMessageBox(temp);
}
} //if(!m_pConnection=NULL)
} //if(m_bDataBase)