(高分)在MFC工程中用ADO方法连接Access数据库,如何实现将查询的记录集导入Excel文件中?

恳请大侠给出代码,强调下,是ADO方法,不是ODBC
连接上数据库没问题,关键是如何把查询的记录集导入Excel

给你一段代码,我一个程序中用到的通过ADO将记录写入EXCEL并保存。
你自己挑挑里面有用的部分。应该能看懂吧。
UpdateData(TRUE);
SetDlgItemText(IDC_STATIC11, "条件选择");
if(m_list2.GetCurSel()==-1||m_list4.GetCurSel()==-1||m_list5.GetCurSel()==-1)
{
AfxMessageBox("请将条件选择完整!");
return;
}
if(m_check1)
{
if(m_list6.GetCurSel()==-1)
{
AfxMessageBox("请选择B表中某列作为拷贝源!");
return;
}
}
_ConnectionPtr m_pConnection1 = NULL;
_ConnectionPtr m_pConnection2 = NULL;
_RecordsetPtr m_pRecordset = NULL;
// CString m_SinFile;
// CString m_TableName;
long lFiledCount1 = 0;
long lFiledCount2 = 0;
long lRowCount1 = 0; // 表的记录数目,不包括表格的表头行
long lRowCount2 = 0; // 表的记录数目,不包括表格的表头行
long lRowIndex2 = 0;

CoInitialize(NULL);

m_pConnection1.CreateInstance("ADODB.Connection");
m_pConnection2.CreateInstance("ADODB.Connection");
m_pRecordset.CreateInstance("ADODB.Connection");

CString strText1;
m_list1.GetLBText(m_list1.GetCurSel(),strText1);
CString SQLstr1;
SQLstr1.Format("SELECT * FROM [%s$]",strText1);

CString strText2;
m_list2.GetLBText(m_list2.GetCurSel(),strText2);
CString SQLstr2;
SQLstr2.Format("SELECT * FROM [%s$]",strText2);

