步骤:
-
安装microsoft的excel软件,因为只有ms的excel软件才提供了有关的类库
-
选中工程,添加类→MFC→Typelib中的MFC类
-
选中类库, 在可用类类型库中选择Microsoft Excel Object Library(据版本有可能细微之不同)文章来源:https://www.toymoban.com/news/detail-602625.html
-
添加需要使用的接口,根据网上大部分人的说法和能满足我需要的类就只有5个类CApplication, CWorkBook, CWorkSheet, CWorkBooks, CWorkSheets, CRange,选择接口时分别是_Application, _WorkBook, _WorkSheet, Range, WorkBooks, WorkSheets,选择好后点确定,就会多5个头文件,删除每个头文件里面的开头的“#import “C:\Program Files (x86)\Microsoft Office\Office12\EXCEL.EXE” no_namespace”,不删除则会有莫名其妙编译错误文章来源地址https://www.toymoban.com/news/detail-602625.html
//加载xls
HRESULT hr; //HRESULT函数返回值
hr = CoInitialize(NULL); //CoInitialize用来告诉 Windows以单线程的方式创建com对象
if (FAILED(hr))
{
AfxMessageBox(_T("Failed to call Coinitialize()"));
}
CFileDialog fd(true, "*.xls", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Xls文件 (*.xls)|*.xls|csv文件 (*.csv)|*.csv|All Files (*.*)|*.*||",this);
fd.m_ofn.lpstrTitle = "打开文件";
CString strFilePath;
if (IDOK == fd.DoModal())
{
strFilePath = fd.GetPathName();
}
else
{
return;
}
CApplication app1;
CWorkbooks books;
CWorkbook book;
CWorksheets sheets;
CWorksheet sheet;
CRange range;
CRange iCell;
LPDISPATCH lpDisp;
COleVariant vResult; //COleVariant类是对VARIANT结构的封装
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
if (!app1.CreateDispatch(_T("Excel.Application"), NULL))
{
AfxMessageBox(_T("无法启动Excel服务器!"));
return;
}
books.AttachDispatch(app1.get_Workbooks());
lpDisp = books.Open(strFilePath, 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);
int counts = 0;
for(int i =1;i<200;i++){
range.AttachDispatch(sheet.get_Cells());
range.AttachDispatch(range.get_Item(COleVariant((long)i), COleVariant((long)1)).pdispVal);
vResult = range.get_Value2();
CString str;
if (vResult.vt == VT_BSTR) //字符串
{
str = vResult.bstrVal;
}
else if (vResult.vt == VT_R8) //8字节的数字
{
str.Format(_T("%f"), vResult.dblVal);
}else{
str =" ";
}
locationlist.InsertItem(locationlist.GetItemCount(),str);
for(int j = 1;j<7;j++){
range.AttachDispatch(sheet.get_Cells());
range.AttachDispatch(range.get_Item(COleVariant((long)i), COleVariant((long)j+1)).pdispVal); //第一变量是行,第二个变量是列,即第二行第一列
vResult = range.get_Value2();
CString str;
if (vResult.vt == VT_BSTR) //字符串
{
str = vResult.bstrVal;
}
else if (vResult.vt == VT_R8) //数字
{
str.Format(_T("%f"), vResult.dblVal);
}else {
str = " ";
if(j==5){
return;
}
}
locationlist.SetItemText(locationlist.GetItemCount()-1,j,str);
}
}
UpdateData(false);
books.Close();
app1.Quit();
//释放对象
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
app1.ReleaseDispatch();
到了这里,关于MFC操作excel的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!