码迷,mamicode.com
首页 > 编程语言 > 详细

MFC操作excel

时间:2016-10-24 20:50:47      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:variant   add   释放   code   lap   内容   环境   office   optional   

环境: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();

 

MFC操作excel

标签:variant   add   释放   code   lap   内容   环境   office   optional   

原文地址:http://www.cnblogs.com/karappo/p/5994172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!