ITPub博客

首页 > Linux操作系统 > Linux操作系统 > odbc写excel

odbc写excel

原创 Linux操作系统 作者:xuhuai1 时间:2009-07-15 16:44:04 0 删除 编辑

#pragma once
#include
#include

class __declspec(dllexport) CRWExcel
{
public:
 CRWExcel(void);
 CRWExcel(CString File, CString SheetOrSeparator, bool Backup = true);
 CString getExcelDriver();
 BOOL open(const CString &dsn);
 bool readRows(CString &sql,CStringArray &RowValues);
 bool readTitles(CStringArray &RowValues);
 bool executeSQL(CString &sql);
public:
 ~CRWExcel(void);
private:
 CDatabase *m_pDatabase;
 CRecordset *m_pRecordset;
 CString m_sDsn;
 CString m_sSheetName;// = SheetOrSeparator;
 CString m_sFile;
 CString m_sSql;

 int m_dTotalColumns;//total columns
};

 

#include "StdAfx.h"
#include "RWExcel.h"
#include
using namespace std;
CRWExcel::CRWExcel(CString sFile, CString SheetOrSeparator, bool Backup /* = true */)
:m_sFile(sFile),m_pRecordset(NULL)
{
 m_pDatabase = new CDatabase;

 m_sDsn.Format(L"DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s", getExcelDriver(), sFile, sFile);
 if(!open(m_sDsn))
 {
  AfxMessageBox(L"打開失敗");
  m_pDatabase->Close();
  m_pDatabase = NULL;
 }
 if (m_pDatabase)
 {
  m_pRecordset = new CRecordset(m_pDatabase);
 }
}

CRWExcel::CRWExcel(void)
{
}

CRWExcel::~CRWExcel(void)
{
 if (m_pRecordset != NULL)
 {
  m_pRecordset->Close();
  delete m_pRecordset;
  m_pRecordset = NULL;
 }
 if (m_pDatabase != NULL)
 {
  m_pDatabase->Close();
  delete m_pDatabase;
  m_pDatabase = NULL;
 }
}
CString CRWExcel::getExcelDriver()
{
 wchar_t szBuf[2001];
 WORD cbBufMax = 2000;
 WORD cbBufOut;
 wchar_t *pszBuf = szBuf;
 CString sDirver;
 // Get the names of the installed drivers ("odbcinst.h" has to be included )
 if(!SQLGetInstalledDrivers(szBuf,cbBufMax,& cbBufOut))
 {
  sDirver = L"";
 }

 // Search for the driver...
 do
 {
  if( wcsstr( pszBuf, L"Excel" ) != 0 )
  {
   // Found !
   sDirver= CString( pszBuf );
   break;
  }
  pszBuf = wcschr( pszBuf, '\0' ) + 1;
 }
 while( pszBuf[1] != '\0' );
 return sDirver;
}

BOOL CRWExcel::open(const CString &dsn)
{
 return m_pDatabase->OpenEx(dsn, CDatabase::noOdbcDialog);
}
bool CRWExcel::executeSQL(CString &sql)
{
 try
 {
  // 能夠後退 [3/12/2009 f3219162]
  // pRecordset->Open(CRecordset::snapshot, sql, CRecordset::readOnly);
  // 不能夠后退
  //pRecordset->Open(CRecordset::forwardOnly, sql, CRecordset::readOnly);
  m_pRecordset->Open(CRecordset::snapshot, sql, CRecordset::readOnly);
 }
 catch(...)
 {
  return false;
 }
 return true;
}
bool CRWExcel::readRows(CString &sql,CStringArray &RowValues)
{
 ASSERT(m_pDatabase!=NULL);
 ASSERT(m_pRecordset!=NULL);
 //- 得到列數
 int nColumns =m_pRecordset->m_nResultCols;
 while (!m_pRecordset->IsEOF())
 {

  wstring wstr(L"");
  for (int i=0;i  {
   CString sAge1;
   m_pRecordset->GetFieldValue(i,sAge1);
   wstr +=L" ";
   wstr +=sAge1.GetBuffer();
   wstr +=L" ";
  }
  CString sAge(wstr.c_str());
  RowValues.Add(sAge);
  m_pRecordset->MoveNext();
 }
 m_pRecordset->MoveFirst();
 return true;
}
bool CRWExcel::readTitles(CStringArray &RowValues)
{
 ASSERT(m_pDatabase!=NULL);
 ASSERT(m_pRecordset!=NULL);
 int nColumns =m_pRecordset->m_nResultCols;
 if(m_pRecordset->IsEOF())
 {
  return false;
 }
 wstring wstr(L"");
 for (int i=0;i {
  CString sAge1;
  m_pRecordset->GetFieldValue(i,sAge1);
  wstr +=L" ";
  wstr += m_pRecordset->m_rgODBCFieldInfos[i].m_strName;
  wstr +=L" ";
 }
 CString sAge(wstr.c_str());
 RowValues.Add(sAge);
 return true;
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12560256/viewspace-609244/,如需转载,请注明出处,否则将追究法律责任。

全部评论

注册时间:2008-09-03

  • 博文量
    13
  • 访问量
    26888