You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

315 lines
8.0 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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)
// 参数:
// Path 路径
// FilenameIncluded 路径是否包含文件名
// 返回值:
// 文件是否存在
// 说明:
// 判断Path文件(FilenameIncluded=true)是否存在,存在返回TURE不存在返回FALSE
// 自动创建目录
//
///////////////////////////////////////////////////////////////////////////////
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);
}
//获得默认的文件名
BOOL CExcelMgr::GetDefaultXlsFileName(CString &Path,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(Path == "")
{
if(dlgFile.DoModal()==IDCANCEL)
return FALSE; // open cancelled
sExcelFile = dlgFile.GetPathName();
//设置为默认路径
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("覆盖文件时出错!");
return FALSE;
}
}
return TRUE;
}
//////////////////////////////////////////////////////////////////////////////
//名称GetExcelDriver
//功能获取ODBC中Excel驱动
//作者:徐景周(jingzhou_xu@163.net)
//组织:未来工作室(Future Studio)
//日期2002.9.1
/////////////////////////////////////////////////////////////////////////////
CString CExcelMgr::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;
}
//创建数据库表(ColNum 是列的数量)
void CExcelMgr::CreatDataBaseTable(CString &Path,int ColNum)
{
CString sDriver;
CString sExcelFile;
CString sSql;
CString tableName(TABLE_NAME);
sDriver = GetExcelDriver();
if (sDriver.IsEmpty())
{
// 没有发现Excel驱动
AfxMessageBox("没有安装Excel!\n请先安装Excel软件才能使用导出功能!");
return;
}
///默认文件名
if (!GetDefaultXlsFileName(Path,sExcelFile))
return;
// 创建进行存取的字符串
sSql.Format("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",sDriver, sExcelFile, sExcelFile);
// 创建数据库 (既Excel表格文件)
if( m_Database.OpenEx(sSql,CDatabase::noOdbcDialog) )
{
// 创建表结构
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;//列名
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();//列名
CString strV;//值
int columnNum = 0;
//pList 列的数量
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);
}
}
//插入一行
void CExcelMgr::InsertStringVec(vector<CString> &StrVec)
{
if(!m_Database.IsOpen())
return;
CString tableName(TABLE_NAME);
CString sSql;
CString strH = GetTableItemName();//列名
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);
}
//插入空行
void CExcelMgr::InsertNullItem(int n)
{
if(!m_Database.IsOpen())
return;
CString tableName(TABLE_NAME);
CString sSql;
CString strH = GetTableItemName();//列名
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);
}
}
//关闭数据库表
void CExcelMgr::CloseDataBase()
{
if(m_Database.IsOpen())
m_Database.Close();
}