RWExcelDlg.cpp
上传用户:xiuanze55
上传日期:2013-06-16
资源大小:85k
文件大小:11k
- //////////////////////////////////////////////////////////////////////////////
- //类名:CRWExcelDlg
- //功能:直接通过ODBC读、写Excel文件示例实现对话框
- //作者:徐景周(jingzhou_xu@163.net)
- //组织:未来工作室(Future Studio)
- //日期:2002.9.1
- /////////////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "RWExcel.h"
- #include "RWExcelDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- CPictureEx m_Flag; //GIF动态图像显示
- CHyperLink m_Mail; //超链接形式显示EMAIL
- CWBButton m_OK; //位图按钮
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- virtual BOOL OnInitDialog();
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- DDX_Control(pDX, IDC_FLAG, m_Flag);
- DDX_Control(pDX, IDC_STATIC_MAIL, m_Mail);
- DDX_Control(pDX, IDOK, m_OK);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- //////////////////////////////////////////////////////////////////////////////
- //名称:OnInitDialog
- //功能:初始化"关于"对话框
- //作者:徐景周(jingzhou_xu@163.net)
- //组织:未来工作室(Future Studio)
- //日期:2001.12.1
- /////////////////////////////////////////////////////////////////////////////
- BOOL CAboutDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- //设置“关于”对话框的位图按钮显示
- m_OK.LoadBitmaps(IDB_BUTTON,5, 5, 5, 5, 4 );
- //加入EMAIL的超连接
- m_Mail.SetURL(_T("mailto:jingzhou_xu@163.net"));
- m_Mail.SetUnderline(FALSE);
- //显示动态GIF图像logo
- if(m_Flag.Load(MAKEINTRESOURCE(IDR_FLAG),_T("GIF")))
- {
- m_Flag.SetBkColor(RGB(160,180,220));
- m_Flag.Draw();
- }
-
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- /////////////////////////////////////////////////////////////////////////////
- // CRWExcelDlg dialog
- CRWExcelDlg::CRWExcelDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CRWExcelDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CRWExcelDlg)
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CRWExcelDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CRWExcelDlg)
- DDX_Control(pDX, IDC_ABOUT, m_About);
- DDX_Control(pDX, IDC_LISTEXCEL, m_ExcelList);
- DDX_Control(pDX, IDOK, m_OK);
- DDX_Control(pDX, IDC_WRITEEXCEL, m_Write);
- DDX_Control(pDX, IDC_READEXCEL, m_Read);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CRWExcelDlg, CDialog)
- //{{AFX_MSG_MAP(CRWExcelDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_READEXCEL, OnReadexcel)
- ON_BN_CLICKED(IDC_WRITEEXCEL, OnWriteexcel)
- ON_BN_CLICKED(IDC_ABOUT, OnAbout)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CRWExcelDlg message handlers
- //////////////////////////////////////////////////////////////////////////////
- //名称:OnInitDialog
- //功能:初始化对话框
- //作者:徐景周(jingzhou_xu@163.net)
- //组织:未来工作室(Future Studio)
- //日期:2002.9.1
- /////////////////////////////////////////////////////////////////////////////
- BOOL CRWExcelDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
-
- //初始置窗体位于最顶层
- SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
- //设置位图按钮显示
- m_OK.LoadBitmaps(IDB_BUTTON,5, 5, 5, 5, 4 );
- m_About.LoadBitmaps(IDB_BUTTON,5, 5, 5, 5, 4 );
- m_Write.LoadBitmaps(IDB_BUTTON,5, 5, 5, 5, 4 );
- m_Read.LoadBitmaps(IDB_BUTTON,5, 5, 5, 5, 4 );
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CRWExcelDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CRWExcelDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CRWExcelDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- //////////////////////////////////////////////////////////////////////////////
- //名称:OnReadexcel
- //功能:从Excel文件中读取相应数据并显示出来
- //作者:徐景周(jingzhou_xu@163.net)
- //组织:未来工作室(Future Studio)
- //日期:2002.9.1
- /////////////////////////////////////////////////////////////////////////////
- void CRWExcelDlg::OnReadexcel()
- {
- CDatabase database;
- CString sSql;
- CString sItem1, sItem2;
- CString sDriver;
- CString sDsn;
- CString sFile,sPath;
-
- //获取主程序所在路径,存在sPath中
- GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
- sPath.ReleaseBuffer ();
- int nPos;
- nPos=sPath.ReverseFind ('\');
- sPath=sPath.Left (nPos);
- sFile = sPath + "\Demo.xls"; // 将被读取的Excel文件名
- // 检索是否安装有Excel驱动 "Microsoft Excel Driver (*.xls)"
- sDriver = GetExcelDriver();
- if (sDriver.IsEmpty())
- {
- // 没有发现Excel驱动
- AfxMessageBox("没有安装Excel驱动!");
- return;
- }
-
- // 创建进行存取的字符串
- sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s", sDriver, sFile);
- TRY
- {
- // 打开数据库(既Excel文件)
- database.Open(NULL, false, false, sDsn);
-
- CRecordset recset(&database);
- // 设置读取的查询语句.
- sSql = "SELECT Name, Age "
- "FROM Exceldemo " ;
- "ORDER BY Name ";
-
- // 执行查询语句
- recset.Open(CRecordset::forwardOnly, sSql, CRecordset::readOnly);
- // 获取查询结果
- while (!recset.IsEOF())
- {
- //读取Excel内部数值
- recset.GetFieldValue("Name", sItem1);
- recset.GetFieldValue("Age", sItem2);
- //显示记取的内容
- m_ExcelList.AddString( sItem1 + " --> "+sItem2 );
- // 移到下一行
- recset.MoveNext();
- }
- // 关闭数据库
- database.Close();
-
- }
- CATCH(CDBException, e)
- {
- // 数据库操作产生异常时...
- AfxMessageBox("数据库错误: " + e->m_strError);
- }
- END_CATCH;
-
- }
- //////////////////////////////////////////////////////////////////////////////
- //名称:GetExcelDriver
- //功能:获取ODBC中Excel驱动
- //作者:徐景周(jingzhou_xu@163.net)
- //组织:未来工作室(Future Studio)
- //日期:2002.9.1
- /////////////////////////////////////////////////////////////////////////////
- CString CRWExcelDlg::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, ' ') + 1;
- }
- while (pszBuf[1] != ' ');
- return sDriver;
- }
- //////////////////////////////////////////////////////////////////////////////
- //名称:OnWriteexcel
- //功能:创建并写入数据到Excel文件中
- //作者:徐景周(jingzhou_xu@163.net)
- //组织:未来工作室(Future Studio)
- //日期:2002.9.1
- /////////////////////////////////////////////////////////////////////////////
- void CRWExcelDlg::OnWriteexcel()
- {
- CDatabase database;
- CString sDriver = "MICROSOFT EXCEL DRIVER (*.XLS)"; // Excel安装驱动
- CString sExcelFile,sPath;
- CString sSql;
-
- //获取主程序所在路径,存在sPath中
- GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
- sPath.ReleaseBuffer ();
- int nPos;
- nPos=sPath.ReverseFind ('\');
- sPath=sPath.Left (nPos);
- sExcelFile = sPath + "\Demo.xls"; // 要建立的Excel文件
- TRY
- {
- // 创建进行存取的字符串
- sSql.Format("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB="%s";DBQ=%s",sDriver, sExcelFile, sExcelFile);
- // 创建数据库 (既Excel表格文件)
- if( database.OpenEx(sSql,CDatabase::noOdbcDialog) )
- {
- // 创建表结构(姓名、年龄)
- sSql = "CREATE TABLE Exceldemo (Name TEXT,Age NUMBER)";
- database.ExecuteSQL(sSql);
- // 插入数值
- sSql = "INSERT INTO Exceldemo (Name,Age) VALUES ('徐景周',26)";
- database.ExecuteSQL(sSql);
- sSql = "INSERT INTO Exceldemo (Name,Age) VALUES ('徐志慧',22)";
- database.ExecuteSQL(sSql);
- sSql = "INSERT INTO Exceldemo (Name,Age) VALUES ('郭徽',27)";
- database.ExecuteSQL(sSql);
- }
- // 关闭数据库
- database.Close();
- AfxMessageBox("Excel文件写入成功!");
- }
- CATCH_ALL(e)
- {
- TRACE1("Excel驱动没有安装: %s",sDriver);
- }
- END_CATCH_ALL;
-
- }
- //////////////////////////////////////////////////////////////////////////////
- //名称:OnAbout
- //功能:弹出"关于"对话框
- //作者:徐景周(jingzhou_xu@163.net)
- //组织:未来工作室(Future Studio)
- //日期:2002.9.1
- /////////////////////////////////////////////////////////////////////////////
- void CRWExcelDlg::OnAbout()
- {
- CAboutDlg AboutDlg;
- AboutDlg.DoModal();
- }