123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- #include "StdAfx.h"
- #include "ExcelImpl.h"
- CString CExcelImpl::GetExcelDriver()
- {
- char szBuf[2001];
- WORD cbBufMax = 2000;
- WORD cbBufOut;
- char *pszBuf = szBuf;
- CString sDriver;
-
- if (!SQLGetInstalledDrivers(szBuf, cbBufMax, &cbBufOut))
- return "";
-
- do
- {
- if (strstr(pszBuf, "Excel") != 0)
- {
-
- sDriver = CString(pszBuf);
- break;
- }
- pszBuf = strchr(pszBuf, '\0') + 1;
- }
- while (pszBuf[1] != '\0');
- return sDriver;
- }
- BOOL CExcelImpl::MakeSurePathExists( CString &Path, bool FilenameIncluded)
- {
- int Pos=0;
- while((Pos=Path.Find('\\',Pos+1))!=-1)
- CreateDirectory(Path.Left(Pos),NULL);
- if(!FilenameIncluded)
- CreateDirectory(Path,NULL);
-
-
- return !_access(Path,0);
- }
- BOOL CExcelImpl::GetDefaultXlsFileName(CString& sExcelFile)
- {
-
- CString timeStr;
- CTime day;
- day=CTime::GetCurrentTime();
- int filenameday,filenamemonth,filenameyear,filehour,filemin,filesec;
- filenameday=day.GetDay();
- filenamemonth=day.GetMonth();
- filenameyear=day.GetYear();
- filehour=day.GetHour();
- filemin=day.GetMinute();
- filesec=day.GetSecond();
- timeStr.Format("%04d%02d%02d%02d%02d%02d",filenameyear,filenamemonth,filenameday,filehour,filemin,filesec);
- sExcelFile = timeStr + ".xls";
-
- CFileDialog dlgFile(FALSE,".xls",sExcelFile);
- CString title;
- CString strFilter;
- title = "指纹机数据";
- strFilter = "Excel文件(*.xls)";
- strFilter += (TCHAR)'\0';
- strFilter += _T("*.xls");
- strFilter += (TCHAR)'\0';
- dlgFile.m_ofn.nMaxCustFilter++;
- dlgFile.m_ofn.nFilterIndex = 1;
-
- CString allFilter;
- VERIFY(allFilter.LoadString(AFX_IDS_ALLFILTER));
- strFilter += allFilter;
- strFilter += (TCHAR)'\0';
- strFilter += _T("*.*");
- strFilter += (TCHAR)'\0';
- dlgFile.m_ofn.nMaxCustFilter++;
- dlgFile.m_ofn.lpstrFilter = strFilter;
- dlgFile.m_ofn.lpstrTitle = title;
- if (dlgFile.DoModal()==IDCANCEL)
- return FALSE;
- CString sn = dlgFile.GetFileTitle() ;
- CString strPath = dlgFile.GetPathName();
- sExcelFile = strPath;
- if (MakeSurePathExists(sExcelFile,true))
- {
- if(!DeleteFile(sExcelFile))
- {
- MessageBox(NULL, "覆盖文件时出错!", "提示", MB_ICONINFORMATION);
- return FALSE;
- }
- }
- return TRUE;
- }
- CExcelImpl::CExcelImpl(void)
- {
- }
- CExcelImpl::~CExcelImpl(void)
- {
- }
- CString CExcelImpl::ExportListToExcel(CListCtrl* pList, CString strTitle)
- {
- CString warningStr;
- CString sExcelFile;
- if (pList->GetItemCount()>0)
- {
- CDatabase database;
- CString sDriver;
- CString sSql;
- CString tableName = strTitle;
-
- sDriver = GetExcelDriver();
- if (sDriver.IsEmpty())
- {
-
- AfxMessageBox("没有安装Excel!");
- return "";
- }
-
- if (!GetDefaultXlsFileName(sExcelFile))
- {
- return "";
- }
-
- sSql.Format("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",sDriver, sExcelFile, sExcelFile);
-
- if( database.OpenEx(sSql,CDatabase::noOdbcDialog) )
- {
-
- int i;
- LVCOLUMN columnData;
- CString columnName;
- int columnNum = 0;
- CString strH;
- CString strV;
- sSql = "";
- strH = "";
- columnData.mask = LVCF_TEXT;
- columnData.cchTextMax =100;
- columnData.pszText = columnName.GetBuffer (100);
- for(i=0;pList->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 + " ) ";
- database.ExecuteSQL(sSql);
-
- int nItemIndex;
- for (nItemIndex=0;nItemIndex<pList->GetItemCount();nItemIndex++){
- strV = "";
- for(i=0;i<columnNum;i++)
- {
- if (i!=0)
- {
- strV = strV + ", " ;
- }
- strV = strV + " '" + pList->GetItemText(nItemIndex,i) +"' ";
- }
- sSql = "INSERT INTO "+ tableName
- +" ("+ strH + ")"
- +" VALUES("+ strV + ")";
- database.ExecuteSQL(sSql);
- }
- }
-
- database.Close();
- warningStr.Format("Excel文件成功保存至%s",sExcelFile);
- AfxMessageBox(warningStr);
- return sExcelFile;
- }
- return "";
- }
- void CExcelImpl::ReadExcelToList(CListCtrl* pList, CString strTitle,DWORD &dwListCount)
- {
-
- CStringArray Rows, Column;
- CSpreadSheet varSheet(strTitle, "LoginData");
-
- DWORD oldListCount = dwListCount;
- for (int i = 0; i <varSheet.GetTotalRows()-1; i++)
- {
-
- varSheet.ReadRow(Rows, i+2);
- CString strContents = "";
- pList->InsertItem(i+oldListCount, strContents) ;
- for (int j = 0; j < Rows.GetSize(); j++)
- {
- strContents = Rows.GetAt(j);
- pList->SetItemText(i+oldListCount,j,strContents);
- }
- dwListCount++;
- }
- }
|