MFC 加载 EXCEL库 可以查看
http://t.csdn.cn/U8TJU
这里主要记录一下读取EXCEL数据的方式:
方式一:遍历所有单元格,不适用于大数据量
CApplication app1;
CWorkbooks books;
CWorkbook book;
CWorksheets sheets;
CWorksheet sheet;
CRange range;
CRange oCurCell;
LPDISPATCH lpDisp;
COleVariant vResult; //COleVariant类是对VARIANT结构的封装
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
COleSafeArray ole_safe_array_;
if (S_OK != CoInitialize(NULL)){
return -1;
}
if (!app1.CreateDispatch(_T("Excel.Application"), NULL))
{
MessageBox(NULL,_T("无法启动Excel服务器!"), _T("提示"), MB_ICONWARNING);
return 0;
}
books.AttachDispatch(app1.get_Workbooks());
lpDisp = books.Open(pDlg->m_fileName, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional);
//得到Workbook (工作簿)
book.AttachDispatch(lpDisp);
//得到Worksheets (工作表)
sheets.AttachDispatch(book.get_Worksheets());
//sheet = sheets.get_Item(COleVariant((short)1));
//得到当前活跃sheet
//如果有单元格正处于编辑状态中,此操作不能返回,会一直等待
lpDisp = book.get_ActiveSheet();
sheet.AttachDispatch(lpDisp);
// 获得使用的区域Range(区域)
range.AttachDispatch(