社区
VC.NET
帖子详情
vc 怎样通过 ado 向 EXCEL 中增加新的列
zhangyongjun001
2012-09-09 03:38:04
我用vc 通过 ado 方式能够向 EXCEL 中增加新的行,
m_pRecordset->AddNew();//添加一个新行
我想知道怎样才能增加一个新的列呢?
请高手指点。
...全文
252
4
打赏
收藏
vc 怎样通过 ado 向 EXCEL 中增加新的列
我用vc 通过 ado 方式能够向 EXCEL 中增加新的行, m_pRecordset->AddNew();//添加一个新行 我想知道怎样才能增加一个新的列呢? 请高手指点。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
4 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
lmj1988_perfect
2013-05-18
打赏
举报
回复
楼主此问题解决了否?小弟也遇到此问题,求指教!
zhangyongjun001
2012-09-25
打赏
举报
回复
求求高手帮忙呀!
zhangyongjun001
2012-09-13
打赏
举报
回复
我在往上搜了一下,有人说可以用sql 语句添加字段:
Alter TABLE [tablename] ADD COLUMN [col_TT] text(20);
我试了一下,代码如下:
bool CtestDoc::Read_Excel_Data( )
{
///////////////得到程序文件所在路径////////////
CString file_name = ((CtestApp*)AfxGetApp())->m_Path + _T("\\fenbu.xls");
CoInitialize(NULL);
CString strConnect = _T("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=0\";Data Source=");//连接字串
strConnect += file_name;
try
{
//Open the connection
if(FAILED(m_pConnection.CreateInstance(__uuidof(Connection))))//建立连接
{
AfxMessageBox(_T("无法建立连接"));
return false;
}
if(FAILED(m_pConnection->Open(_bstr_t(LPCTSTR(strConnect)), _T(""), _T(""), adModeUnknown)))//打开连接
{
AfxMessageBox(_T("无法打开连接"));
return false;
}
_RecordsetPtr table = m_pConnection->OpenSchema(adSchemaTables);//定义表
CString strTblName;
CString sql;
strTblName = (LPCTSTR)(_bstr_t)table->Fields->GetItem(_T("TABLE_NAME"))->Value;
strTblName.Trim(_T('\''));
_variant_t RecordsAffected;
m_pConnection->Execute(_bstr_t(LPCTSTR(_T("Alter TABLE [") + strTblName + _T("] ADD COLUMN [TT] text(20)"))),&RecordsAffected,adCmdText);
m_pRecordset.CreateInstance(__uuidof(Recordset)); if (m_pRecordset[i] == NULL)
{
AfxMessageBox(_T("建立数据集失败!"));
return false;
}
sql.Format(_T("select * from [%s]"), strTblName);
if (!SUCCEEDED(m_pRecordset[i]->Open(_variant_t(sql),
m_pConnection.GetInterfacePtr(),
adOpenStatic,
adLockPessimistic,//adLockOptimistic,
adCmdText)))
{
AfxMessageBox(_T("无法打开记录集!"));
return false;
}
table->Close();
}
catch(_com_error &e)
{
AfxMessageBox((LPTSTR)e.Description());
return false;
}
return true;
}
可是执行后,程序提示“此操作无效”,不知错在哪里。说明一下,如果去掉添加字段语句,程序可以正常执行,并能对其中的记录数据进行读、写操作。
有人说可以通过fileds->Append 添加字段,我也进行了试验代码如下:
bool CtestDoc::Read_Excel_Data( )
{
///////////////得到程序文件所在路径////////////
CString file_name = ((CtestApp*)AfxGetApp())->m_Path + _T("\\fenbu.xls");
CoInitialize(NULL);
CString strConnect = _T("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=0\";Data Source=");//连接字串
strConnect += file_name;
try
{
//Open the connection
if(FAILED(m_pConnection.CreateInstance(__uuidof(Connection))))//建立连接
{
AfxMessageBox(_T("无法建立连接"));
return false;
}
if(FAILED(m_pConnection->Open(_bstr_t(LPCTSTR(strConnect)), _T(""), _T(""), adModeUnknown)))//打开连接
{
AfxMessageBox(_T("无法打开连接"));
return false;
}
_RecordsetPtr table = m_pConnection->OpenSchema(adSchemaTables);//定义表
///////////////////////////////////////////
CString strTblName;
CString sql;
strTblName = (LPCTSTR)(_bstr_t)table->Fields->GetItem(_T("TABLE_NAME"))->Value;
strTblName.Trim(_T('\''));
table->put_CursorLocation( adUseClient );
table->Fields->Append( "99" , adInteger ,10 ,adFldUpdatable);
///////////////////////////////////////////////////////////////////////////// m_pRecordset.CreateInstance(__uuidof(Recordset));
if (m_pRecordset == NULL)
{
AfxMessageBox(_T("建立数据集失败!"));
return false;
}
sql.Format(_T("select * from [%s]"), strTblName);
if (!SUCCEEDED(m_pRecordset[i]->Open(_variant_t(sql),
m_pConnection.GetInterfacePtr(),
adOpenStatic,
adLockPessimistic,//adLockOptimistic,
adCmdText)))
{
AfxMessageBox(_T("无法打开记录集!"));
return false;
}
table->Close();
}
catch(_com_error &e)
{
AfxMessageBox((LPTSTR)e.Description());
return false;
}
return true;
}
程序运行后提示,“此环境不允许此操作”,请大侠们帮帮忙吧!
zhangyongjun001
2012-09-11
打赏
举报
回复
怎么没有人回答,难道没人知道吗。请高手帮帮忙吧,这个问题已经困扰我很久了,我实在解决不了。
VC
利用
ADO
操作
Excel
本文介绍如何使用
ADO
连接
Excel
文件并进行基本的数据操作,包括读取数据、执行SQL查询、
增加
新
记录及更
新
单元格内容。
VC
6.0
ADO
读取
Excel
2003内容,并将读取的数据上传到远程服务器SQL2008表
中
这篇博客详细介绍了如何利用
VC
6.0
中
的
ADO
技术,读取
Excel
2003文件内容,并将这些数据上传到远程服务器上的SQL Server 2008数据库
中
。通过创建Dispatch,打开
Excel
文件,连接
Excel
工作簿和工作表,然后读取数据,最后使用
ADO
连接SQL Server并将数据插入指定表。
利用
ADO
操作
Excel
文件
本文介绍如何使用
ADO
对象在
VC
环境
中
操作
Excel
文件,包括连接、读取、插入和更
新
数据的具体步骤及注意事项。
VS2010通过
ADO
连接
EXCEL
数据库
本文介绍了一种简单的方法在VS2010
中
使用MFC通过
ADO
连接并读取
EXCEL
文件。首先,需要在头文件
中
导入ms
ado
15.dll并初始化COM组件。接着,声明并初始化
ADO
的_ConnectionPtr和_RecordsetPtr智能指针,用于打开数据库连接和操作数据。通过打开本地数据库并执行查询,可以从
EXCEL
文件
中
读取数据。注意,连接字符串
中
的参数如Provider、Data Source、Extended Properties等对连接的成功至关重要。
在 VS2008 下操作
Excel
的方法总结
本文总结了在
VC
环境下操作
Excel
的不同方法,包括OLE自动化、
ADO
/ODBC、第三方库及开源库等,并重点推荐Basic
Excel
库,它支持直接解析
Excel
格式,提高了速度且不再依赖Office安装。
VC.NET
7,539
社区成员
27,670
社区内容
发帖
与我相关
我的任务
VC.NET
.NET技术 VC.NET
复制链接
扫一扫
分享
社区描述
.NET技术 VC.NET
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章