excel自动化操作

写道
此文章不断更新中……

 

将汉字写入excel中时产生乱码,将wxWidgets更新为2.9.4版本后乱码消失。 

 

#include <wx/msw/ole/automtn.h>


wxAutomationObject excelObject, rangeObject;
if (!excelObject.GetInstance(wxT("Excel.Application")))
{
    // Start Excel if it is not running
    if (!excelObject.CreateInstance(wxT("Excel.Application")))
    {
        wxMessageBox(wxT("Could not create Excel object."));
        return;
    }
}

if (!excelObject.PutProperty(wxT("Visible"), true))
{
    wxMessageBox(wxT("Could not make Excel object visible"));
}
const wxVariant workbooksCountVariant = excelObject.GetProperty(wxT("Workbooks.Count"));
if (workbooksCountVariant.IsNull())
{
    wxMessageBox(wxT("Could not get workbooks count"));
    return;
}
const long workbooksCount = workbooksCountVariant;
if (workbooksCount == 0)
{
    const wxVariant workbook = excelObject.CallMethod(wxT("Workbooks.Add"));
    if (workbook.IsNull())
    {
        wxMessageBox(wxT("Could not create new Workbook"));
        return;
    }
}

//excelObject.PutProperty(wxT("ActiveCell.Font.Bold"), wxVariant(true));
if (!excelObject.PutProperty(wxT("ActiveCell.Value"),wxT("wxWidgets automation test!")))
{
    wxMessageBox(wxT("Could not set active cell value."));
    return;
}
if (!excelObject.PutProperty(wxT("ActiveCell.Font.Name"),wxVariant(wxT("Arial Black"))))//wxVariant(true)) )
{
    wxMessageBox(wxT("Could not put Bold property to active cell."));
    return;
}
//    wxVariant params[2];
//    params[0]=1L;// Row number
//    params[1]=1L;// Column number
//    excelObject.GetObject(rangeObject,wxT("cells"),2,params);
//    rangeObject.PutProperty(wxT("value"),wxVariant(wxT("在固定行列添加属性")));
wxVariant rng[1];
rng[0] = wxVariant(wxT("C3"));
excelObject.GetObject(rangeObject, wxT("Range"), 1,rng);
rangeObject.CallMethod(wxT("Activate"));
excelObject.PutProperty(wxT("ActiveCell.Value"), wxT("在固定行列添加属性"));

 

 

 

参考:

1、http://forums.wxwidgets.org/viewtopic.php?f=32&t=26026

2、http://blog.csdn.net/eaglewood2005/article/details/5289230

3、http://hi.baidu.com/renyucjs/blog/item/d34c4529ab42e23a349bf7f5.html

你可能感兴趣的:(Excel)