java ole excel_用OLE操作Excel

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

PG(

"

workbooks

"

)

.

FN(

"

Add

"

,

"

C:\\Temp\\result.xlt

"

);

4f1150b881333f12a311ae9ef34da474.png

Range

=

ExcelApp

.

PG(

"

Cells

"

,

2

,

1

);

4f1150b881333f12a311ae9ef34da474.png

Range

.

PS(

"

RowHeight

"

,

25

);

在应用程序中控制Excel的运行,首先必须在编制自动化客户程序时包含Comobj.hpp

#include "Comobj.hpp"

C++ Builder把Excel自动化对象的功能包装在下面的四个Ole Object Class函数中,应用人员可以很方便地进行调用。

设置对象属性:void        OlePropertySet(属性名,参数……);

获得对象属性:Variant     OlePropertyGet(属性名,参数……);

调用对象方法:1) Variant  OleFunction(函数名,参数……);

2) void     OleProcedure(过程名,参数……);

在程序中可以用宏定义来节省时间:

4f1150b881333f12a311ae9ef34da474.png

#

define   PG   OlePropertyGet

4f1150b881333f12a311ae9ef34da474.png#

define   PS   OlePropertySet

4f1150b881333f12a311ae9ef34da474.png#

define   FN   OleFunction

4f1150b881333f12a311ae9ef34da474.png#

define   PR   OleProcedure

举例:

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

OlePropertyGet(

"

workbooks

"

)

.

OleFunction(

"

Add

"

);

4f1150b881333f12a311ae9ef34da474.png

可写为

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

PG(

"

workbooks

"

)

.

FN(

"

Add

"

);

C++ Builder中使用OLE控制Excel2000,必须掌握Excel2000的自动化对象及Microsoft Word Visual Basic帮助文件中的关于Excel的对象、方法和属性。对象是一个Excel元素,属性是对象的一个特性或操作的一个方面,方法是对象可以进行的动作。

首先定义以下几个变量:

Variant ExcelApp,Workbook1,Sheet1,Range1;

1、Excel中常用的对象是:Application,Workbooks,Worksheets等。

★创建应用对象★

4f1150b881333f12a311ae9ef34da474.png

Variant ExcelApp;

4f1150b881333f12a311ae9ef34da474.png       ExcelApp

=

Variant

::

CreateObject (

"

Excel.Application

"

);

或者

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

=

CreateOleObject (

"

Excel.Application

"

);

★创建工作簿对象★

4f1150b881333f12a311ae9ef34da474.png

Variant WorkBook1;

4f1150b881333f12a311ae9ef34da474.png       WorkBook1

=

ExcelApp

.

PG(

"

ActiveWorkBook

"

);

★创建工作表对象★

4f1150b881333f12a311ae9ef34da474.png

Variant Sheet1;

4f1150b881333f12a311ae9ef34da474.png       Sheet1

=

WorkBook1

.

PG(

"

ActiveSheet

"

);

★创建区域对象★

Variant Range;

4f1150b881333f12a311ae9ef34da474.png

Range

=

Sheet1

.

PG(

"

Range

"

,

"

A1:A10

"

);

或者使用

4f1150b881333f12a311ae9ef34da474.png

Excel

.

Exec

(PropertyGet(

"

Range

"

)

<<

"

A1:C1

"

)

.

Exec

(Procedure(

"

Select

"

));

2、常用的属性操作:

★使Excel程序不可见★

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

PS(

"

Visible

"

,

(Variant)

false

);

★新建EXCEL文件★

◎ 新建系统模板的工作簿

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

PG(

"

workbooks

"

)

.

FN(

"

Add

"

)

//

默认工作簿

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

PG(

"

workbooks

"

)

.

FN(

"

Add

"

,

1

)

//

单工作表

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

PG(

"

workbooks

"

)

.

FN(

"

Add

"

,

2

)

//

图表

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

PG(

"

workbooks

"

)

.

FN(

"

Add

"

,

3

)

//

宏表

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

PG(

"

workbooks

"

)

.

FN(

"

Add

"

,

4

)

//

国际通用宏表

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

PG(

"

workbooks

"

)

.

FN(

"

Add

"

,

5

)

//

与默认的相同

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

PG(

"

workbooks

"

)

.

FN(

"

Add

"

,

6

)

//

工作簿且只有一个表

4f1150b881333f12a311ae9ef34da474.png

或者使用ExcelApp的Exec方法

4f1150b881333f12a311ae9ef34da474.png       Excel

.

Exec

(PropertyGet(

"

Workbooks

"

))

.

Exec

(Procedure(

"

Add

"

));

◎ 新建自己创建的模板的工作簿

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

PG(

"

workbooks

"

)

.

FN(

"

Add

"

,

"

C:\\Temp\\result.xlt

"

);

★打开工作簿★

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

PG(

"

workbooks

"

)

.

FN(

"

open

"

,

"

路径名.xls

"

)

★保存工作簿★

4f1150b881333f12a311ae9ef34da474.png

WorkBook1

.

FN(

"

Save

"

);

//

保存工作簿

4f1150b881333f12a311ae9ef34da474.png

WorkBook1

.

FN(

"

SaveAs

"

,

"

文件名

"

);

//

工作簿保存为,路径注意用"\\"

★退出EXCEL★

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

FN (

"

Quit

"

);

4f1150b881333f12a311ae9ef34da474.png       ExcelApp

=

Unassigned;

或者

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

Exec

(Procedure(

"

Quit

"

));

★操作工作表★

◎ 选择选择工作表中第一个工作表

4f1150b881333f12a311ae9ef34da474.png

Workbook1

.

PG(

"

Sheets

"

,

1

)

.

PR(

"

Select

"

);

4f1150b881333f12a311ae9ef34da474.png       Sheet1

=

Workbook1

.

PG(

"

ActiveSheet

"

);

◎ 重命名工作表

4f1150b881333f12a311ae9ef34da474.png

Sheet1

.

PS(

"

Name

"

,

"

Sheet的新名字

"

);

◎ 当前工作簿中的工作表总数

4f1150b881333f12a311ae9ef34da474.png

int nSheetCount

=

Workbook1

.

PG(

"

Sheets

"

)

.

PG(

"

Count

"

);

4f1150b881333f12a311ae9ef34da474.png

★操作行和列★

◎ 获取当前工作表中有多少行和多少列:

4f1150b881333f12a311ae9ef34da474.png

Sheet1

.

PG(

"

UsedRange

"

)

.

PG(

"

Columns

"

)

.

PG(

"

Count

"

);

//

列数

4f1150b881333f12a311ae9ef34da474.png

Sheet1

.

PG(

"

UsedRange

"

)

.

PG(

"

Rows

"

)

.

PG(

"

Count

"

);

//

行数

◎ 设置列宽

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

PG(

"

Columns

"

,

1

)

.

PS(

"

ColumnWidth

"

,

22

);

或者

4f1150b881333f12a311ae9ef34da474.png

Range

=

ExcelApp

.

PG(

"

Cells

"

,

1

,

3

);

4f1150b881333f12a311ae9ef34da474.png

Range

.

PS(

"

ColumnWidth

"

,

22

);

◎ 设置行高

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

PG(

"

Rows

"

,

2

)

.

PS(

"

RowHeight

"

,

25

);

或者

4f1150b881333f12a311ae9ef34da474.png

Range

=

ExcelApp

.

PG(

"

Cells

"

,

2

,

1

);

4f1150b881333f12a311ae9ef34da474.png

Range

.

PS(

"

RowHeight

"

,

25

);

◎ 在工作表最前面插入一行

4f1150b881333f12a311ae9ef34da474.png

Sheet1

.

PG(

"

Rows

"

,

1

)

.

PR(

"

Insert

"

);

◎ 删除一行

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

PG(

"

Rows

"

,

2

)

.

PR(

"

Delete

"

);

//

将第2行删除

// 本文作者:ccrun ,如转载请保证本文档的完整性,并注明出处。

// 欢迎光临 C++ Builder 研究

www.ccrun.com

