com_excel之CWorkBooks

来源:互联网 发布:电子电路仿真软件下载 编辑:程序博客网 时间:2024/06/07 17:04

Excel Primary Interop Assembly Reference

Workbooks Members

A collection of all the Workbook objects that are currently open in the Microsoft Excel application.

The following tables list the members exposed by the Workbooks type.

Public Properties

 NameDescription_Default ApplicationReturns an Application object that represents the Microsoft Excel application. Read-only.CountReturns the number of objects in the collection. Read-only Integer.CreatorReturns a 32-bit integer that indicates the application in which this object was created. If the object was created in Microsoft Excel, this property returns the string XCEL, which is equivalent to the hexadecimal number 5843454C. Read-onlyXlCreator.ItemReturns a single object from a collection.ParentReturns the parent object for the specified object. Read-only.Top
Public Methods

 NameDescription__OpenText _Open _OpenText _OpenXML AddCreates a new workbook. The new workbook becomes the active workbook. Returns aWorkbook object.CanCheckOutTrue if Microsoft Excel can check out a specified workbook from a server. Read/writeBoolean.CheckOutCopies the specified workbook from a server to a local computer for editing.CloseCloses the object.GetEnumeratorReturns an enumerator to support iterating through the collection.OpenOpens a workbook.OpenDatabaseReturns a Workbook object representing a database.OpenTextLoads and parses a text file as a new workbook with a single sheet that contains the parsed text-file data.OpenXMLOpens an XML data file. Returns a Workbook object.


测试:

COleVariant covTrue((short)TRUE);
COleVariant covFalse((short)FALSE);
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);


Open:打开一个已存在的excel文件

    /*
     *    打开一个excel文件
     **/
    string path = "D:\\test_big_file.xls";
    LPDISPATCH lpdis = books.Open(path.c_str(), covOptional, covOptional, covOptional, covOptional, covOptional,
            covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional,
            covOptional);

    books.Close();

如果文件不存在就报异常


Close:关闭全部workbook,如果想只关闭一个就需要设置workbook的自动关闭宏不启动

Closing a workbook using the Close method doesn't run any Auto_Close macros in the workbook. Use theRunAutoMacros method to run the auto close macros.


Add:新建一个excel文件

    LPDISPATCH Add(VARIANT& Template)

      参数Template

Optional Object. Determines how the new workbook is created. If this argument is a string specifying the name of an existing Microsoft Excel file, the new workbook is created with the specified file as a template. If this argument is a constant, the new workbook contains a single sheet of the specified type. Can be one of the followingXlWBATemplate constants:xlWBATChart, xlWBATExcel4IntlMacroSheet,xlWBATExcel4MacroSheet, orxlWBATWorksheet. If this argument is omitted, Microsoft Excel creates a new workbook with a number of blank sheets (the number of sheets is set by theSheetsInNewWorkbook property).


由于没有导出excel14的枚举值 add的第二种情况没有测试

add的第三种情况 默认: lpdis2 = books.Add(covOptional);


OpenText:打开文本文件

    path = "D:\\test_big_file.txt";
    books.OpenText(path.c_str(), covOptional, covOptional, covOptional, 1, covOptional, covOptional,
            covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional,
            covOptional, covOptional, covOptional);


属性:Item

可以通过get_Item()函数获取值  

可以根据参数index取得books里的指定book对象

ps: 可以用get_Item()和get_count()遍历books集合


books的另外一个测试结果:

Open  Add  OpenText三个函数的区别:

说明:excel服务处理excel文件或是文本文件都需要加载到内存

1:Open 内存对象和磁盘对象(文件)是关联的:改变内存的值,使用cworkbook的save,磁盘文件也会被相应的改变

2:Add 新建,通过名字就知道内存里的文件是新的,使用add的三种用法中的第一种(拷贝磁盘文件的副本到内存, 详细用法请看Add函数的说明),这时,内存里的任何操作与磁盘文件没有半点关系  保存的时候需要用到cworkbook的SaveCopyAs()函数,具体在下一节详说

3:OpenText同Open,但是有一点需要注意的是,opentext加载会非常慢,因为他需要检查没一个项值的属性:eg: 是否用“”包裹,用什么分割域等等。。检查的多了耗时也多,所以不推荐用opentext


因为我也是边学边记录  肯定会有些错误的地方, 我写到的地方大家最好重测一下



原创粉丝点击