社区
VC.NET
帖子详情
vc 怎样通过 ado 向 EXCEL 中增加新的列
zhangyongjun001
2012-09-09 03:38:04
我用vc 通过 ado 方式能够向 EXCEL 中增加新的行,
m_pRecordset->AddNew();//添加一个新行
我想知道怎样才能增加一个新的列呢?
请高手指点。
...全文
218
4
打赏
收藏
vc 怎样通过 ado 向 EXCEL 中增加新的列
我用vc 通过 ado 方式能够向 EXCEL 中增加新的行, m_pRecordset->AddNew();//添加一个新行 我想知道怎样才能增加一个新的列呢? 请高手指点。
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用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
根据MSDN官方说,使用
ADO
读取
Excel
存在不少问题,比如一
列
中
的单元格的类型是多种的话,有些单元格的值明明有值,读取确实空,这主要是
Excel
不是关系型数据库...如果想准确无误的操作
Excel
,请使用其他方法,例如...
VC
2005+
ADO
操作
Excel
文件
与从
Excel
.exe导出类库相比,更方便,但是有局限性,当表格
中
同一
列
包含不同类型数据时,则读取数据可能出错,这时需要在连接字符串
中
将IMEX值设为1(导入模式),把所有数据都当作TEXT类型来处理,但是这样一来将只能...
用
ado
连接
excel
数据库
本文将详细讨论如何在Visual C++(
VC
)环境
中
使用ActiveX Data Objects (
ADO
)技术来连接并查询
Excel
数据库。 首先,
ADO
是Microsoft提供的一个接口,它允许开发者访问多种数据源,包括但不限于SQL Server、Access和...
ado
连接
excel
###
ADO
连接
Excel
的实现方法 在开发过程
中
,我们常常需要处理
Excel
文件
中
的数据。ActiveX Data Objects (
ADO
) 是一种广泛使用的数据访问技术,它可以用来连接各种数据源,包括
Excel
。本文将详细介绍如何使用
ADO
...
vc
6.0操作.XLS(
excel
)
在本文
中
,我们将深入探讨如何在Visual C++ 6.0(简称
VC
6.0)环境
中
,使用MFC(Microsoft Foundation Classes)库来操作
Excel
文件,特别是读取和写入.XLS格式的数据。这个示例代码展示了一个对话框应用程序,其
中
...
VC.NET
7,539
社区成员
27,671
社区内容
发帖
与我相关
我的任务
VC.NET
.NET技术 VC.NET
复制链接
扫一扫
分享
社区描述
.NET技术 VC.NET
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章