当前位置:编程学习 > C#/ASP.NET >>

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

我用vc 通过 ado 方式能够向 EXCEL 中增加新的行,
m_pRecordset->AddNew();//添加一个新行
我想知道怎样才能增加一个新的列呢?
请高手指点。 --------------------编程问答-------------------- 怎么没有人回答,难道没人知道吗。请高手帮帮忙吧,这个问题已经困扰我很久了,我实在解决不了。 --------------------编程问答-------------------- 我在往上搜了一下,有人说可以用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;
}
程序运行后提示,“此环境不允许此操作”,请大侠们帮帮忙吧! --------------------编程问答-------------------- 求求高手帮忙呀! --------------------编程问答-------------------- 楼主此问题解决了否?小弟也遇到此问题,求指教!
补充:.NET技术 ,  VC.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,