首页 开发技术 C++     /    VC++(VS2010)读写Excel文件代码(附示例)

VC++(VS2010)读写Excel文件代码(附示例)

上传者: ssj021224 | 上传时间:2025/11/15 14:25:49 | 文件大小:281KB | 文件类型:zip
VC++(VS2010)读写Excel文件代码(附示例)
【前言】工作或学习中可能需要实现基于VC读\写Excel文件的功能,本人最近也遇到了该问题。
中间虽经波折,但是最终还是找到了解决问题的办法。
在此跟大家分享,希望对跟我同样迷茫过的同学们有所帮助。
1、程序功能1)打开一个excel文件;2)显示到CListCtrl上;3)新建一个Excel文件。
以上均在对话框中实现。
2、平台VC++20103、实现方法常用的Excel打开方式有两种1)通过数据库打开;2)OLE方式打开。
由于方式1)操作繁琐,经常出现莫名的错误,这里选用方式2).4、准备步骤首先新建一个Dialog窗体程序,添加listcontrol和两个按钮1)将ExcelLib文件夹拷贝到程序目录下;
2)将Export2Excel.h,Export2Excel.cpp两个文件添加到项目;
3)包含头文件,#include"ExcelLib/Export2Excel.h"通过以上步骤在程序中引入了可以读取Excle文件的CExport2Excel类;
5、打开excel文件通过按钮点击打开voidCExcelTestDlg::OnBnClickedButtonOpenExcel(){//获取文件路径CFileDialog*lpszOpenFile;CStringszGetName;lpszOpenFile=newCFileDialog(TRUE,"","",OFN_FILEMUSTEXIST|OFN_HIDEREADONLY,"ExcelFile(*.xlsx;*.xls)|*.xls;*.xlsx",NULL);if(lpszOpenFile->DoModal()==IDOK){szGetName=lpszOpenFile->GetPathName();SetWindowText(szGetName);deletelpszOpenFile;}elsereturn;//打开文件//文件中包含多个sheet时,默认打开第一个sheetCExport2ExcelExcel_example;Excel_example.OpenExcel(szGetName);//获取sheet个数intiSheetNum=Excel_example.GetSheetsNumber();//获取已使用表格行列数intiRows=Excel_example.GetRowCount();intiCols=Excel_example.GetColCount();//获取单元格的内容CStringcs_temp=Excel_example.GetText(1,1);//AfxMessageBox(cs_temp);//Listcontrol上显示//获取工作表列名(第一行)CStringArraym_HeadName;m_HeadName.Add(_T("ID"));for(inti=1;iGetItemCount()>0){m_list.DeleteColumn(0);}//初始化ClistCtrl,加入列名InitList(m_list,m_HeadName);//填入内容//第一行是标题,所以从第2行开始CStringnum;intpos;for(introw=2;row<=iRows;row++){pos=m_list.GetItemCount();num.Format(_T("%d"),pos+1);m_list.InsertItem(pos,num);for(intcolum=1;colum<=iCols;colum++){//插入均从序号0开始m_list.SetItemText(pos,colum,Excel_e 本软件ID:8186823

文件下载

资源详情

[{"title":"(42个子文件281KB)VC++(VS2010)读写Excel文件代码(附示例)","children":[{"title":"ExcelTest","children":[{"title":"附加说明.txt <span style='color:#111;'>4.08KB</span>","children":null,"spread":false},{"title":"ExcelTest.vcxproj.user <span style='color:#111;'>143B</span>","children":null,"spread":false},{"title":"resource.h <span style='color:#111;'>1.51KB</span>","children":null,"spread":false},{"title":"ExcelTestDlg.h <span style='color:#111;'>776B</span>","children":null,"spread":false},{"title":"stdafx.h <span style='color:#111;'>1.59KB</span>","children":null,"spread":false},{"title":"ExcelTest.rc <span style='color:#111;'>10.32KB</span>","children":null,"spread":false},{"title":"ExcelTest.aps <span style='color:#111;'>103.46KB</span>","children":null,"spread":false},{"title":"targetver.h <span style='color:#111;'>234B</span>","children":null,"spread":false},{"title":"ExcelTestDlg.cpp <span style='color:#111;'>6.48KB</span>","children":null,"spread":false},{"title":"ExcelTest.h <span style='color:#111;'>494B</span>","children":null,"spread":false},{"title":"Debug","children":[{"title":"ExcelTest.exe <span style='color:#111;'>222.50KB</span>","children":null,"spread":false}],"spread":true},{"title":"stdafx.cpp <span style='color:#111;'>142B</span>","children":null,"spread":false},{"title":"ExcelTest.suo <span style='color:#111;'>9.00KB</span>","children":null,"spread":false},{"title":"ReadMe.txt <span style='color:#111;'>3.10KB</span>","children":null,"spread":false},{"title":"ExcelTest.cpp <span style='color:#111;'>1.98KB</span>","children":null,"spread":false},{"title":"ExcelTest.vcxproj.filters <span style='color:#111;'>2.16KB</span>","children":null,"spread":false},{"title":"res","children":[{"title":"ExcelTest.rc2 <span style='color:#111;'>674B</span>","children":null,"spread":false},{"title":"ExcelTest.ico <span style='color:#111;'>66.19KB</span>","children":null,"spread":false}],"spread":false},{"title":"ExcelTest.vcxproj <span style='color:#111;'>5.95KB</span>","children":null,"spread":false},{"title":"ExcelLib","children":[{"title":"Export2Excel.h <span style='color:#111;'>4.62KB</span>","children":null,"spread":false},{"title":"Export2Excel.cpp <span style='color:#111;'>25.69KB</span>","children":null,"spread":false},{"title":"Excel","children":[{"title":"CWorksheet.h <span style='color:#111;'>27.11KB</span>","children":null,"spread":false},{"title":"CFont0.h <span style='color:#111;'>5.30KB</span>","children":null,"spread":false},{"title":"CChartTitle.h <span style='color:#111;'>5.33KB</span>","children":null,"spread":false},{"title":"CWorkbooks.h <span style='color:#111;'>7.33KB</span>","children":null,"spread":false},{"title":"CWorksheets.h <span style='color:#111;'>4.45KB</span>","children":null,"spread":false},{"title":"CSeries.h <span style='color:#111;'>14.11KB</span>","children":null,"spread":false},{"title":"CChartObjects.h <span style='color:#111;'>7.88KB</span>","children":null,"spread":false},{"title":"CChartObject.h <span style='color:#111;'>7.75KB</span>","children":null,"spread":false},{"title":"CApplication.h <span style='color:#111;'>67.81KB</span>","children":null,"spread":false},{"title":"CAxis.h <span style='color:#111;'>12.19KB</span>","children":null,"spread":false},{"title":"CTickLabels.h <span style='color:#111;'>4.16KB</span>","children":null,"spread":false},{"title":"CRange.h <span style='color:#111;'>41.90KB</span>","children":null,"spread":false},{"title":"CWindow0.h <span style='color:#111;'>15.97KB</span>","children":null,"spread":false},{"title":"CBorder.h <span style='color:#111;'>2.24KB</span>","children":null,"spread":false},{"title":"CAxisTitle.h <span style='color:#111;'>5.32KB</span>","children":null,"spread":false},{"title":"Cnterior.h <span style='color:#111;'>2.90KB</span>","children":null,"spread":false},{"title":"CWorkbook.h <span style='color:#111;'>36.14KB</span>","children":null,"spread":false},{"title":"CLegend.h <span style='color:#111;'>4.40KB</span>","children":null,"spread":false},{"title":"CBorders.h <span style='color:#111;'>3.24KB</span>","children":null,"spread":false},{"title":"CChart.h <span style='color:#111;'>30.10KB</span>","children":null,"spread":false}],"spread":false}],"spread":false},{"title":"ExcelTest.sln <span style='color:#111;'>884B</span>","children":null,"spread":false}],"spread":false}],"spread":true}]

评论信息

  • KINGWEED:
    能读出来,很好用。就是积分要的多了。2021-04-30
  • wind_45:
    有效果,学习了2021-02-25
  • 妙为:
    效果不错,但是积分要的太多了,还是谢谢楼主2020-09-26
  • lnu2012:
    好资源;可是需要的积分太多了2020-05-15
  • redbud04:
    参考了,很好用,谢谢。2019-07-19

免责申明

【好快吧下载】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【好快吧下载】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【好快吧下载】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,8686821#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明