try
{

SetDlgItemText(IDC_STATIC12, "正在标记,耐心等候...");
//打开excel文件
m_pConnection1->Open((_bstr_t)path1,"","",adModeUnknown);
m_pConnection2->Open((_bstr_t)path2,"","",adModeUnknown);

_RecordsetPtr pRecordset1;
_RecordsetPtr pRecordset2;
pRecordset1.CreateInstance (__uuidof(Recordset));
pRecordset2.CreateInstance (__uuidof(Recordset));

try
{
// [Feature$]代表Excel表格的某个工作表的名称
pRecordset1->Open(_variant_t(SQLstr1), // 查询DemoTable表中所有字段
m_pConnection1.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenStatic,//adOpenDynamic,adOpenKeyset
adLockOptimistic,
adCmdText);
pRecordset2->Open(_variant_t(SQLstr2), // 查询DemoTable表中所有字段
m_pConnection2.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenStatic,//adOpenDynamic,adOpenKeyset
adLockOptimistic,
adCmdText);
CString lb3; //A表某工作簿子集的全部子字段标题
CString lb4; //B表某工作簿子集的全部子字段标题
CString lb5;
CString lb6;
int p1=0; //A表需对比列的索引位置
int p2=0; //B表需对比列的索引位置
int p3=0;
m_list3.GetLBText(m_list3.GetCurSel(),lb3);
m_list4.GetLBText(m_list4.GetCurSel(),lb4);
m_list5.GetLBText(m_list5.GetCurSel(),lb5);
m_list6.GetLBText(m_list5.GetCurSel(),lb6);
//得到字段数目
lFiledCount1 = pRecordset1->GetFields()->GetCount();
lFiledCount2 = pRecordset2->GetFields()->GetCount();
//得到记录条数
lRowCount1 = pRecordset1->GetRecordCount();
lRowCount2 = pRecordset2->GetRecordCount();

//获取字段名
_bstr_t *filedName1 = new _bstr_t[lFiledCount1]; // 存储字段名
_bstr_t *filedName2 = new _bstr_t[lFiledCount2]; // 存储字段名
for (int filedIndex1 = 0;filedIndex1 < lFiledCount1;filedIndex1++)
{
filedName1[filedIndex1] = pRecordset1->GetFields()->GetItem(_variant_t(long(filedIndex1)))->GetName();
if(lb3==(LPCSTR)filedName1[filedIndex1])
{
p1=filedIndex1;
}
if(lb6==(LPCSTR)filedName1[filedIndex1])
{
p3=filedIndex1;
}
}

// FieldsPtr fds=pRecordset1->GetFields();
// fds->Append("S_id",adTinyInt,NULL,adFldRowID);

for (int filedIndex2 = 0;filedIndex2 < lFiledCount2;filedIndex2++)
{
filedName2[filedIndex2] = pRecordset2->GetFields()->GetItem(_variant_t(long(filedIndex2)))->GetName();
if(lb4==(LPCSTR)filedName2[filedIndex2])
{
p2=filedIndex2;
}
}

long lRowIndex1 = 0;
CString str1; // 存储表格中的所有数据
CString str2; // 存储表格中的所有数据

int flag=m_list5.FindString(-1,m_liststr5);

m_progress1.SetRange(0,lRowCount1);
pRecordset1->MoveFirst();
while(!pRecordset1->adoEOF)
{

// 按行,然后对每条数据的各个字段进行存储
str1 = VariantToString(pRecordset1->GetFields()->GetItem(_variant_t(filedName1[p1]))->Value);

pRecordset2->MoveFirst();
while(!pRecordset2->adoEOF)
{

// 按行,然后对每条数据的各个字段进行存储
str2 = VariantToString(pRecordset2->GetFields()->GetItem(_variant_t(filedName1[p2]))->Value);

if(str1==str2)
{
_variant_t t = _variant_t(long(flag));
pRecordset1->PutCollect(&t,_variant_t(m_eFlagStr));
lRowIndex2++;
pRecordset1->Update();
}

pRecordset2->MoveNext();
}
pRecordset1->MoveFirst();
lRowIndex1++;
m_progress1.SetPos(lRowIndex1);
pRecordset1->Move(lRowIndex1);

}

//pMyCom->Release();

pRecordset1->Close();
pRecordset1.Release();
pRecordset1 = NULL;
pRecordset2->Close();
pRecordset2.Release();
pRecordset2 = NULL;
m_pConnection1->Close();
m_pConnection2->Close();
if (filedName1 != NULL)
{
delete []filedName1;
filedName1 = NULL;
}
if (filedName2 != NULL)
{
delete []filedName2;
filedName2 = NULL;
}
}
catch(_com_error e)
{
EndWaitCursor();
AfxMessageBox(e.Description());
SetDlgItemText(IDC_STATIC12, "处理失败!");
}
}
catch(_com_error e)
{
AfxMessageBox(e.Description());

EndWaitCursor();
return;
SetDlgItemText(IDC_STATIC12, "处理失败!");
}
CString num;
num.Format("完成标记%d条记录!",lRowIndex2);
SetDlgItemText(IDC_STATIC12, num);
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-02-28
excel.applation
工作簿是excel.workbook wkbk = excelapp.add
工作表 excel.worksheet sht = wkbk.worksheet(1)
单元格的内容为 sht.cells(row,col).value = "你要的内容 "
row,col从1开始,表示工作表中的单元格。
如果你要导出复杂的内容,你可以试用excel模板 ,excelapp.add(模板路径)

还有一种ado方法,是对应带标题行的数据,用ado连接,连接句可以参考ado控件的connectstring向导
查询用 select * from [表名]
别用 order之类的,不稳定。本回答被提问者采纳
第2个回答  2010-02-13
vc不会,VB我会,导入数据库是引用 excel,然后建 一个变量 excelapp = new excel.applation
工作簿是excel.workbook wkbk = excelapp.add
工作表 excel.worksheet sht = wkbk.worksheet(1)
单元格的内容为 sht.cells(row,col).value = "你要的内容 "
row,col从1开始,表示工作表中的单元格。
如果你要导出复杂的内容,你可以试用excel模板 ,excelapp.add(模板路径)

还有一种ado方法,是对应带标题行的数据,用ado连接,连接句可以参考ado控件的connectstring向导
查询用 select * from [表名]
别用 order之类的,不稳定。

再还有方法就是简单的导出csv,要求你导出的内容中没有“,”不然会变到下一格
就是从标题到内容用 , 回车 连成串,把这个串写入文件
第3个回答  2010-02-17
用csv吧,也是用Excel打开的
第4个回答  2010-02-13
我这有一些VC连接数据库的电子书,你可以参考一下,书上的代码我没有,你可以上迅雷或CSDN找找代码参考一下,至于导入Excel的代码,这点我也不会
相似回答