// 摘自:http://www.ccrun.com/doc/go.asp?id=529

★操作单元格★

◎ 设置单元格字体

4f1150b881333f12a311ae9ef34da474.png

Sheet1

.

PG(

"

Cells

"

,

1

,

1

)

.

PG(

"

Font

"

)

.

PS(

"

Name

"

,

"

隶书

"

);

//

字体

4f1150b881333f12a311ae9ef34da474.png

Sheet1

.

PG(

"

Cells

"

,

2

,

3

)

.

PG(

"

Font

"

)

.

PS(

"

size

"

,

28

);

//

大小

◎ 设置所选区域字体

4f1150b881333f12a311ae9ef34da474.png

Range

.

PG(

"

Cells

"

)

.

PG(

"

Font

"

)

.

PS(

"

Size

"

,

28

);

4f1150b881333f12a311ae9ef34da474.png

Range

.

PG(

"

Cells

"

)

.

PG(

"

Font

"

)

.

PS(

"

Color

"

,

RGB(

0

,

0

,

255

));

其中参数的设置:

4f1150b881333f12a311ae9ef34da474.png

Font   Name

:

"

隶书

"

//

字体名称

4f1150b881333f12a311ae9ef34da474.png

Size

:

12

//

字体大小

4f1150b881333f12a311ae9ef34da474.png

Color

:

RGB(

*,*,*

)

//

颜色

4f1150b881333f12a311ae9ef34da474.png

Underline

:

true

/

false

//

下划线

4f1150b881333f12a311ae9ef34da474.png

Italic

:

true

/

false

//

斜体

◎ 设置单元格格式为小数百分比

4f1150b881333f12a311ae9ef34da474.png

Sheet1

.

PG(

"

Cells

"

,

1

,

1

)

.

PS(

"

NumberFormatLocal

"

,

"

0.00%

"

);

◎ 设定单元格的垂直对齐方式

4f1150b881333f12a311ae9ef34da474.png

Range

=

ExcelApp

.

PG(

"

Cells

"

,

3

,

4

);

4f1150b881333f12a311ae9ef34da474.png

//

1=靠上 2=居中 3=靠下对齐 4=两端对齐 5=分散对齐

4f1150b881333f12a311ae9ef34da474.png

Range

.

PS(

"

VerticalAlignment

"

,

2

);

◎ 设定单元格的文本为自动换行

4f1150b881333f12a311ae9ef34da474.png

Range

=

ExcelApp

.

PG(

"

Cells

"

,

3

,

4

);

4f1150b881333f12a311ae9ef34da474.png

Range

.

PS(

"

WrapText

"

,

true

);

★单元格的合并★

4f1150b881333f12a311ae9ef34da474.png

Range

=

Sheet1

.

PG(

"

Range

"

,

"

A1:A2

"

);

//

A1和A2单元格合并

4f1150b881333f12a311ae9ef34da474.png

String

strRange

=

"

A

"

+

IntToStr(j)

+

"

:

"

+

"

C

"

+

IntToStr(j);

//

比如:A1:C5

4f1150b881333f12a311ae9ef34da474.png

Range1

=

Sheet1

.

PG(

"

Range

"

,

strRange

.

c_str());

//

可以用变量控制单元格合并

4f1150b881333f12a311ae9ef34da474.png

Range1

.

FN(

"

Merge

"

,

false

);

★读写单元格★

◎ 指定单元格赋值

4f1150b881333f12a311ae9ef34da474.png

String

strValue

=

"

abcdefg

"

;

4f1150b881333f12a311ae9ef34da474.png       Sheet1

.

PG(

"

Cells

"

,

3

,

6

)

.

PS(

"

Value

"

,

strValue

.

c_str());

4f1150b881333f12a311ae9ef34da474.png       Sheet1

.

PG(

"

Cells

"

,

j

,

1

)

.

PS(

"

Value

"

,

"

总记录:

"

+

String

(j

-

6

));

4f1150b881333f12a311ae9ef34da474.png       或者使用

4f1150b881333f12a311ae9ef34da474.png

Excel

.

Exec

