VC++(MFC)如何从对话框写数据到Excel

VC++(MFC)如何从对话框写数据到Excel,第1张

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.0Data Source="+szOpen+"BiocaDatabase.mdb"

#else

szOpen = "Provider=Microsoft.Jet.OLEDB.3.51Data 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)

String sql = "insert into 表名(字段1,字段2) values( '" + 字段1对应的值+ "' , '" + 字段2对应的值 + "')"

String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0Data Source=|DataDirectory|\\data.mdb"

OleDbConnection connection = new OleDbConnection(connectionString)

OleDbCommand cmd = new OleDbCommand(sql,connection)

connection.Open()

if (cmd.ExecuteNonQuery() >0)//判断插入数据是否成功 { //执行要 *** 作的语句 }


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9898345.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存