vc 怎样通过 ado 向 EXCEL 中增加新的列

zhangyongjun001 2012-09-09 03:38:04
我用vc 通过 ado 方式能够向 EXCEL 中增加新的行,
m_pRecordset->AddNew();//添加一个新行
我想知道怎样才能增加一个新的列呢?
请高手指点。
...全文
206 4 打赏 收藏 转发到动态 举报
写回复
用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
  • 打赏
  • 举报
回复
怎么没有人回答,难道没人知道吗。请高手帮帮忙吧,这个问题已经困扰我很久了,我实在解决不了。

7,540

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 VC.NET
社区管理员
  • VC.NET社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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