您的位置:首页 > 其它

VC:用ADO方式实现把CListCtrl数据写成Excel文件

2007-07-17 14:22 633 查看
ADODB::_ConnectionPtr mcon;
mcon.CreateInstance("ADODB.Connection");

CString constr;

constr="Provider=Microsoft.Jet.OLEDB.4.0;/
Data Source=C://du-XIAO.xls;/
Extended Properties = Excel 8.0";

mcon->Open(_bstr_t(LPCTSTR(constr)),"","",ADODB::adModeUnknown);

// 创建表结构
int i;
LVCOLUMN columnData;
CString columnName;
int columnNum = 0;
CString strH , sSql;
CString strV;
CString tableName = "客户销量"; //SHEET

sSql = "";
strH = "";
columnData.mask = LVCF_TEXT;
columnData.cchTextMax =100;
columnData.pszText = columnName.GetBuffer (100);
for(i=0;m_list.GetColumn(i,&columnData);i++)
{
if (i!=0)
{
sSql = sSql + ", " ;
strH = strH + ", " ;
}
sSql = sSql + " " + columnData.pszText +" TEXT";
strH = strH + " " + columnData.pszText +" ";
}
columnName.ReleaseBuffer ();
columnNum = i;

sSql = "CREATE TABLE " + tableName + " ( " + sSql + " ) ";
mcon->Execute(_bstr_t(LPCTSTR(sSql)), NULL, ADODB::adCmdText);

// 插入数据项
int nItemIndex;
for (nItemIndex=0; nItemIndex < m_list.GetItemCount ();nItemIndex++){
strV = "";
for(i=0;i<columnNum;i++)
{
if (i!=0)
{
strV = strV + ", " ;
}
strV = strV + " '" + m_list.GetItemText(nItemIndex,i) +"' ";
}

sSql = "INSERT INTO "+ tableName
+" ("+ strH + ")"
+" VALUES("+ strV + ")";
mcon->Execute(_bstr_t(LPCTSTR(sSql)), NULL, ADODB::adCmdText);

}

mcon->Close();

AfxMessageBox("写入成功!");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: