CString sSql;
CString sDriver;
CString sDsn;
CString sFile = "AddressExcel.xls";
sDriver = GetExcelDriver();
if (sDriver.IsEmpty())
{
AfxMessageBox("没有安装Excel驱动!");
return;
}
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s", sDriver, sFile);
try
{
database.Open(NULL, false, false, sDsn);
CRecordset recset(&database);
sSql="SELECT * FROM address";
recset.Open(CRecordset::forwardOnly, sSql, CRecordset::readOnly);
recset.MoveFirst();
int i=0;
while (!recset.IsEOF())
{
CString str1,str2,str3;
recset.GetFieldValue("Relation",str1);
recset.GetFieldValue("Name",str2);
recset.GetFieldValue("Address",str3);
m_list.InsertItem(i,"");
m_list.SetItemText(i,0,str1);
m_list.SetItemText(i,1,str2);
m_list.SetItemText(i,2,str3);
recset.MoveNext();
i++;
}
database.Close();
}
catch(CDBException e)
{
AfxMessageBox("数据库错误: "+e.m_strError);
}
以上代码用于读取excel表格中内容,表格的默认表名应该是Sheet1吧?
我现在都搞糊涂了,网上查了说要改,有一种方法:(需要先选定我们要读取的数据,之后,选择插入>>名字>>)
网上说插入的名字就是我们选定数据的表名,但运行后还是无法读取,文件名,各种表名也试了,都不行,
老是弹出说读不到该表,让我检查路径是否正确,诚不知其所然矣!
望高手赐教!谢谢!谢谢!!!
5 个解决方案
#1
sSql="SELECT * FROM address"; 关键是这段代码里的FROM后面的东西,搞得我心都碎了,望好人相教!!!
#2
请能者帮助我一下!
不胜感激!
不胜感激!
#3
我只会OLE方式
CApplication m_appExcel;
CWorkbooks m_books;
CWorkbook m_book;
CWorksheets m_sheets;
CWorksheet m_sheet;
//.......................//
// 初始化Com
if (::CoInitialize( NULL ) == E_INVALIDARG)
{
AfxMessageBox(_T("初始化Com失败!"));
}
// 启动Excel
if ( !m_appExcel.CreateDispatch(_T("Excel.Application"), NULL))
{
AfxMessageBox(_T("创建Excel服务失败!"));
::CoUninitialize();
}
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
m_appExcel.put_Visible(TRUE);
m_books.AttachDispatch(m_appExcel.get_Workbooks());
m_books.Open(_T("E:\\123.xls"), covOptional, covOptional,covOptional,covOptional,
covOptional,covOptional,covOptional,covOptional,covOptional,covOptional
,covOptional,covOptional,covOptional,covOptional);
m_book.AttachDispatch(m_books.Add(covOptional));
m_sheets.AttachDispatch(m_book.get_Worksheets());
m_sheet.AttachDispatch(m_sheets.get_Item(_variant_t(1)));
MessageBox(m_sheet.get_Name());
#4
我要ODBC操作Excel
请能者为我解忧,不胜感激!
请能者为我解忧,不胜感激!
#5
SQL语句 写成 sSql="SELECT * FROM [address$]";试试看吧
#1
sSql="SELECT * FROM address"; 关键是这段代码里的FROM后面的东西,搞得我心都碎了,望好人相教!!!
#2
请能者帮助我一下!
不胜感激!
不胜感激!
#3
我只会OLE方式
CApplication m_appExcel;
CWorkbooks m_books;
CWorkbook m_book;
CWorksheets m_sheets;
CWorksheet m_sheet;
//.......................//
// 初始化Com
if (::CoInitialize( NULL ) == E_INVALIDARG)
{
AfxMessageBox(_T("初始化Com失败!"));
}
// 启动Excel
if ( !m_appExcel.CreateDispatch(_T("Excel.Application"), NULL))
{
AfxMessageBox(_T("创建Excel服务失败!"));
::CoUninitialize();
}
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
m_appExcel.put_Visible(TRUE);
m_books.AttachDispatch(m_appExcel.get_Workbooks());
m_books.Open(_T("E:\\123.xls"), covOptional, covOptional,covOptional,covOptional,
covOptional,covOptional,covOptional,covOptional,covOptional,covOptional
,covOptional,covOptional,covOptional,covOptional);
m_book.AttachDispatch(m_books.Add(covOptional));
m_sheets.AttachDispatch(m_book.get_Worksheets());
m_sheet.AttachDispatch(m_sheets.get_Item(_variant_t(1)));
MessageBox(m_sheet.get_Name());
#4
我要ODBC操作Excel
请能者为我解忧,不胜感激!
请能者为我解忧,不胜感激!
#5
SQL语句 写成 sSql="SELECT * FROM [address$]";试试看吧