VS2010/MFC 读写excel文件 操作类

来源:互联网 发布:知画生孩子是第几集 编辑:程序博客网 时间:2024/05/18 14:43

         操作类参考一:http://blog.csdn.net/superbfly/article/details/18040445

         二维数组操作参考二 : http://www.cnblogs.com/xianyunhe/archive/2011/09/25/2190485.html

         

         1,根据参考一建立操作类,参考一中只能打开现有excel文件,否则报错。我适当修改了下,根据当前机器时钟命名并建立一个excel文件,;

         2,根据参考二添加了写二维int、double数组的功能。

       

          操作类下载地址:http://download.csdn.net/detail/yu_fujiang/9534113


          使用环境:VS2010 / MFC

          使用方法:

          1,将CApplication.h、CWorkbooks.h、CWorkbook.h、CWorksheets.h、CWorksheet.h、CRange.h、OperationExcelFile.h、OpetationExcelFile.cpp几个文件放在程序当前文件夹下;

           2,在C**Dlg.cpp中添加OperationExcelFile.h,同时添加g_excelFile全局变量或者m_excelFile成员变量;

           3,在C**App类对应.cpp文件的CexcelFileTest2App::InitInstance()中添加excel操作类的初始化函数

      //初始化excel操作      OperationExcelFile::InitExcel();
     <pre name="code" class="cpp">      4,给C**App类重写int ExitInstance()函数,并在其中添加操作类的释放函数

<pre name="code" class="cpp">      int CexcelFileTest2App::ExitInstance()      {// TODO: 在此添加专用代码和/或调用基类//excel操作释放OperationExcelFile::ReleaseExcel();return CWinApp::ExitInstance();      }
            5,在需要调用这个函数的地方,添加代码段:

            

        CString filename;CTime tm = CTime::GetCurrentTime();filename = tm.Format(L"%Y%m%d_%H%M%S.xlsx");char pCurPath[100];GetCurrentDirectoryA( 100, pCurPath );filename = CString(pCurPath) + L"\\Log\\" + filename;g_excelFile.OpenExcelFile( filename );g_excelFile.LoadSheet(L"Sheet1",1);g_excelFile.SetCellString(1,1,L"Hello world!");g_excelFile.SetCellDouble(2,1,0.56);g_excelFile.SetCellInt(3,1,10000);int iArray[2][3] = {1,2,3,4,5,6};g_excelFile.SetSheetIntArray(5,1,&iArray[0][0],2,3);double dArray[2][3] = {1.5,2.5,3.05,4.005,5.1,6.3};g_excelFile.SetSheetDoubleArray(10,1,&dArray[0][0],2,3);g_excelFile.SaveasXSLFile( filename );g_excelFile.CloseExcelFile( false );

           

       

1 0