vc2005怎么操作Excel2007

cau228charm 2009-10-14 09:14:42
大家有没有什么好的方法,在网上查了一些方法,大都是vc6.0对低版本Excel的操作,试了几次没有成功;还有一个问题,是不是必须建立mfc项目才能操作Excel
...全文
941 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
jasonM2008 2009-10-14
  • 打赏
  • 举报
回复
调试一下嘛,或者用try catch 一下!看看是什么异常!
cau228charm 2009-10-14
  • 打赏
  • 举报
回复
我做了 但是每当执行到 //打开excel文件
m_pConnection->Open((_bstr_t)adoinfo,"","",adModeUnknown);这个语句的时候就发生异常了跳出了,不知道怎么回事???
MoXiaoRab 2009-10-14
  • 打赏
  • 举报
回复
_RecordsetPtr m_pRecordset;
MoXiaoRab 2009-10-14
  • 打赏
  • 举报
回复
_ConnectionPtr m_pConnection;
cau228charm 2009-10-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 oyljerry 的回复:]
VC2008 打开读取excel2003和excel2007文件
http://hi.baidu.com/onlywater/blog/item/2e708c2391c4914cad34deac.html
[/Quote]
m_pConnection.CreateInstance("ADODB.Connection");
m_pRecordset.CreateInstance("ADODB.Connection");
上面这两个自变量怎么定义呢?
MoXiaoRab 2009-10-14
  • 打赏
  • 举报
回复
一般都是通过ADO操作Excel文档的。
#7给出的资料是不错的

如果ADO觉得不爽,可以用ODBC,移植性差点,但是更具有通用性
pady_pady 2009-10-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 oyljerry 的回复:]
VC2008 打开读取excel2003和excel2007文件
http://hi.baidu.com/onlywater/blog/item/2e708c2391c4914cad34deac.html
[/Quote]
用ado当数据库处理excel是一种最简洁的方法了
cau228charm 2009-10-14
  • 打赏
  • 举报
回复
谢谢 能不能提供一个比较完整的说明和流程呢。。。。
oyljerry 2009-10-14
  • 打赏
  • 举报
回复
VC2008 打开读取excel2003和excel2007文件
http://hi.baidu.com/onlywater/blog/item/2e708c2391c4914cad34deac.html
jyh_baoding 2009-10-14
  • 打赏
  • 举报
回复
看一些微软的说明,可能升级后有不同
fishion 2009-10-14
  • 打赏
  • 举报
回复
class view 中右键 选择添加 新类 ,再选择ActiveX 控件中的MFC类,在ActiveX 控件添加类向导下的“从以下资料添加类”中选择“文件”,再选择你的文件
cau228charm 2009-10-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 pady_pady 的回复:]
不是啊,只需导入ole和com环境,再import mso11.dll 和 excel.exe就可以了。
[/Quote]

“导入ole和com环境,再import mso11.dll 和 excel.exe”具体怎么做呢?
cau228charm 2009-10-14
  • 打赏
  • 举报
回复
能说具体一点吗 我有的时候vc2005
pady_pady 2009-10-14
  • 打赏
  • 举报
回复
好像是mso12.dll
pady_pady 2009-10-14
  • 打赏
  • 举报
回复
不是啊,只需导入ole和com环境,再import mso11.dll 和 excel.exe就可以了。
cau228charm 2009-10-14
  • 打赏
  • 举报
回复
VC2005利用ADO读Excel2007文件示例
1. 建立基于对话框的MFC程序
2. 在“stdafx.h”文件的末尾处加上下面的语句(注意:路径需要根据具体的目录设置)
#import "C:\Program Files\Common Files\System\ado\msado27.tlb" no_namespace rename("EOF", "adoEOF")
或者
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
3. 在对话框中的“确定”按钮上双击,响应事件OnBnClickedOk()
4. 在事件OnBnClickedOk()中写代码,代码如下:

