?? excel1.cpp
字號:
// Excel1.cpp: implementation of the CExcel class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "GSMTest.h"
#include "Excel1.h"
#include <io.h>
#include <odbcinst.h>
#include <afxdb.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CExcel::CExcel()
{
}
CExcel::~CExcel()
{
}
BOOL CExcel::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);
}
BOOL CExcel::GetDefaultXlsFileName(CString &sExcelFile)
{
///默認文件名:yyyymmddhhmmss.xls
CString timeStr;
CTime day;
day=CTime::GetCurrentTime();
int filenameday,filenamemonth,filenameyear,filehour,filemin,filesec;
filenameday=day.GetDay();//dd
filenamemonth=day.GetMonth();//mm月份
filenameyear=day.GetYear();//yyyy
filehour=day.GetHour();//hh
filemin=day.GetMinute();//mm分鐘
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 = "導出";
strFilter = "Excel文件(*.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 (dlgFile.DoModal()==IDCANCEL)
return FALSE; // open cancelled
sExcelFile.ReleaseBuffer();
if (MakeSurePathExists(sExcelFile,true)) {
if(!DeleteFile(sExcelFile)) { // delete the file
AfxMessageBox("覆蓋文件時出錯!");
return FALSE;
}
}
return TRUE;
}
void CExcel::ExportListToExcel(CListCtrl *pList, CString strTitle)
{
CString warningStr;
if (pList->GetItemCount ()>0)
{
CDatabase database;
CString sDriver;
CString sExcelFile;
CString sSql;
CString tableName = strTitle;
// 檢索是否安裝有Excel驅動 "Microsoft Excel Driver (*.xls)"
sDriver = GetExcelDriver();
if (sDriver.IsEmpty())
{
// 沒有發現Excel驅動
AfxMessageBox("沒有安裝Excel!\n請先安裝Excel軟件才能使用導出功能!");
return;
}
///默認文件名
if (!GetDefaultXlsFileName(sExcelFile))
return;
// 創建進行存取的字符串
sSql.Format("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",sDriver, sExcelFile, sExcelFile);
// 創建數據庫 (既Excel表格文件)
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 =1000;
columnData.pszText = columnName.GetBuffer (1000);
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("導出文件保存于%s!",sExcelFile);
AfxMessageBox(warningStr);
}
}
CString CExcel::GetExcelDriver()
{
char szBuf[2001];
WORD cbBufMax = 2000;
WORD cbBufOut;
char *pszBuf = szBuf;
CString sDriver;
// 獲取已安裝驅動的名稱(涵數在odbcinst.h里)
if (!SQLGetInstalledDrivers(szBuf, cbBufMax, &cbBufOut))
return "";
// 檢索已安裝的驅動是否有Excel...
do
{
if (strstr(pszBuf, "Excel") != 0)
{
//發現 !
sDriver = CString(pszBuf);
break;
}
pszBuf = strchr(pszBuf, '\0') + 1;
}
while (pszBuf[1] != '\0');
return sDriver;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -