社区
VC.NET
帖子详情
vc 怎样通过 ado 向 EXCEL 中增加新的列
zhangyongjun001
2012-09-09 03:38:04
我用vc 通过 ado 方式能够向 EXCEL 中增加新的行,
m_pRecordset->AddNew();//添加一个新行
我想知道怎样才能增加一个新的列呢?
请高手指点。
...全文
206
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
根据MSDN官方说,使用
ADO
读取
Excel
存在不少问题,比如一
列
中
的单元格的类型是多种的话,有些单元格的值明明有值,读取确实空,这主要是
Excel
不是关系型数据库...如果想准确无误的操作
Excel
,请使用其他方法,例如...
VC
2005+
ADO
操作
Excel
文件
与从
Excel
.exe导出类库相比,更方便,但是有局限性,当表格
中
同一
列
包含不同类型数据时,则读取数据可能出错,这时需要在连接字符串
中
将IMEX值设为1(导入模式),把所有数据都当作TEXT类型来处理,但是这样一来将只能...
用
VC
#.NET开发交互式CAD系统
用
VC
#.NET开发交互式CAD系统,包括结合GDI+和OPENGL两种方案。 联系平台客服可开发票。
vc
++ 应用源码包_6
列
出所有的显示模式并
列
表出来,通过单击
列
表来改变显示分辨率。 iconbutton_demo.zip iconbutton_src.zip 创建一个按钮,并在上面显示图标。 jpeglib_demo.zip jpeglib_src.zip 利用Delphi的代码在
VC
中
显示JPG...
ACReport
中
国式报表控件2011(Ver2.3)
12、Delphi的接口源码
中
增加
了LoadFromCommonBlobField和SaveToCommonBlobField函数,可以直接从一个非
ADO
的Dataset
中
装入或保存模板文件。 13、函数优化,在同一页
中
如果两个或多个公式有公共子公式,则此公共子...
VC.NET
7,540
社区成员
27,673
社区内容
发帖
与我相关
我的任务
VC.NET
.NET技术 VC.NET
复制链接
扫一扫
分享
社区描述
.NET技术 VC.NET
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章