void CExcelTestADODlg::OnBnClickedOk()
{
_ConnectionPtr m_pConnection = NULL;
_RecordsetPtr m_pRecordset = NULL;
CString m_SinFile;
CString m_TableName;
long lFiledCount = 0;
long lRowCount = 0; // 表的记录数目,不包括表格的表头行

CoInitialize(NULL);

CFileDialog dlg(TRUE,_T(".xls"),NULL,OFN_OVERWRITEPROMPT,_T("Excel2003 Files (*.xls)|*.xls|Excel2007 Files (*.xlsx)|*.xlsx||"),NULL,NULL);
if(dlg.DoModal()==IDOK)
{
BeginWaitCursor();
CString FileExt=dlg.GetFileExt();
m_SinFile=dlg.GetPathName();

m_pConnection.CreateInstance("ADODB.Connection");
m_pRecordset.CreateInstance("ADODB.Connection");


CString adoinfo;
if(FileExt=="xlsx")
adoinfo.Format(_T("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=%s;Extended Properties=Excel 12.0"),m_SinFile);
else
adoinfo.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Extended Properties=Excel 8.0"),m_SinFile);



try
{

//打开excel文件
m_pConnection->Open((_bstr_t)adoinfo,"","",adModeUnknown);

////读取表名
//_RecordsetPtr pRstSchema = NULL;
//pRstSchema = m_pConnection->OpenSchema(adSchemaTables);
//_bstr_t table_name;

//// 循环读取excel文件中的所有表的名字
//while(!pRstSchema->adoEOF)
//{
// table_name = pRstSchema->Fields->GetItem("TABLE_NAME")->Value;
// m_TableName=(LPCSTR)table_name;
// pRstSchema->MoveNext();
//}


//pRstSchema->Close();
//pRstSchema.Release();
//pRstSchema=NULL;
////打开表
//char conntchar[150];
//sprintf(conntchar,"SELECT * FROM [yueanzhi$]",m_TableName);//(LPCSTR)m_TableName.GetBuffer()
//_bstr_t sqltext(conntchar);

_RecordsetPtr pRecordset;
pRecordset.CreateInstance (__uuidof(Recordset));

try
{
// [Feature$]代表Excel表格的某个工作表的名称
pRecordset->Open("SELECT * FROM [Feature$]", // 查询DemoTable表中所有字段
m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenStatic,//adOpenDynamic,adOpenKeyset
adLockOptimistic,
adCmdText);

//得到字段数目
lFiledCount = pRecordset->GetFields()->GetCount();
//得到记录条数
lRowCount = pRecordset->GetRecordCount();

//获取字段名
_bstr_t *filedName = new _bstr_t[lFiledCount]; // 存储字段名
for (int filedIndex = 0;filedIndex < lFiledCount;filedIndex++)
{
filedName[filedIndex] = pRecordset->GetFields()->GetItem(_variant_t(long(filedIndex)))->GetName();
}

//Suffiled=pRecordset->GetFields()->GetItem(_variant_t(long(0)))->GetName();//GetItem(_variant_t(0));
//XCorfiled=pRecordset->GetFields()->GetItem(_variant_t(long(1)))->GetName();
//YCorfiled=pRecordset->GetFields()->GetItem(_variant_t(long(2)))->GetName();
//Elevfiled=pRecordset->GetFields()->GetItem(_variant_t(long(3)))->GetName();

long lRowIndex = 0;
float *fFeatureData = new float[lFiledCount*lRowCount]; // 存储表格中的所有数据
pRecordset->MoveFirst();
while(!pRecordset->adoEOF)
{
for (int filedIndex = 0;filedIndex < lFiledCount;filedIndex++)
{
// 按行,然后对每条数据的各个字段进行存储
fFeatureData[lRowIndex*lFiledCount+filedIndex] = pRecordset->GetFields()->GetItem(_variant_t(filedName[filedIndex]))->Value;
}
//revNum=pRecordset->GetFields()->GetItem(_variant_t(Suffiled))->Value;
//x=pRecordset->GetFields()->GetItem(_variant_t(XCorfiled))->Value;
//y=pRecordset->GetFields()->GetItem(_variant_t(YCorfiled))->Value;
//Elev=pRecordset->GetFields()->GetItem(_variant_t(Elevfiled))->Value;

pRecordset->MoveNext();
lRowIndex++;
}

pRecordset->Close();
pRecordset.Release();
pRecordset = NULL;
if (filedName != NULL)
{
delete []filedName;
filedName = NULL;
}
if (fFeatureData != NULL)
{
delete []fFeatureData;
fFeatureData = NULL;
}

}
catch(_com_error e)
{
EndWaitCursor();
AfxMessageBox(e.Description());
}
}
catch(_com_error e)
{
AfxMessageBox(e.Description());

EndWaitCursor();
return;
}
}

}

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