環境:VS2013+office2007
頭文件:
1 #pragma once 2 #ifndef __MYEXCEL_H_ 3 #define __MYEXCEL_H_ 4 #include "CApplication.h" 5 #include "CFont0.h" 6 #include "CRange.h" 7 #include "CWorkbook.h" 8 #include "CWorkbooks.h" 9 #include "CWorksheet.h" 10 #include "CWorksheets.h" 11 12 class CMyExcel 13 { 14 public: 15 CMyExcel(); 16 ~CMyExcel(); 17 18 CApplication app; 19 CWorkbook book; 20 CWorkbooks books; 21 CWorksheet sheet; 22 CWorksheets sheets; 23 CRange range; 24 CFont0 font; 25 CRange cols; 26 LPDISPATCH lpDisp; 27 28 void test(); 29 void create_new_excel(CString filename); 30 CString change_name_to_current_path(CString filename); 31 void open_excel(CString filename); 32 void close_excel(); 33 void save_excel(); 34 void set_value(char* pos, char* val); 35 CString get_value(char* pos); 36 }; 37 38 #endif
源文件:
#include "stdafx.h" #include "MyExcel.h" #include <shlwapi.h> #pragma comment(lib,"Shlwapi.lib") //如果沒有這行,會出現link錯誤 CMyExcel::CMyExcel() { } CMyExcel::~CMyExcel() { } void CMyExcel::test() { } //新建excel,通過save as void CMyExcel::create_new_excel(CString path) { //導出 COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); if (!app.CreateDispatch(_T("Excel.Application"))) { AfxMessageBox(_T("無法創建Excel應用!")); return; } books = app.get_Workbooks(); //打開Excel,其中pathname為Excel表的路徑名 book = books.Add(covOptional); //CString path = change_name_to_path(filename); if (!PathFileExists(path)) { book.SaveAs(COleVariant(path), covOptional, covOptional, covOptional, covOptional, covOptional, (long)0, covOptional, covOptional, covOptional, covOptional, covOptional); } //釋放對象(相當重要!) book.ReleaseDispatch(); books.ReleaseDispatch(); //退出程序 app.Quit(); //m_ExlApp一定要釋放,否則程序結束后還會有一個Excel進程駐留在內存中,而且程序重復運行的時候會出錯 app.ReleaseDispatch(); } //將文件名轉化為當前目錄下文件的絕對路徑 CString CMyExcel::change_name_to_current_path(CString filename) { CString theAppPath, theAppName; char Path[MAX_PATH]; GetModuleFileName(NULL, Path, MAX_PATH);//得到應用程序的全路徑 theAppPath = (CString)Path; theAppName = AfxGetApp()->m_pszAppName; theAppName += ".exe"; //把最后的文件名去掉 int length1, length2; length1 = theAppPath.GetLength(); length2 = theAppName.GetLength(); theAppPath.Delete(length1 - length2, length2); CString TempPath = ""; return(theAppPath + filename);//EXCEL模板的路徑 } // void CMyExcel::open_excel(CString path) { COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); if (!app.CreateDispatch(_T("Excel.Application"))) { AfxMessageBox("無法創建Excel應用!"); return; } //CString path = change_name_to_path(filename); books = app.get_Workbooks(); lpDisp = books.Open(path, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional); book.AttachDispatch(lpDisp); } void CMyExcel::save_excel() { book.Save(); } //設置值,pos:指定位置 val:指定值 void CMyExcel::set_value(char* pos, char* val) { sheets = book.get_Worksheets(); sheet = sheets.get_Item(COleVariant((short)1)); //sheet.put_Name("test"); range = sheet.get_Range(COleVariant(_T(pos)), COleVariant(_T(pos))); //獲得坐標為(A,1)單元格 range.put_Value2(COleVariant(_T(val))); //設置單元格類容為Hello Exce cols = range.get_EntireColumn();//選擇整列,並設置寬度為自適應 cols.AutoFit(); //設置字體為粗體 font = range.get_Font(); font.put_Bold(COleVariant((short)TRUE)); //選擇整列,並設置寬度為自適應 cols = range.get_EntireColumn(); cols.AutoFit(); //顯示Excel表 //app.put_Visible(TRUE); //app.put_UserControl(TRUE); } CString CMyExcel::get_value(char* pos) { range = sheet.get_Range(COleVariant(_T(pos)), COleVariant(_T(pos))); //獲得單元格的內容 COleVariant rValue; rValue = COleVariant(range.get_Value2()); //轉換成寬字符 rValue.ChangeType(VT_BSTR); //轉換格式,並輸出 AfxMessageBox(CString(rValue.bstrVal)); return CString(rValue.bstrVal); } void CMyExcel::close_excel() { //釋放對象(相當重要!) range.ReleaseDispatch(); sheet.ReleaseDispatch(); sheets.ReleaseDispatch(); book.ReleaseDispatch(); books.ReleaseDispatch(); //退出程序 app.Quit(); //m_ExlApp一定要釋放,否則程序結束后還會有一個Excel進程駐留在內存中,而且程序重復運行的時候會出錯 app.ReleaseDispatch(); }
使用:
CMyExcel t; //t.test(); CString path = t.change_name_to_current_path("3.xlsx"); t.create_new_excel(path); t.open_excel(path); t.set_value("A3","hello hello"); t.set_value("A8", "hello 123"); t.get_value("A8"); t.save_excel(); t.close_excel();