项目中,做了这样一个小工具,有兴趣的看看!

 

1、Excel --->>> ListCtrl --->>> SQL 2000

2、遍历父窗口ListCtrl中指定“失败”的行,插入到子窗口中新的ListCtrl中,最后再生成导入错误的Excel报表

 

    (这样让用户可以自行修改错误的资料,多次导入)

 

    看了代码,你会觉得方法确实有些“猥琐”......

 

    在重绘ListCtrl着色错误列时,查了许久资料,希望友友不要再走“弯路”,代码写得粗糙,希望大牛们不要见保持“蛋定”......

 

相关代码:

 

1、指定行着色

/************************************************************************/ /* 函数说明: 重绘ListCtrl控件 —— 红色显示失败的记录 /* 参 数: 参数MSDN /* 返 回 值: 无 /* 备 注: 无 /************************************************************************/ void CImportDlg::OnCustomdrawMyList( NMHDR* pNMHDR, LRESULT* pResult ) { NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR ); // Take the default processing unless we set this to something else below. *pResult = 0; // First thing - check the draw stage. If it's the control's prepaint // stage, then tell Windows we want messages for every item. if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage ) { *pResult = CDRF_NOTIFYITEMDRAW; } // This is the notification message for an item. We'll request // notifications before each subitem's prepaint stage. else if ( pLVCD->nmcd.dwDrawStage==CDDS_ITEMPREPAINT ) { COLORREF m_clrText; int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec); // 根据文本内容判断使ListCtrl不同颜色现实的条件 CString str = m_List1.GetItemText(nItem ,6); if (str == "成功") { m_clrText = RGB(12,26,234); } else if (str == "") { m_clrText = RGB(0,0,0); } else { m_clrText = RGB(255, 0, 0); } pLVCD->clrText = m_clrText; *pResult = CDRF_DODEFAULT; } }

 

2、浏览Excel - 导入Excel数据

/************************************************************************/ /* 函数说明: 浏览Excel文件 /* 参 数: 无 /* 返 回 值: 无 /* 备 注: 无 /************************************************************************/ void CImportDlg::OnSysChangefile() { // TODO: Add your command handler code here m_List1.DeleteAllItems(); CString strFile; char szFilter[]="Excel 文件(*.xls)|*.xls"; CFileDialog dlg(1,NULL,NULL,NULL,szFilter); if( IDOK == dlg.DoModal()) { strFile = dlg.GetPathName(); } CSpreadSheet SS(strFile, "Sheet1"); CStringArray Rows, Column; CString str = ""; int nItem = 0; char szYW[100]; memset(szYW,0,sizeof(szYW)); COPini::ReadString("历史","LastUser",szYW,"SetInfo.ini"); str.Format("%s",szYW); CString str1,str2,str3,str4,str5; // 插入数据库 for (int i = 2; i <= SS.GetTotalRows(); i++) { SS.ReadRow(Rows, i); // 读取一行 str1 = Rows.GetAt(0); str2 = Rows.GetAt(1); str3 = Rows.GetAt(2); str4 = Rows.GetAt(3); str5 = Rows.GetAt(4); str1.Replace(".0",""); // 避免识别为字数 str2.Replace(".0",""); // 客户电话 str3.Replace(".0",""); // 客户传真 str4.Replace(".0",""); str5.Replace(".0",""); if(str1.Find("个人",0) >=1 ) // 如果客户为“个人” { // 如果收件人不为“先生或小姐”则替换客户名称 if( (str4.Find("先生",0) <= 0) && (str4.Find("小姐",0) <= 0)) { str1 = str4; } } nItem = m_List1.InsertItem(0,str); m_List1.SetItem(nItem,1,1,str1,NULL,0,0,0); m_List1.SetItem(nItem,2,1,str2,NULL,0,0,0); m_List1.SetItem(nItem,3,1,str3,NULL,0,0,0); m_List1.SetItem(nItem,4,1,str4,NULL,0,0,0); m_List1.SetItem(nItem,5,1,str5,NULL,0,0,0); Invalidate(TRUE); } }

 

3、遍历ListCtrl,插入SQL

/************************************************************************/ /* 函数说明: 遍历ListCtrl,向数据库中写入记录 /* 参 数: 无 /* 返 回 值: 无 /* 备 注: 无 /************************************************************************/ void CImportDlg::OnSysStart() { // TODO: Add your command handler code here if(m_List1.GetItemCount() <=0 ) { MessageBox("没有任何数据需要导入!","提示",MB_OK | MB_ICONWARNING); return; } if( IDYES != MessageBox("确定开始导入吗?","提示",MB_YESNO | MB_ICONWARNING)) { return; } int nRet = 0; int nCount = m_List1.GetItemCount()-1; char szYeWu[100],szKeHu[100],szDian[100]; char szChua[100],szShou[100],szAddr[100]; CString str1,str2,str3,str4,str5,str6; for(int j=0;j<=nCount;j++) // 开始遍历ListCtrl { m_List1.GetItemText(j,0,szYeWu,100); // 获取业务员 m_List1.GetItemText(j,1,szKeHu,100); // 获取客户名称 m_List1.GetItemText(j,2,szDian,100); // 获取客户电话 m_List1.GetItemText(j,3,szChua,100); // 获取客户传真 m_List1.GetItemText(j,4,szShou,100); // 获取收件人 m_List1.GetItemText(j,5,szAddr,100); // 获取客户地址 str1.Format("%s",szYeWu); str2.Format("%s",szKeHu); str3.Format("%s",szDian); str4.Format("%s",szChua); str5.Format("%s",szShou); str6.Format("%s",szAddr); nRet = InsertKeHu(str1,str2,str3,str4,str5,str6); if(nRet == 2) { m_List1.SetItemText(j,6,"成功"); m_List1.SetItemText(j,7,""); } else if (nRet == 0) { m_List1.SetItemText(j,6,"失败"); m_List1.SetItemText(j,7,"客户名称为空"); nErrorCount++; } else if(nRet == 1) { m_List1.SetItemText(j,6,"失败"); m_List1.SetItemText(j,7,"客户名称不能为先生、小姐、个人"); nErrorCount++; } else if(nRet == 3) { m_List1.SetItemText(j,6,"失败"); m_List1.SetItemText(j,7,m_strLastError); nErrorCount++; } Invalidate(TRUE); } }

 

 

4、截图如下:

导入Excel报表到ListCtrl再插入SQL (客户信息导入工具)_null

 

VC++源码下载地址:

 

http://www.rayfile.com/files/4c86cd6b-3f3e-11df-9bad-0015c55db73d/