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)