(PropertyGet(

"

Cells

"

)

<<

1

<<

3

)

.

Exec

(PropertySet(

"

Value

"

)

<<

15

);

◎ 所选区域单元格赋值

4f1150b881333f12a311ae9ef34da474.png

Range

.

PG(

"

Cells

"

)

.

PS(

"

Value

"

,

10

);

◎ 所选区域行赋值

4f1150b881333f12a311ae9ef34da474.png

Range

.

PG(

"

Rows

"

,

1

)

.

PS(

"

Value

"

,

1234

);

◎ 工作表列赋值

4f1150b881333f12a311ae9ef34da474.png

Sheet1

.

PG(

"

Columns

"

,

1

)

.

PS(

"

Value

"

,

1234

);

◎ 读取取值语句:

4f1150b881333f12a311ae9ef34da474.png

String

strValue

=

Sheet1

.

PG(

"

Cells

"

,

3

,

5

)

.

PG(

"

Value

"

);

★窗口属性★

◎ 显示属性

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

PS(

"

Windowstate

"

,

3

);

//

最大化显示

4f1150b881333f12a311ae9ef34da474.png

1

---------

xlNormal

//

正常显示

4f1150b881333f12a311ae9ef34da474.png

2

---------

xlMinimized

//

最小化显示

4f1150b881333f12a311ae9ef34da474.png

3

---------

xlMaximized

//

最大化显示

◎ 状态栏属性

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

PS(

"

StatusBar

"

,

"

您好,请您稍等。正在查询!

"

);

4f1150b881333f12a311ae9ef34da474.png       ExcelApp

.

PS(

"

StatusBar

"

,

false

);

//

还原成默认值

◎ 标题属性:

4f1150b881333f12a311ae9ef34da474.png

ExcelApp

.

PS(

"

Caption

"

,

"

查询系统

"

);

4f1150b881333f12a311ae9ef34da474.png

3、操作图表

★添加图表

4f1150b881333f12a311ae9ef34da474.png

Variant Chart;

4f1150b881333f12a311ae9ef34da474.png     Chart

=

ExcelApp

.

Exec

(PropertyGet(

"

Charts

"

))

.

Exec

(

Function

(

"

Add

"

));

4f1150b881333f12a311ae9ef34da474.png     ExcelApp

.

Exec

(PropertySet(

"

Visible

"

)

<<

true

);

4f1150b881333f12a311ae9ef34da474.png     Chart

.

Exec

(PropertySet(

"

Type

"

)

<<

-

4100

);

★滚动图表

4f1150b881333f12a311ae9ef34da474.png

for

(int nRotate

=

5

; nRotate

<=

180

; nRotate

+=

5

)

4f1150b881333f12a311ae9ef34da474.png     {

4f1150b881333f12a311ae9ef34da474.png          Chart

.

Exec

(PropertySet(

"

Rotation

"

)

<<

nRotate);

4f1150b881333f12a311ae9ef34da474.png     }

4f1150b881333f12a311ae9ef34da474.png

for

(int nRotate

=

175

; nRotate

>=

0

; nRotate

-=

5

)

4f1150b881333f12a311ae9ef34da474.png     {

4f1150b881333f12a311ae9ef34da474.png          Chart

.

Exec

(PropertySet(

"

Rotation

"

)

<<

nRotate);

4f1150b881333f12a311ae9ef34da474.png     }

另外,为保证程序能正常运行,需要在程序中判断目标机器是否安装了Office;

4f1150b881333f12a311ae9ef34da474.png

try

4f1150b881333f12a311ae9ef34da474.png{

4f1150b881333f12a311ae9ef34da474.png    ExcelApp

=

Variant

::

CreateObject (

"

Excel.Application

"

);

4f1150b881333f12a311ae9ef34da474.png}

4f1150b881333f12a311ae9ef34da474.png

catch

(

9b8a8a44dd1c74ae49c20a7cd451974e.png

)

