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

来源:互联网 发布:时时彩个位计划数据 编辑:程序博客网 时间:2024/06/05 16:51
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("写入成功!"); 

原创粉丝点击