分类: C/C++
2008-04-07 11:14:54
void CDlgClientSingalCPJG::OnOK()
{
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
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("写入成功!");
CDialog::OnOK();
}