4f1150b881333f12a311ae9ef34da474.png{

4f1150b881333f12a311ae9ef34da474.png    ShowMessage(

"

运行Excel出错,请确认安装了Office

"

);

4f1150b881333f12a311ae9ef34da474.png

return

;

4f1150b881333f12a311ae9ef34da474.png}

4f1150b881333f12a311ae9ef34da474.png

#

include "comobj.hpp"

4f1150b881333f12a311ae9ef34da474.png//---------------------------------------------------------------------------

4f1150b881333f12a311ae9ef34da474.png// 对指定Excel文件中的指定列进行排序

4f1150b881333f12a311ae9ef34da474.png// strExcelFileName : excel文件名

4f1150b881333f12a311ae9ef34da474.png// nCol : 指定的列号

4f1150b881333f12a311ae9ef34da474.png// nSortStyle : 1:升序,2:降序

4f1150b881333f12a311ae9ef34da474.png

void SortExcelColumn(

String

strExcelFileName

,

int nCol

,

int nSortStyle)

4f1150b881333f12a311ae9ef34da474.png{

4f1150b881333f12a311ae9ef34da474.png    Variant vExcelApp

,

vWorkbook

,

vRange;

4f1150b881333f12a311ae9ef34da474.png    vExcelApp

=

Variant

::

CreateObject(

"

Excel.Application

"

);

4f1150b881333f12a311ae9ef34da474.png    vExcelApp

.

OlePropertySet(

"

Visible

"

,

false

);

4f1150b881333f12a311ae9ef34da474.png    vExcelApp

.

OlePropertyGet(

"

WorkBooks

"

)

.

OleProcedure(

"

Open

"

,

strExcelFileName

.

c_str());

4f1150b881333f12a311ae9ef34da474.png    vWorkbook

=

vExcelApp

.

OlePropertyGet(

"

ActiveWorkbook

"

);

4f1150b881333f12a311ae9ef34da474.png    vExcelApp

.

OlePropertyGet(

"

Columns

"

,

nCol)

.

OleProcedure(

"

Select

"

);

4f1150b881333f12a311ae9ef34da474.png    vExcelApp

.

OlePropertyGet(

"

ActiveSheet

"

)

.

OlePropertyGet(

"

Cells

"

,

1

,

nCol)

.

OleProcedure(

"

Select

"

);

4f1150b881333f12a311ae9ef34da474.png    vRange

=

vExcelApp

.

OlePropertyGet(

"

Selection

"

);

4f1150b881333f12a311ae9ef34da474.png    vRange

.

Exec

(

Function

(

"

Sort

"

)

<<

vExcelApp

.

OlePropertyGet(

"

Selection

"

)

<<

nSortStyle);

4f1150b881333f12a311ae9ef34da474.png    vWorkbook

.

OleProcedure(

"

Save

"

);

4f1150b881333f12a311ae9ef34da474.png    vWorkbook

.

OleProcedure(

"

Close

"

);

4f1150b881333f12a311ae9ef34da474.png    vExcelApp

.

OleFunction(

"

Quit

"

);

4f1150b881333f12a311ae9ef34da474.png    vWorkbook

=

Unassigned;

4f1150b881333f12a311ae9ef34da474.png    vExcelApp

=

Unassigned;

4f1150b881333f12a311ae9ef34da474.png    ShowMessage(

"

ok

"

);

4f1150b881333f12a311ae9ef34da474.png}

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.pngvoid __fastcall TForm1

::

Button1Click(TObject

*

Sender)

4f1150b881333f12a311ae9ef34da474.png{

4f1150b881333f12a311ae9ef34da474.png

//

对C:\123\123.xls文件中第一个Sheet的第四列进行升序排序

4f1150b881333f12a311ae9ef34da474.png

SortExcelColumn(

"

C:\\123\\123.xls

"

,

4

,

1

);

4f1150b881333f12a311ae9ef34da474.png}

4f1150b881333f12a311ae9ef34da474.png

from:http://www.itepub.net/html/kaifawendang/C__/OLE_COM_DLL_XML/2006/0429/6995.html

posted on 2006-09-10 20:05 young 阅读(308) 评论(0)  编辑  收藏 所属分类: C++Builder

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值