(1) 在VC6.0 中点击View菜单,选中ClassWizard打开类向导对话框,点击右侧的“Add Class”按
钮选择From a Type Library选项,在打开的对话框中找到Office的安装地址并找到EXCEL.EXE,
选中后点击“打开”按钮,此时弹出一个“Confirm Classes”对话框,按住键盘上面的Ctrl键
选中“_Application”、“Workbooks”、“_Workbook”、“Worksheets”、
“_Worksheet”、“Range”(选中这几个够基本的读写操作),此时在工程中会多出来两个文
件,Excel.h和Excel.cpp文件,打开Excel.h文件在文件的开始添加namespace spaceExcel{,
在文件的结束添加},打开Excel.cpp文件在头文件的引用最后的地方添加
using namespace spaceExcel;
(2) 在工程项目的*App类的InitInstance函数中对OLE环境进行初始化:
if (!AfxOleInit())
{
AfxMessageBox("初始化OLE DLL失败!");
returnFALSE;
}
(3) 在工程项目的*.Dlg的类中添加成员变量并在该文件的首部添加上excel.h头文件
spaceExcel::Range rgMyRge,excelRange;//选区
spaceExcel::_Worksheet wsMysheet;//工作表对象
spaceExcel::Worksheets wssMysheets;//工作表管理器
spaceExcel::_Workbook wbMyBook;//工作薄对象
spaceExcel::Workbooks wbsMyBooks;//工作薄管理器
spaceExcel::_Application ExcelApp;//应用程序对象
(4) 操作Excel表格:【读取excel中的数据】
if(!ExcelApp.CreateDispatch("Excel.Application",NULL))
{
AfxMessageBox("未检测到Excel程序!");
}
else
{
ExcelApp.SetVisible(FALSE);//设置excel工作簿为不可见状态
wbsMyBooks.AttachDispatch(ExcelApp.GetWorkbooks(),TRUE);
wbMyBook =wbsMyBooks.Open(m_strExternalFilePath, vtMissing,vtMissing,vtMissing,vtMissing, vtMissing,vtMissing,vtMissing,vtMissing, vtMissing,vtMissing,vtMissing,vtMissing, vtMissing,vtMissing);
wssMysheets.AttachDispatch(wbMyBook.GetWorksheets(),TRUE);
wsMysheet =wssMysheets.GetItem(COleVariant((short)1));
rgMyRge =wsMysheet.GetUsedRange();
excelRange =rgMyRge.GetRows();
long nRowNum = excelRange.GetCount();
excelRange =rgMyRge.GetColumns();
long nColumnNum =excelRange.GetCount();
rgMyRge.AttachDispatch(wsMysheet.GetCells(),TRUE);
for (long row = 1; row<= nRowNum; row++)
{
for (long column= 1; column <= nColumnNum; column++)
{
excelRange.AttachDispatch(rgMyRge.GetItem(_variant_t(row),
_variant_t(column)).pdispVal,TRUE);
VARIANTvarItemName = excelRange.GetText();
switch(column)
{
case 1:
strNum= varItemName.bstrVal;
continue;
case2:
strName= varItemName.bstrVal;
continue;
case 3:
strSex= varItemName.bstrVal;
continue;
case 4:
strAcademy= varItemName.bstrVal;
continue;
case 5:
strAdress= varItemName.bstrVal;
continue;
default:
break;
}
}
}
}
【写数据到excel中】
if(!ExcelApp.CreateDispatch("Excel.Application",NULL))
{
AfxMessageBox("创建Excel服务失败!");
return;
}
ExcelApp.SetVisible(FALSE);
wbsMyBooks.AttachDispatch(ExcelApp.GetWorkbooks(),TRUE);
wbMyBook.AttachDispatch(wbsMyBooks.Add((_variant_t)strSavePath),TRUE);
wssMysheets.AttachDispatch(wbMyBook.GetSheets(),TRUE);
wssMysheets.Add(vtMissing,vtMissing,_variant_t((long)1),vtMissing);
wsMysheet.AttachDispatch(wssMysheets.GetItem(_variant_t((long)2)),TRUE);
wsMysheet.Delete();
wsMysheet.AttachDispatch(wssMysheets.GetItem(_variant_t((long)1)),TRUE);
wsMysheet.SetName("student");
rgMyRge.AttachDispatch(wsMysheet.GetCells(),TRUE);
for(int i = 0; i < nCount; i++)
{ //读取列表控件中的数据到Excel文件中
m_list.GetItemText(i,0,strNum,MAX_PATH);
m_list.GetItemText(i,1,strName,MAX_PATH);
m_list.GetItemText(i,2,strSex,MAX_PATH);
m_list.GetItemText(i,3,strAcademy,MAX_PATH);
m_list.GetItemText(i,4,strAdress,MAX_PATH);
rgMyRge.SetItem(_variant_t((long)i+1),_variant_t((long)1),_variant_t(strNum));
rgMyRge.SetItem(_variant_t((long)i+1),_variant_t((long)2),_variant_t(strName));
rgMyRge.SetItem(_variant_t((long)i+1),_variant_t((long)3),_variant_t(strSex));
rgMyRge.SetItem(_variant_t((long)i+1),_variant_t((long)4),_variant_t(strAcademy));
rgMyRge.SetItem(_variant_t((long)i+1),_variant_t((long)5),_variant_t(strAdress));
}
ExcelApp.SetAlertBeforeOverwriting(FALSE);
ExcelApp.SetDisplayAlerts(FALSE);
wsMysheet.SaveAs(strSavePath,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,
vtMissing,vtMissing,vtMissing,vtMissing);
AfxMessageBox("Excel文件导出完成!");
(5) 释放excel资源:
excelRange.ReleaseDispatch();
rgMyRge.ReleaseDispatch();
wsMysheet.ReleaseDispatch();
wssMysheets.ReleaseDispatch();
wbMyBook.ReleaseDispatch();
wbsMyBooks.ReleaseDispatch();
ExcelApp.Quit();
ExcelApp.ReleaseDispatch(); //注意:先Quit,再ReleaseDispatch;
注:使用vtMissing需要的头文件:#include <comutil.h>