CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  基础类

VC++操作Excel对象

楼主longbow74()2001-04-04 10:56:00 在 VC/MFC / 基础类 提问

1.我想调用Excel的一个函数  
  _Application   m_app;  
  m_app.CreateDispatch("Excel.Application");  
  哪位大侠知道下面的函数怎么用?  
  m_app.Evaluate()  
  2.如何在VC中生成一个新的Excel?  
  问题点数:20、回复次数:2Top

1 楼xyzboat(一叶小舟)回复于 2001-04-04 11:13:00 得分 20

1.挺复杂的,要看看Office的帮助  
  2.找个例子看看  
  一般你得到了实例以后,就可以调用Workbook,worksheet,等的方法Add。  
  具体,还是看看例子吧,网上很多地方有的.Top

2 楼joybug(笑虫)回复于 2001-06-11 17:00:00 得分 0

HOWTO:   Use   MFC   to   Automate   Excel   and   Create/Format   a   New   Workbook    
   
  --------------------------------------------------------------------------------  
  The   information   in   this   article   applies   to:  
   
  Microsoft   Visual   C++,   32-bit   Editions,   versions   5.0,   6.0    
  Microsoft   Excel   2000    
  The   Microsoft   Foundation   Classes   (MFC)    
  Microsoft   Office   2000   Developer    
  Microsoft   Excel   97   for   Windows  
   
  --------------------------------------------------------------------------------  
   
   
  SUMMARY  
  This   article   shows   how   to   automate   Microsoft   Excel   97   or   Excel   2000   using   the   Microsoft   Foundation   Class   (MFC)   library,   version   4.2   (installed   with   Microsoft   Visual   C++   versions   5.0   and   6.0).    
   
  The   article   describes   a   technique   for   using   OLE   automation   to   create/format   a   Microsoft   Excel   workbook;   it   introduces   several   methods/properties   (from   the   Microsoft   Excel   type   library)   for   adding   data   to   the   worksheet   and   formatting   the   worksheet.  
   
   
   
  Notes   for   Automating   Microsoft   Excel   2000  
  Some   methods   and   properties   have   changed   for   Microsoft   Excel   2000.   For   additional   information   about   using   the   sample   code   described   in   this   article   with   the   Microsoft   Excel   2000   type   library,,   please   click   the   article   number   below   to   view   it   in   the   Microsoft   Knowledge   Base:    
  Q224925   INFO:   Type   Libraries   for   Office   2000   Have   Changed    
   
   
   
  MORE   INFORMATION  
  You   can   copy   the   code   in   this   article   to   the   message   handler   function   of   an   event   defined   in   an   MFC   .cpp   file.   However,   the   purpose   of   the   code   is   to   illustrate   the   process   of   using   the   IDispatch   interfaces   and   member   functions   defined   in   the   Excel8.olb   type   library.   The   primary   benefit   of   this   article,   however,   comes   from   reading   and   understanding   the   code   in   the   example   so   that   you   can   modify   the   example   or   write   your   own   code   to   automate   Microsoft   Excel   97   using   MFC.    
   
  Steps   to   Create   the   Project  
  Follow   steps   1   through   12   in   the   following   Microsoft   Knowledge   Base   article   to   create   a   sample   project   that   uses   the   IDispatch   interfaces   and   member   functions   defined   in   the   Excel8.olb   type   library:  
   
   
  Q178749   HOWTO:   Create   an   Automation   Project   Using   MFC   and   a   Type   Library    
  At   the   top   of   the   AutoProjectDlg.cpp   file,   add   the   following   line:  
   
   
              #include   "excel8.h"    
  Add   the   following   code   to   CAutoProjectDlg::OnRun()   in   the   AutoProjectDLG.cpp   file:  
   
   
  Sample   Code  
              //   Commonly   used   OLE   variants.  
              COleVariant  
                                    covTrue((short)TRUE),  
                                    covFalse((short)FALSE),  
                                    covOptional((long)DISP_E_PARAMNOTFOUND,   VT_ERROR);  
   
              _Application   app;  
              Workbooks   books;  
              _Workbook   book;  
              Worksheets   sheets;  
              _Worksheet   sheet;  
              Range   range;  
              Font   font;  
              Range   cols;  
   
              //   Start   Excel   and   get   Application   object.  
              if(!app.CreateDispatch("Excel.Application"))  
              {  
                AfxMessageBox("Couldn't   start   Excel   and   get   Application   object.");  
                return;  
              }  
   
              //Get   a   new   workbook.  
              books   =   app.GetWorkbooks();  
              book   =   books.Add   (covOptional);  
   
              //Get   the   first   sheet.  
              sheets   =book.GetSheets();  
              sheet   =   sheets.GetItem(COleVariant((short)1));  
   
              //Fill   cells   A1,   B1,   C1,   and   D1   one   cell   at   a   time   with   "headers".  
              range   =   sheet.GetRange(COleVariant("A1"),COleVariant("A1"));  
              range.SetValue(COleVariant("First   Name"));  
              range   =   sheet.GetRange(COleVariant("B1"),COleVariant("B1"));  
              range.SetValue(COleVariant("Last   Name"));  
              range   =   sheet.GetRange(COleVariant("C1"),COleVariant("C1"));  
              range.SetValue(COleVariant("Full   Name"));  
              range   =   sheet.GetRange(COleVariant("D1"),COleVariant("D1"));  
              range.SetValue(COleVariant("Salary"));  
   
              //Format   A1:D1   as   bold,   vertical   alignment   =   center.  
              range   =   sheet.GetRange(COleVariant("A1"),   COleVariant("D1"));  
              font   =   range.GetFont();  
              font.SetBold(covTrue);  
              range.SetVerticalAlignment(  
                                COleVariant((short)-4108));       //xlVAlignCenter   =   -4108  
   
              //Fill   A2:B6   with   an   array   of   values   (First   &   Last   Names).  
              {  
                    COleSafeArray   saRet;  
                    DWORD   numElements[]={5,2};       //5x2   element   array  
                    saRet.Create(VT_BSTR,   2,   numElements);  
   
                    //Fill   the   5x2   SafeArray   with   the   following   data:  
                    //       John             Smith  
                    //       Tom               Brown  
                    //       Sue               Thomas  
                    //       Jane             Jones  
                    //       Adam             Johnson  
   
                    FillSafeArray(L"John",   0,   0,   &saRet);  
                    FillSafeArray(L"Smith",   0,   1,   &saRet);  
                    FillSafeArray(L"Tom",   1,   0,   &saRet);  
                    FillSafeArray(L"Brown",   1,   1,   &saRet);  
                    FillSafeArray(L"Sue",   2,   0,   &saRet);  
                    FillSafeArray(L"Thomas",   2,   1,   &saRet);  
                    FillSafeArray(L"Jane",   3,   0,   &saRet);  
                    FillSafeArray(L"Jones",   3,   1,   &saRet);  
                    FillSafeArray(L"Adam",   4,   0,   &saRet);  
                    FillSafeArray(L"Johnson",   4,   1,   &saRet);  
   
                    range   =   sheet.GetRange(COleVariant("A2"),   COleVariant("B6"));  
                    range.SetValue(COleVariant(saRet));  
   
                    saRet.Detach();  
              }  
   
              //Fill   C2:C6   with   a   relative   formula   (=A2   &   "   "   &   B2).  
              range   =   sheet.GetRange(COleVariant("C2"),   COleVariant("C6"));  
              range.SetFormula(COleVariant("=A2   &   \"   \"   &   B2"));  
   
              //Fill   D2:D6   with   a   formula(=RAND()*100000)   and   apply   a   number  
              //format.  
              range   =   sheet.GetRange(COleVariant("D2"),   COleVariant("D6"));  
              range.SetFormula(COleVariant("=RAND()*100000"));  
              range.SetNumberFormat(COleVariant("$0.00"));  
   
              //AutoFit   columns   A:D.  
              range   =   sheet.GetRange(COleVariant("A1"),   COleVariant("D1"));  
              cols   =   range.GetEntireColumn();  
              cols.AutoFit();  
   
              //Manipulate   a   variable   number   of   columns   for   Quarterly   Sales   Data.  
              {  
                    short   NumQtrs;  
                    CString   msg;  
                    Range   resizedrange;  
                    Interior   interior;  
                    Borders   borders;  
   
                    //Determine   how   many   quarters   to   display   data   for.  
                    for(NumQtrs=1;NumQtrs<=3;NumQtrs++)  
                    {  
                          msg.Format("Enter   sales   data   for   %d   quarter(s)?",   NumQtrs);  
                          if(AfxMessageBox(msg,MB_YESNO)==IDYES)  
                          {  
                                break;  
                          }  
                    }  
                    msg.Format("Displaying   data   for   %d   quarters.",   NumQtrs);  
                    AfxMessageBox(msg);  
   
                    //Starting   at   E1,   fill   headers   for   the   number   of   columns   selected.  
                    range   =   sheet.GetRange(COleVariant("E1"),   COleVariant("E1"));  
                    resizedrange   =   range.GetResize(covOptional,   COleVariant(NumQtrs));  
                    resizedrange.SetFormula(  
                                  COleVariant("=\"Q\"   &   COLUMN()-4   &   CHAR(10)   &   \"Sales\""));  
                    //Change   the   Orientation   and   WrapText   properties   for   the   headers.  
                    resizedrange.SetOrientation(COleVariant((short)38));  
                    resizedrange.SetWrapText(covTrue);  
                    //Fill   the   interior   color   of   the   headers.  
                    interior   =   resizedrange.GetInterior();  
                    interior.SetColorIndex(COleVariant((short)36));  
   
                    //Fill   the   columns   with   a   formula   and   apply   a   number   format.  
                    range   =   sheet.GetRange(COleVariant("E2"),   COleVariant("E6"));  
                    resizedrange   =   range.GetResize(covOptional,   COleVariant(NumQtrs));  
                    resizedrange.SetFormula(COleVariant("=RAND()*100"));  
                    resizedrange.SetNumberFormat(COleVariant("$0.00"));  
   
                    //Apply   borders   to   the   Sales   data   and   headers.  
                    range   =   sheet.GetRange(COleVariant("E1"),   COleVariant("E6"));  
                    resizedrange=   range.GetResize(covOptional,   COleVariant(NumQtrs));  
                    borders   =   resizedrange.GetBorders();  
                    borders.SetWeight(COleVariant((short)2));       //xlThin   =   2  
   
                    //Add   a   Totals   formula   for   the   Quarterly   sales   data   and   apply   a  
                    //border.  
                    range   =   sheet.GetRange(COleVariant("E8"),   COleVariant("E8"));  
                    resizedrange   =   range.GetResize(covOptional,   COleVariant(NumQtrs));  
                    resizedrange.SetFormula(COleVariant("=SUM(E2:E6)"));  
                    borders   =   resizedrange.GetBorders();  
                    {  
                          Border   bottomborder;  
                          bottomborder   =   borders.GetItem((long)9);  
                          bottomborder.SetLineStyle(  
                                                        COleVariant((short)-4119));       //xlDouble   =   -4119  
                          bottomborder.SetWeight(  
                                                        COleVariant((short)4));               //xlThick   =   4  
                    }  
              }  
   
              //Make   the   application   visible   and   give   the   user   control   of  
              //Microsoft   Excel.  
              app.SetVisible(TRUE);  
              app.SetUserControl(TRUE);    
  Add   the   following   function   to   AutoProjectDLG.cpp,   locating   it   somewhere   before   CAutoProjectDlg::OnRun():  
   
   
  Sample   Code  
              void   FillSafeArray(OLECHAR   FAR*   sz,   int   iRow,   int   iCol,  
                                                    COleSafeArray*   sa)  
              {  
                    VARIANT   v;  
                    long   index[2];  
   
                    index[0]   =   iRow;  
                    index[1]   =   iCol;  
   
                    VariantInit(&v);  
                    v.vt   =   VT_BSTR;  
                    v.bstrVal   =   SysAllocString(sz);  
                    sa->PutElement(index,   v.bstrVal);  
                    SysFreeString(v.bstrVal);  
                    VariantClear(&v);  
              }    
  In   Developer   Studio,   run   the   application.   When   the   dialog   box   appears   (the   example   creates   a   dialog-based   application),   click   the   Run   button.  
   
   
   
  Additional   query   words:   Excel   8   8.0   XL97   XL8  
     
  Top

相关问题

  • vc 操作 excel的问题.
  • ★★★★★★★vc操作excel的问题★★★★★★
  • VC如何操作msaccess对象呢?excel到处都有,但为什么没有access的例子呢?
  • 未将对象引用设置到对象实例( 操作Excel)?
  • 用VC怎么操作EXCEL文件???????
  • 在VC中操作excel的问题
  • 请问vc怎么操作excel啊?
  • vc对Excel的操作问题
  • 如何在VC中调用excel对象?
  • vc操作excel时,关于获得excel版本的问题

关键词

  • visual c++
  • excel
  • article
  • microsoft
  • mfc
  • automation
  • workbook
  • 看看
  • app
  • versions

得分解答快速导航

  • 帖主:longbow74
  • xyzboat

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo