VS2010/MFC 讀寫excel文件 操作類

         操作類參考一: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 );

           

       

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章