|
|
|
|
#include "StdAfx.h"
|
|
|
|
|
#include "ExcelMgr.h"
|
|
|
|
|
#include <odbcinst.h>
|
|
|
|
|
|
|
|
|
|
#include <io.h>
|
|
|
|
|
|
|
|
|
|
#define TABLE_NAME "table1"
|
|
|
|
|
CExcelMgr::CExcelMgr(void)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
CExcelMgr::~CExcelMgr(void)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// BOOL MakeSurePathExists( CString &Path,bool FilenameIncluded)
|
|
|
|
|
// <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// Path ·<><C2B7>
|
|
|
|
|
// FilenameIncluded ·<><C2B7><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
|
|
|
|
|
// <09><><EFBFBD><EFBFBD>ֵ:
|
|
|
|
|
// <09>ļ<EFBFBD><C4BC>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// ˵<><CBB5>:
|
|
|
|
|
// <09>ж<EFBFBD>Path<74>ļ<EFBFBD>(FilenameIncluded=true)<29>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD>ڷ<EFBFBD><DAB7><EFBFBD>TURE<52><45><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD><DAB7><EFBFBD>FALSE
|
|
|
|
|
// <09>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>Ŀ¼
|
|
|
|
|
//
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
BOOL CExcelMgr::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 ((!FilenameIncluded)?!_access(Path,0):
|
|
|
|
|
// !_access(Path.Left(Path.ReverseFind('\\')),0));
|
|
|
|
|
|
|
|
|
|
return !_access(Path,0);
|
|
|
|
|
}
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϵ<EFBFBD><CFB5>ļ<EFBFBD><C4BC><EFBFBD>
|
|
|
|
|
BOOL CExcelMgr::GetDefaultXlsFileName(CString &Path,CString& sExcelFile)
|
|
|
|
|
{
|
|
|
|
|
///Ĭ<><C4AC><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>yyyymmddhhmmss.xls
|
|
|
|
|
CString timeStr;
|
|
|
|
|
CTime day;
|
|
|
|
|
day=CTime::GetCurrentTime();
|
|
|
|
|
int filenameday,filenamemonth,filenameyear,filehour,filemin,filesec;
|
|
|
|
|
filenameday=day.GetDay();//dd
|
|
|
|
|
filenamemonth=day.GetMonth();//mm<6D>·<EFBFBD>
|
|
|
|
|
filenameyear=day.GetYear();//yyyy
|
|
|
|
|
filehour=day.GetHour();//hh
|
|
|
|
|
filemin=day.GetMinute();//mm<6D><6D><EFBFBD><EFBFBD>
|
|
|
|
|
filesec=day.GetSecond();//ss
|
|
|
|
|
timeStr.Format("%04d%02d%02d%02d%02d%02d",filenameyear,filenamemonth,filenameday,filehour,filemin,filesec);
|
|
|
|
|
|
|
|
|
|
sExcelFile = timeStr + ".xls";
|
|
|
|
|
// prompt the user (with all document templates)
|
|
|
|
|
CFileDialog dlgFile(FALSE,".xls",sExcelFile);
|
|
|
|
|
CString title;
|
|
|
|
|
CString strFilter;
|
|
|
|
|
|
|
|
|
|
title = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
|
|
|
|
|
strFilter = "Excel<EFBFBD>ļ<EFBFBD>(*.xls)";
|
|
|
|
|
strFilter += (TCHAR)'\0'; // next string please
|
|
|
|
|
strFilter += _T("*.xls");
|
|
|
|
|
strFilter += (TCHAR)'\0'; // last string
|
|
|
|
|
dlgFile.m_ofn.nMaxCustFilter++;
|
|
|
|
|
dlgFile.m_ofn.nFilterIndex = 1;
|
|
|
|
|
// append the "*.*" all files filter
|
|
|
|
|
CString allFilter;
|
|
|
|
|
VERIFY(allFilter.LoadString(AFX_IDS_ALLFILTER));
|
|
|
|
|
strFilter += allFilter;
|
|
|
|
|
strFilter += (TCHAR)'\0'; // next string please
|
|
|
|
|
strFilter += _T("*.*");
|
|
|
|
|
strFilter += (TCHAR)'\0'; // last string
|
|
|
|
|
dlgFile.m_ofn.nMaxCustFilter++;
|
|
|
|
|
|
|
|
|
|
dlgFile.m_ofn.lpstrFilter = strFilter;
|
|
|
|
|
dlgFile.m_ofn.lpstrTitle = title;
|
|
|
|
|
|
|
|
|
|
if(Path == "")
|
|
|
|
|
{
|
|
|
|
|
if(dlgFile.DoModal()==IDCANCEL)
|
|
|
|
|
return FALSE; // open cancelled
|
|
|
|
|
sExcelFile = dlgFile.GetPathName();
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>ΪĬ<CEAA><C4AC>·<EFBFBD><C2B7>
|
|
|
|
|
int Index = sExcelFile.ReverseFind(_T('\\'));
|
|
|
|
|
int len = sExcelFile.GetLength();
|
|
|
|
|
Path = sExcelFile.Left(len -(len-Index));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sExcelFile = Path+"\\"+timeStr + ".xls";
|
|
|
|
|
}
|
|
|
|
|
sExcelFile.ReleaseBuffer();
|
|
|
|
|
if (MakeSurePathExists(sExcelFile,true)) {
|
|
|
|
|
if(!DeleteFile(sExcelFile)) { // delete the file
|
|
|
|
|
AfxMessageBox("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//<2F><><EFBFBD>ƣ<EFBFBD>GetExcelDriver
|
|
|
|
|
//<2F><><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD>ȡODBC<42><43>Excel<65><6C><EFBFBD><EFBFBD>
|
|
|
|
|
//<2F><><EFBFBD>ߣ<EFBFBD><DFA3>쾰<EFBFBD><ECBEB0>(jingzhou_xu@163.net)
|
|
|
|
|
//<2F><>֯<EFBFBD><D6AF>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(Future Studio)
|
|
|
|
|
//<2F><><EFBFBD>ڣ<EFBFBD>2002.9.1
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
CString CExcelMgr::GetExcelDriver()
|
|
|
|
|
{
|
|
|
|
|
char szBuf[2001];
|
|
|
|
|
WORD cbBufMax = 2000;
|
|
|
|
|
WORD cbBufOut;
|
|
|
|
|
char *pszBuf = szBuf;
|
|
|
|
|
CString sDriver;
|
|
|
|
|
|
|
|
|
|
// <20><>ȡ<EFBFBD>Ѱ<EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>odbcinst.h<><68>)
|
|
|
|
|
if (!SQLGetInstalledDrivers(szBuf, cbBufMax, &cbBufOut))
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>Ѱ<EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>Excel...
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
if (strstr(pszBuf, "Excel") != 0)
|
|
|
|
|
{
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD> !
|
|
|
|
|
sDriver = CString(pszBuf);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
pszBuf = strchr(pszBuf, '\0') + 1;
|
|
|
|
|
}
|
|
|
|
|
while (pszBuf[1] != '\0');
|
|
|
|
|
|
|
|
|
|
return sDriver;
|
|
|
|
|
}
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD>(ColNum <20><><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>)
|
|
|
|
|
void CExcelMgr::CreatDataBaseTable(CString &Path,int ColNum)
|
|
|
|
|
{
|
|
|
|
|
CString sDriver;
|
|
|
|
|
CString sExcelFile;
|
|
|
|
|
CString sSql;
|
|
|
|
|
CString tableName(TABLE_NAME);
|
|
|
|
|
|
|
|
|
|
sDriver = GetExcelDriver();
|
|
|
|
|
if (sDriver.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
// û<>з<EFBFBD><D0B7><EFBFBD>Excel<65><6C><EFBFBD><EFBFBD>
|
|
|
|
|
AfxMessageBox("û<EFBFBD>а<EFBFBD>װExcel!\n<EFBFBD><EFBFBD><EFBFBD>Ȱ<EFBFBD>װExcel<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///Ĭ<><C4AC><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
|
|
|
|
|
if (!GetDefaultXlsFileName(Path,sExcelFile))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|
|
|
|
sSql.Format("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",sDriver, sExcelFile, sExcelFile);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD> (<28><>Excel<65><6C><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>)
|
|
|
|
|
if( m_Database.OpenEx(sSql,CDatabase::noOdbcDialog) )
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṹ
|
|
|
|
|
int i;
|
|
|
|
|
LVCOLUMN columnData;
|
|
|
|
|
CString columnName;
|
|
|
|
|
|
|
|
|
|
sSql = "";
|
|
|
|
|
columnData.mask = LVCF_TEXT;
|
|
|
|
|
columnData.cchTextMax =100;
|
|
|
|
|
columnData.pszText = columnName.GetBuffer (100);
|
|
|
|
|
for(i=0;i<ColNum;i++)
|
|
|
|
|
{
|
|
|
|
|
if (i!=0)
|
|
|
|
|
{
|
|
|
|
|
sSql = sSql + ", " ;
|
|
|
|
|
}
|
|
|
|
|
CString ColName;
|
|
|
|
|
ColName.Format(_T("%d"),i);
|
|
|
|
|
sSql = sSql + " " + ColName +" TEXT";
|
|
|
|
|
}
|
|
|
|
|
columnName.ReleaseBuffer();
|
|
|
|
|
|
|
|
|
|
m_ColNum = ColNum;
|
|
|
|
|
|
|
|
|
|
sSql = "CREATE TABLE " + tableName + " ( " + sSql + " ) ";
|
|
|
|
|
m_Database.ExecuteSQL(sSql);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
CString CExcelMgr::GetTableItemName()
|
|
|
|
|
{
|
|
|
|
|
CString strH;//<2F><><EFBFBD><EFBFBD>
|
|
|
|
|
strH = "";
|
|
|
|
|
for(int i=0;i<m_ColNum;i++)
|
|
|
|
|
{
|
|
|
|
|
if (i!=0)
|
|
|
|
|
{
|
|
|
|
|
strH = strH + ", " ;
|
|
|
|
|
}
|
|
|
|
|
CString ColName;
|
|
|
|
|
ColName.Format(_T("%d"),i);
|
|
|
|
|
strH = strH + " " + ColName +" ";
|
|
|
|
|
}
|
|
|
|
|
return strH;
|
|
|
|
|
}
|
|
|
|
|
void CExcelMgr::ExportListToExcel(CListCtrl* pList)
|
|
|
|
|
{
|
|
|
|
|
if(!m_Database.IsOpen())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
CString tableName(TABLE_NAME);
|
|
|
|
|
CString sSql;
|
|
|
|
|
CString strH = GetTableItemName();//<2F><><EFBFBD><EFBFBD>
|
|
|
|
|
CString strV;//ֵ
|
|
|
|
|
int columnNum = 0;
|
|
|
|
|
//pList <20>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int k = 0;
|
|
|
|
|
CHeaderCtrl* pHeaderCtrl = pList->GetHeaderCtrl();
|
|
|
|
|
if(pHeaderCtrl)
|
|
|
|
|
{
|
|
|
|
|
columnNum = pHeaderCtrl->GetItemCount();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int nItemIndex;
|
|
|
|
|
for (nItemIndex=0;nItemIndex<pList->GetItemCount();nItemIndex++)
|
|
|
|
|
{
|
|
|
|
|
strV = "";
|
|
|
|
|
for(int i=0;i<m_ColNum;i++)
|
|
|
|
|
{
|
|
|
|
|
if (i!=0)
|
|
|
|
|
{
|
|
|
|
|
strV = strV + ", " ;
|
|
|
|
|
}
|
|
|
|
|
if(i<columnNum)
|
|
|
|
|
{
|
|
|
|
|
strV = strV + " '" + pList->GetItemText(nItemIndex,i) +"' ";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
strV = strV + " '" + " " +"' ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sSql = "INSERT INTO "+ tableName +" ("+ strH + ")"+" VALUES("+ strV + ")";
|
|
|
|
|
m_Database.ExecuteSQL(sSql);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
|
|
|
|
void CExcelMgr::InsertStringVec(vector<CString> &StrVec)
|
|
|
|
|
{
|
|
|
|
|
if(!m_Database.IsOpen())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
CString tableName(TABLE_NAME);
|
|
|
|
|
CString sSql;
|
|
|
|
|
CString strH = GetTableItemName();//<2F><><EFBFBD><EFBFBD>
|
|
|
|
|
CString strV;//ֵ
|
|
|
|
|
|
|
|
|
|
int size = StrVec.size();
|
|
|
|
|
strV = "";
|
|
|
|
|
for(int i=0;i<m_ColNum;i++)
|
|
|
|
|
{
|
|
|
|
|
if (i!=0)
|
|
|
|
|
{
|
|
|
|
|
strV = strV + ", " ;
|
|
|
|
|
}
|
|
|
|
|
if(i>=size)
|
|
|
|
|
{
|
|
|
|
|
strV = strV + " '" + " " +"' ";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
strV = strV + " '" + StrVec[i] +"' ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sSql = "INSERT INTO "+ tableName +" ("+ strH + ")"+" VALUES("+ strV + ")";
|
|
|
|
|
m_Database.ExecuteSQL(sSql);
|
|
|
|
|
}
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
void CExcelMgr::InsertNullItem(int n)
|
|
|
|
|
{
|
|
|
|
|
if(!m_Database.IsOpen())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
CString tableName(TABLE_NAME);
|
|
|
|
|
CString sSql;
|
|
|
|
|
CString strH = GetTableItemName();//<2F><><EFBFBD><EFBFBD>
|
|
|
|
|
CString strV;//ֵ
|
|
|
|
|
|
|
|
|
|
for(int k=0;k<n;k++)
|
|
|
|
|
{
|
|
|
|
|
strV = "";
|
|
|
|
|
for(int i=0;i<m_ColNum;i++)
|
|
|
|
|
{
|
|
|
|
|
if (i!=0)
|
|
|
|
|
{
|
|
|
|
|
strV = strV + ", " ;
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
strV = strV + " '" + " " +"' ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sSql = "INSERT INTO "+ tableName +" ("+ strH + ")"+" VALUES("+ strV + ")";
|
|
|
|
|
m_Database.ExecuteSQL(sSql);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//<2F>ر<EFBFBD><D8B1><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD>
|
|
|
|
|
void CExcelMgr::CloseDataBase()
|
|
|
|
|
{
|
|
|
|
|
if(m_Database.IsOpen())
|
|
|
|
|
m_Database.Close();
|
|
|
|
|
}
|