运行效果
代码
void CMFCApplication14Dlg::OnBnClickedButton1()
{
CString FileName = L"first.xls";
CString sDriver = L"MICROSOFT EXCEL DRIVER (*.XLS)"; // Excel安装驱动
CString FILEEXCEL;
FILEEXCEL.Format(L"DRIVER={%s};DSN='''';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s", sDriver, FileName, FileName);
CDatabase date;
CString ssql;
if (date.OpenEx(FILEEXCEL, CDatabase::noOdbcDialog))
{
date.ExecuteSQL(L"CREATE TABLE 记录表(FIR text,FIC text);");
//插入数据 // 如需遍历数据库插入请使用类似方法
for (int i = 0; i < 10; i++)
{
ssql.Format(L"INSERT INTO 记录表(FIR,FIC) VALUES('%d','%d');", i, i);
date.ExecuteSQL(ssql);
}
//关闭驱动
date.ExecuteSQL(ssql);
}
else
{
AfxMessageBox(L"没有Excel驱动");
}
AfxMessageBox(L"导出完成!");
}