Matthew的Blog
IT博客
::
首页
::
新随笔
::
联系
::
聚合
::
管理
::
公告
QQ: 15693996
MSN:protoss0@msn.com
常用链接
我的随笔
我的评论
我参与的随笔
留言簿
(8)
给我留言
查看公开留言
查看私人留言
随笔分类
(26)
Access相关(2)
(rss)
C#编程(3)
(rss)
Delphi编程(13)
(rss)
SQL Server编程(7)
(rss)
编程辅助
(rss)
软件下载(1)
(rss)
随笔档案
(27)
2008年4月 (1)
2007年7月 (1)
2006年7月 (4)
2006年6月 (6)
2006年4月 (15)
相册
My Photos
友情链接
Cynthia个人主页
Dave's blog
Dragon的Blog
最新随笔
1. kerry's blog
2. 程序中动态创建GUID
3. 资料收集工具下载
4. Access中的交叉表
5. Access的一些SQL语法
6. 控制Excel
7. 用SQL语句更改数据库名,表名,列名
8. 我的Delphi多语言处理组件
9. 给窗口上所有的控件设置Caption或Text
10. Delphi中ClientDataSet的使用
积分与排名
积分 - 44892
排名 - 134
阅读排行榜
1. C#中openFileDialog的使用(11806)
2. cxGrid的一些使用方法(5535)
3. Delphi中ClientDataSet的使用(4391)
4. Delphi中结束进程(3892)
5. 用SQL语句更改数据库名,表名,列名(3337)
评论排行榜
1. Delphi中结束进程(10)
2. 资料收集工具下载(3)
3. Delphi中实现全角转半角(2)
4. C#中openFileDialog的使用(1)
5. 我的Delphi多语言处理组件(1)
控制Excel
unit ExcelController;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, DateUtils, ComObj;
type
TExcelController
=
class(TComponent)
private
FxlsApp, FWorkBook: variant;
HaveCreate:
boolean
;
FCenterHeaderText: string;
FCenterFooterText: string;
FCaption: TCaption;
function
BaseNtoChar(n: integer): string;
function
NumberToCol(n: integer): string;
procedure SetCenterHeaderText(const Value: string);
procedure SetCenterFooterText(const Value: string);
procedure SetCaption(const Value: TCaption);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy;
procedure MergeCell(Cell1, Cell2: string) ; overload;
procedure MergeCell(Row1, Col1, Row2, Col2: integer); overload;
procedure SetSheetName(S: string);
procedure AddSheet;
procedure WriteCell(ARow, ACol: integer; AValue: OleVariant);
function
GetCellValue(ARow, ACol: integer): string;
procedure CopySheet(FromSheetIndex, ToSheetIndex: integer);
procedure CopyRange(SourceRange: variant; ToCell: string);
procedure SetSheetIndex(I: integer);
procedure DeleteCol(Col: integer);
procedure DeleteRow(Row: integer);
function
GetCellName(ARow, ACol: integer): string;
function
GetRange(Cell1, Cell2: string): variant; OverLoad;
function
GetRange(Row1, Col1, Row2, Col2: integer): variant; OverLoad;
procedure SetRangeFontName(ARange: variant; FontName: string);
procedure SetRangeFontSize(ARange: variant; FontSize: integer);
procedure SetRangeFontColor(ARange: variant; FontColor: TColor);
procedure SetColWidth(ACol: integer; Width: integer);
procedure SetBorderStyle(Range: variant; LineStyle: integer);
procedure SetRangeRowHeight(Range: variant; Height: integer);
procedure Show;
published
//
标题
property Caption: TCaption read FCaption write SetCaption;
end;
implementation
{ TExcelController }
procedure TExcelController.AddSheet;
begin
FxlsApp.Sheets.Add;
end;
function
TExcelController.BaseNtoChar(n: integer): string;
var
S: string;
begin
case
n of
1
:S:
=
'A';
2
:S:
=
'B';
3
:S:
=
'C';
4
:S:
=
'D';
5
:S:
=
'E';
6
:S:
=
'F';
7
:S:
=
'G';
8
:S:
=
'H';
9
:S:
=
'I';
10
:S:
=
'J';
11
:S:
=
'K';
12
:S:
=
'L';
13
:S:
=
'M';
14
:S:
=
'N';
15
:S:
=
'O';
16
:S:
=
'P';
17
:S:
=
'Q';
18
:S:
=
'R';
19
:S:
=
'S';
20
:S:
=
'T';
21
:S:
=
'U';
22
:S:
=
'V';
23
:S:
=
'W';
24
:S:
=
'X';
25
:S:
=
'Y';
26
:S:
=
'Z';
end;
Result :
=
S;
end;
procedure TExcelController.CopyRange(SourceRange: variant; ToCell: string);
begin
SourceRange.Copy;
FxlsApp.Range[ToCell].PasteSpecial;
end;
procedure TExcelController.CopySheet(FromSheetIndex, ToSheetIndex: integer);
begin
Self.SetSheetIndex(FromSheetIndex);
FxlsApp.ActiveSheet.UsedRange.Copy;
Self.SetSheetIndex(ToSheetIndex);
FxlsApp.ActiveSheet.Range['A1'].PasteSpecial;
end;
constructor TExcelController.Create(AOwner: TComponent);
begin
FxlsApp :
=
CreateOleObject( 'Excel.Application' );
FWorkBook :
=
CreateOleobject('Excel.Sheet');
FWorkBook :
=
FxlsApp.WorkBooks.Add;
HaveCreate :
=
True;
inherited Create(AOwner);
end;
procedure TExcelController.DeleteCol(Col: integer);
begin
FxlsApp.Columns[Col].Delete;
end;
procedure TExcelController.DeleteRow(Row: integer);
begin
FxlsApp.Rows[Row].Delete;
end;
destructor TExcelController.Destroy;
begin
if
HaveCreate then
begin
FWorkBook.Close;
FxlsApp.Quit;
end;
end;
function
TExcelController.GetCellName(ARow, ACol: integer): string;
var
S: string;
begin
S :
=
Self.NumberToCol(ACol);
Result :
=
S
+
IntToStr(ARow);
end;
function
TExcelController.GetCellValue(ARow, ACol: integer): string;
begin
if
not HaveCreate then Exit;
Result :
=
VarToStr(FxlsApp.Cells.Item[ARow, ACol]);
end;
function
TExcelController.GetRange(Row1, Col1, Row2, Col2: integer): variant;
var
Cell1, Cell2: string;
begin
Cell1 :
=
Self.GetCellName(Row1, Col1);
Cell2 :
=
Self.GetCellName(Row2, Col2);
Result :
=
Self.GetRange(Cell1, Cell2);
end;
function
TExcelController.GetRange(Cell1, Cell2: string): variant;
begin
Result :
=
FxlsApp.Range[Cell1
+
':'
+
Cell2];
end;
procedure TExcelController.MergeCell(Row1, Col1, Row2, Col2: integer);
var
S1, S2: string;
begin
S1 :
=
Self.GetCellValue(Row1, Col1);
S2 :
=
Self.GetCellValue(Row2, Col2);
Self.MergeCell(S1, S2);
end;
procedure TExcelController.MergeCell(Cell1, Cell2: string);
var
S: string;
begin
S :
=
Cell1
+
':'
+
Cell2;
FxlsApp.Range[S].HorizontalAlignment :
=
3
;
FxlsApp.Range[S].merge;
end;
function
TExcelController.NumberToCol(n: integer): string;
var
S: string;
x: integer;
begin
if
n
<=
26
then
begin
Result :
=
Self.BaseNToChar(n);
Exit;
end;
if
n
<=
52
then
begin
x :
=
n
-
26
;
Result :
=
'A'
+
Self.BaseNToChar(x);
Exit;
end;
if
n
<=
78
then
begin
x :
=
n
-
52
;
Result :
=
'B'
+
Self.BaseNToChar(x);
Exit;
end;
if
n
<=
104
then
begin
x :
=
n
-
78
;
Result :
=
'C'
+
Self.BaseNToChar(x);
Exit;
end;
if
n
<=
130
then
begin
x :
=
n
-
104
;
Result :
=
'D'
+
Self.BaseNToChar(x);
Exit;
end;
end;
procedure TExcelController.SetBorderStyle(Range: variant; LineStyle: integer);
begin
Range.Borders.LineStyle :
=
LineStyle;
end;
procedure TExcelController.SetCaption(const Value: TCaption);
begin
FCaption :
=
Value;
FxlsApp.Caption :
=
Value;
end;
procedure TExcelController.SetCenterFooterText(const Value: string);
begin
end;
procedure TExcelController.SetCenterHeaderText(const Value: string);
begin
end;
procedure TExcelController.SetColWidth(ACol, Width: integer);
begin
FxlsApp.Columns[ACol].ColumnWidth :
=
Width;
end;
procedure TExcelController.SetRangeFontColor(ARange: variant;
FontColor: TColor);
begin
ARange.Font.Color :
=
FontColor;
end;
procedure TExcelController.SetRangeFontName(ARange: variant; FontName: string);
begin
ARange.Font.Name :
=
FontName;
end;
procedure TExcelController.SetRangeFontSize(ARange: variant; FontSize: integer);
begin
ARange.Font.Size :
=
FontSize;
end;
procedure TExcelController.SetRangeRowHeight(Range: variant; Height: integer);
begin
Range.Rows.RowHeight :
=
Height;
end;
procedure TExcelController.SetSheetIndex(I: integer);
begin
FxlsApp.WorkSheets[I].Activate;
end;
procedure TExcelController.SetSheetName(S: string);
begin
FxlsApp.ActiveSheet.Name :
=
S;
end;
procedure TExcelController.Show;
begin
FxlsApp.Visible :
=
True;
end;
procedure TExcelController.WriteCell(ARow, ACol: integer; AValue: OleVariant);
begin
FxlsApp.Cells(ARow, ACol) :
=
AValue;
end;
end.
posted on 2006-07-05 17:28
matthew
阅读(570)
评论(0)
编辑
收藏
引用
所属分类:
Delphi编程
只有注册用户
登录
后才能发表评论。
Powered by:
IT博客
Copyright © matthew