#include
#include
void ACC::OnButton1()
{
// TODO: Add your control notification handler code here
//CRecordset3 rs ;
int i =0 ;
CString str ;
//排序查询 ASC DESC ORDER BY '编号' ASC
if(rs.Open(CRecordset::forwardOnly,"SELECT * FROM [Sheet1$]" , CRecordset::none))
{
//取得所有数据记录集项
while(!rs.IsEOF())
{
if(i ==0)
{
//nID = rs.m_nID;
//str.Format("%d" ,nID );
rs.GetFieldValue( _T("国家") , str);
//AfxMessageBox(str) ;
GetDlgItem(IDC_EDIT2) -> SetWindowText(str);
}
i++ ;
// 移到下一行
rs.MoveNext();
}
}
rs.Close();
}
void ACC::OnOK()
{
// TODO: Add extra validation here
CString FilePathName ;
CFileDialog dlg(TRUE,"xls file(*.xls)|*.xls", NULL,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"xls file(*.xls)|*.xls||"); //TRUE为OPEN对话框,FALSE为SAVE AS对话框
if(dlg.DoModal()==IDOK)
{
FilePathName = dlg.GetPathName();
GetDlgItem(IDC_EDIT1) -> SetWindowText( FilePathName );
}
if(database.IsOpen())
database.Close();
//读入EXCEL
CString sDriver = _T( "MICROSOFT EXCEL DRIVER (*.xls)" ); // Excel驱动
// 创建进行存取的字符串
//sDsn.Format(_T( "ODBC;DRIVER={%s};DSN='';DBQ=%s" ), sDriver, sFile);
CString sDsn;
//sDsn.Format(_T( "ODBC;DRIVER={%s};DSN='';DBQ=%s;bUseCursorLib=FALSE" ), sDriver, FilePathName); //
sDsn.Format(_T( "ODBC;DRIVER={%s};DSN='';DBQ=%s" ), sDriver, FilePathName);
BOOL result = database.Open(NULL, false , false , sDsn);
if(!result)
return ;
else
{
//AfxMessageBox("CDatabase Open OK !");
}
//
// // 设置读取的查询语句.
//CString sSql ;
//sSql.Format( _T("SELECT [Sheet1$].[编号], [Sheet1$].[国家],[Sheet1$].[首都],[Sheet1$].[GDP] FROM [Sheet1$] ORDER BY [Sheet1$].[编号] ASC "));
// 设置读取的查询语句.
CString sSql;
sSql.Format( _T("SELECT * FROM %s ORDER BY '编号' ASC" ),"[Sheet1$]" ); //字段名称要分大小写
CRecordset rs(&database);
//rs.Open(CRecordset::forwardOnly, sSql, CRecordset::readOnly);
//rs.Open(CRecordset::dynaset, sSql, CRecordset::none);
rs.Open(CRecordset::forwardOnly, sSql,CRecordset::none);
short sFieldCount = rs.GetODBCFieldCount();
if(sFieldCount <=0)
return ;
//else
//AfxMessageBox("CRecordset Open OK !");
//取得控件指针
CListCtrl * m_ListCtrl = (CListCtrl *)GetDlgItem(IDC_LIST1);
DWORD dwExtStyles = m_ListCtrl->GetExtendedStyle();
m_ListCtrl->SetExtendedStyle(dwExtStyles | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
//清窗所有项
m_ListCtrl->DeleteAllItems();
// //清空所有列头
while(m_ListCtrl->GetHeaderCtrl()->GetItemCount() > 0 )
{
m_ListCtrl->DeleteColumn(m_ListCtrl->GetHeaderCtrl()->GetItemCount () -1) ;
}
//
m_ListCtrl->SetRedraw(TRUE);
m_ListCtrl->Invalidate();
m_ListCtrl->UpdateWindow();
//取得标头
CODBCFieldInfo fInfo ;
for ( short column =0 ; column < sFieldCount ; column ++)
{
rs.GetODBCFieldInfo(column , fInfo);
LV_COLUMN lvcolumn;
lvcolumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH ;
lvcolumn.fmt = LVCFMT_LEFT ;
lvcolumn.pszText = fInfo.m_strName.GetBuffer(fInfo.m_strName.GetLength() );
fInfo.m_strName.ReleaseBuffer();
lvcolumn.iSubItem = column ;
CRect rect ;
m_ListCtrl->GetClientRect( &rect);
lvcolumn.cx = rect.Width() / sFieldCount ;
m_ListCtrl->InsertColumn(column , &lvcolumn) ;
}
//取得项
CDBVariant var ;
CString value ;
while(!rs.IsEOF())
{
for ( column =0 ; column < sFieldCount ; column ++)
{
rs.GetFieldValue(column , var);
switch(var.m_dwType)
{
case DBVT_STRING:
value.Format(_T("%s"), var.m_pstring->GetBuffer(var.m_pstring->GetLength()));
break ;
case DBVT_DOUBLE:
value.Format(_T("%0.0f"), var.m_dblVal);
break ;
default:
value = _T("");
break;
}
if(column == 0)
{
m_ListCtrl->InsertItem(0, value);//设置该行的不同列的显示字符
}
else
{
m_ListCtrl->SetItemText(0, column ,value);//设置该行的不同列的显示字符
}
}
// 移到下一行
rs.MoveNext();
}
//关闭记录集
rs.Close();
// 关闭数据库
database.Close();
}
void CTestExcelDlg::OnButton3()
{
// TODO: Add your control notification handler code here
//写入EXCEL
CDatabase database;
CString sDriver = _T( "MICROSOFT EXCEL DRIVER (*.XLS)" ); // Excel安装驱动
CString sExcelFile = _T( "c:\\demo.xls" ); // 要建立的Excel文件
CString sSql;
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 = _T( "CREATE TABLE demo (Name TEXT,Age NUMBER)" );
database.ExecuteSQL(sSql);
// 插入数值
sSql = _T( "INSERT INTO demo (Name,Age) VALUES ('徐景周',26)" );
database.ExecuteSQL(sSql);
sSql = _T( "INSERT INTO demo (Name,Age) VALUES ('徐志慧',22)" );
database.ExecuteSQL(sSql);
sSql = _T( "INSERT INTO demo (Name,Age) VALUES ('郭徽',27)" );
database.ExecuteSQL(sSql);
}
// 关闭数据库
database.Close();
}
CATCH_ALL(e)
{
TRACE1( "Excel驱动没有安装: %s" ,sDriver);
}
END_CATCH_ALL;
}
void CTestExcelDlg::OnButton4()
{
// TODO: Add your control notification handler code here
//读入EXCEL
CDatabase database;
CString sSql;
CString sItem1, sItem2;
CString sDriver;
CString sDsn;
CString sFile = _T( "c:\\demo.xls" ); // 将被读取的Excel文件名
//c:\\
// 检索是否安装有Excel驱动 "Microsoft Excel Driver (*.xls)"
sDriver = GetExcelDriver();
if (sDriver.IsEmpty())
{
// 没有发现Excel驱动
AfxMessageBox(_T( "没有安装Excel驱动!" ));
return ;
}
// 创建进行存取的字符串
sDsn.Format(_T( "ODBC;DRIVER={%s};DSN='';DBQ=%s" ), sDriver, sFile);
TRY
{
// 打开数据库(既Excel文件)
database.Open(NULL, false , false , sDsn);
CRecordset recset(&database);
// 设置读取的查询语句.
sSql = _T( "SELECT Name, Age FROM demo ORDER BY Name " );
// 执行查询语句
recset.Open(CRecordset::forwardOnly, sSql, CRecordset::readOnly);
// 获取查询结果
while (!recset.IsEOF())
{
//读取Excel内部数值
recset.GetFieldValue(_T( "Name" ), sItem1);
recset.GetFieldValue(_T( "Age" ), sItem2);
AfxMessageBox(sItem1);
// 移到下一行
recset.MoveNext();
}
// 关闭数据库
database.Close();
}
CATCH(CDBException, e)
{
// 数据库操作产生异常时...
AfxMessageBox(_T( "数据库错误: " ) + e->m_strError);
}
END_CATCH;
}
CString CTestExcelDlg::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;
}