OLE操作Excel编译错误处理

OLE操作Excel编译错误处理
  Excel在公司用的很多,而这个东西我用的不是很好,就想用程序来处理,遇到很多错误。这几天研究了下OLE操作Excel。
环境:VS2008 SP1+Excel 2007

加入OLE Type Library
随便建立一个MFC程序,选添加类,Typelib中的MFC类,出来一个对话,可用类型库选“Microsoft Excel 12.0 Object Library”
一般来说,添加下边这些类就够了,当然也可以全部添加
CApplication
CWorkbook
CWorkbooks
CWorksheet
CRange
CWorksheets
CPicture
CPictures
CBorder
CBorders
CFont0
Cnterior

包含头文件的时候,把CApplication.h放到最前面

一个操作的例子
None.gif#define VT(x) _variant_t(x)


void
 CExcelTest2Dlg::OnBnClickedButton1()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    CApplication m_appExcel;       
// Excel应用程序
InBlock.gif
    CWorkbooks m_books;
InBlock.gif    CWorkbook m_book;
InBlock.gif    CWorksheets sheets; 
InBlock.gif    CWorksheet sheet;
InBlock.gif    CRange range;          
//选择范围
InBlock.gif
    Cnterior interior; 
InBlock.gif    CFont0 font;               
// 字体
InBlock.gif
    CBorders borders;         // 边框
InBlock.gif
    CBorder border;
InBlock.gif    CRange column;
InBlock.gif    CRange row;
InBlock.gif    
// 初始化Com
InBlock.gif
    if (::CoInitialize( NULL ) == E_INVALIDARG)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        MessageBox(
"初始化Com失败!");
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// 启动Excel
InBlock.gif
    if ( !m_appExcel.CreateDispatch(_T("Excel.Application"), NULL))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        MessageBox(_T(
"创建Excel失败!"));
InBlock.gif        ::CoUninitialize();
ExpandedSubBlockEnd.gif    }

InBlock.gif    COleVariant covOptional((
long)DISP_E_PARAMNOTFOUND, VT_ERROR);
InBlock.gif    m_appExcel.put_Visible(TRUE);
InBlock.gif    m_books.AttachDispatch(m_appExcel.get_Workbooks()); 
InBlock.gif    m_book.AttachDispatch(m_books.Add(covOptional));
InBlock.gif    sheets.AttachDispatch(m_book.get_Worksheets()); 
//得到Worksheets 
InBlock.gif
    sheet.AttachDispatch(sheets.get_Item(_variant_t("sheet1"))); //得到sheet1 
InBlock.gif
    sheet.put_Name("1234"); //sheet1改名
InBlock.gif    
InBlock.gif
InBlock.gif    
//所有单元格颜色设为白色
InBlock.gif
    range.AttachDispatch(sheet.get_Cells()); 
InBlock.gif    interior.AttachDispatch(range.get_Interior());
InBlock.gif    interior.put_Color(VT(RGB(
255255255)));
InBlock.gif    interior.ReleaseDispatch();
InBlock.gif
InBlock.gif    range.ClearContents();
InBlock.gif    range.ReleaseDispatch();
InBlock.gif
InBlock.gif    range.AttachDispatch(sheet.get_Range(VT(
"A1"), VT("C1001")));
InBlock.gif    range.ClearFormats();
InBlock.gif    
//插入数据
InBlock.gif
    range.put_Item(VT(1), VT(1), VT("函数"));
InBlock.gif    range.put_Item(VT(
1), VT(2), VT("大项目"));
InBlock.gif    range.put_Item(VT(
1), VT(3), VT("小项目"));
InBlock.gif    
for (int i = 2; i < 1000; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        range.put_Item(VT(i), VT(
2), VT(i - 1));
InBlock.gif        range.put_Item(VT(i), VT(
3), VT("37122368~37097735~"));
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
// 为四周和内部加上边框
InBlock.gif
    borders.AttachDispatch(range.get_Borders());
InBlock.gif    
for (long i = xlEdgeLeft; i <= xlInsideHorizontal; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        border 
= borders.get_Item(i);
InBlock.gif        border.put_LineStyle(VT(xlContinuous));
InBlock.gif        border.ReleaseDispatch();
ExpandedSubBlockEnd.gif    }

InBlock.gif    borders.ReleaseDispatch();
InBlock.gif
InBlock.gif    
//调整列宽
InBlock.gif
    column = range.get_EntireColumn();
InBlock.gif    column.put_ColumnWidth(VT(
18.63));
InBlock.gif    column.ReleaseDispatch();
InBlock.gif    range.ReleaseDispatch();
InBlock.gif
InBlock.gif    range.AttachDispatch(sheet.get_Range(VT(
"A10"), VT("A20"))); //选中
InBlock.gif
    range.Merge(VT(0)); //合并单元格
InBlock.gif
    range.ReleaseDispatch();
InBlock.gif
InBlock.gif    range.AttachDispatch(sheet.get_Range(VT(
"A1"), VT("C1")));
InBlock.gif    interior.AttachDispatch(range.get_Interior());
InBlock.gif    interior.put_ColorIndex(VT(
7));
InBlock.gif    interior.put_Pattern(VT(xlPatternSolid));
InBlock.gif    interior.ReleaseDispatch();
InBlock.gif
InBlock.gif    font.AttachDispatch(range.get_Font());
InBlock.gif    font.put_ColorIndex(VT(
6));
InBlock.gif    font.get_Bold();
InBlock.gif    font.ReleaseDispatch();
InBlock.gif    range.ReleaseDispatch();
InBlock.gif
InBlock.gif    range.AttachDispatch(sheet.get_Range(VT(
"A2"), VT("C1001")));  //设置range对象的范围
InBlock.gif
    interior.AttachDispatch(range.get_Interior());  //选择表格内部
InBlock.gif
    interior.put_ColorIndex(VT(13));   //颜色
InBlock.gif
    interior.put_Pattern(VT(xlPatternSolid));  //加粗
InBlock.gif
    interior.ReleaseDispatch();
InBlock.gif
InBlock.gif    font.AttachDispatch(range.get_Font());  
//选择字
InBlock.gif
    font.put_ColorIndex(VT(3));  //设置字颜色
InBlock.gif
    font.ReleaseDispatch();
InBlock.gif    
InBlock.gif    row.AttachDispatch(range.get_EntireRow()); 
//选择range里的全部行
InBlock.gif
    row.put_RowHeight(VT(24));  //行高
InBlock.gif
    row.ReleaseDispatch();
InBlock.gif    range.ReleaseDispatch();
InBlock.gif
InBlock.gif    sheet.ReleaseDispatch();
InBlock.gif    sheets.ReleaseDispatch();
InBlock.gif    m_book.ReleaseDispatch(); 
InBlock.gif    m_books.ReleaseDispatch();
InBlock.gif    m_appExcel.ReleaseDispatch();
InBlock.gif
InBlock.gif    
ExpandedBlockEnd.gif}

接下来开始编译,哇,一大堆错误...

e:\myprograms3\exceltest2\exceltest2\debug\excel.tlh(1461) : error C2371: “FontPtr”: 重定义;不同的基类型   等等
把CApplication.h里的#import "C:\\Program Files\\Microsoft Office\\Office12\\EXCEL.EXE" no_namespace
改为#import "C:\Program Files\Microsoft Office\OFFICE12\excel.exe" exclude("IFont", "IPicture") rename("RGB", "ignorethis"), rename("DialogBox", "ignorethis"), rename("VBE", "GREATWSVBE")

rebuild all
e:\myprograms3\exceltest2\exceltest2\debug\excel.tlh(2036) : error C2504: “_IMsoDispObj”: 未定义基类
应该是接口没定义,mso.dll这个接口,在CApplication.h最上面加入#import "C:\Program Files\Common Files\Microsoft Shared\OFFICE12\mso.dll"

rebuild all  错误少多了
还有VBE之类的错误,改法:
加入
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" raw_interfaces_only, rename("Reference", "ignorethis"), rename("VBE", "GREATWSVBE")

注意自己机器的路径!
剩下还有一些rename 的警告,可以在相关import后面几上rename("XXX","XXXXX")


本人也是初学OLE操作excel,希望能和大家一起讨论。





posted on 2008-09-21 23:01 greatws 阅读(9255) 评论(4)   编辑  收藏 引用

你可能感兴趣的:(OLE操作Excel编译错误处理)