unit Unit1;
//cityboat test2008-05-07
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, ExcelXP, OleServer,OleDB, ADODB, DB, DBClient;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
ExcelApplication1: TExcelApplication;
ExcelWorkbook1: TExcelWorkbook;
ExcelWorksheet1: TExcelWorksheet;
ADOConnection1: TADOConnection;
ADOTable1: TADOTable;
ADOQuery1: TADOQuery;
ClientDataSet1: TClientDataSet;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
//—————————————————————————-//
//成功提示框
//—————————————————————————-//
procedure SuccessMsgbox(AMsg: String);
begin
Application.MessageBox(Pchar(AMsg), ‘完成’, MB_ICONINFORMATION + MB_OK);
end;
//—————————————————————————-//
//错误提示框
//—————————————————————————-//
procedure ErrorMsgbox(AMsg: String);
begin
Application.MessageBox(Pchar(AMsg), ‘错误’, MB_ICONSTOP + MB_OK);
end;
//—————————————————————————-//
//询问提示框
//—————————————————————————-//
function AskMsgbox(AMsg: String): Boolean;
begin
if Application.MessageBox(Pchar(AMsg), ‘确认’,
MB_ICONQUESTION + MB_YESNO) = IDYES then
begin
result := true;
end
else begin
result := false;
end;
end;
//—————————————————————————-//
//消息提示框
//—————————————————————————-//
procedure InfoMsgbox(AMsg: String);
begin
Application.MessageBox(Pchar(AMsg), ‘提示’, MB_ICONINFORMATION + MB_OK);
end;
//—————————————————————————-//
//取得某一列数据的最大长度
//—————————————————————————-//
function GetColMaxDataLength(ASGrid: TStringGrid; ACol, AStartRow: Integer): Integer;
var
ColIndex, RowIndex: Integer;
MaxColLength: Integer; //列数据的最大长度
begin
MaxColLength := 0;
with ASGrid do
begin
//取得列数据的最大长度
for RowIndex := AStartRow to RowCount – 1 do
begin
if length(Cells[ACol, RowIndex]) > MaxColLength then
begin
MaxColLength:= length(Cells[ACol, RowIndex]);
end;
end;
end;
result := MaxColLength;
end;
//—————————————————————————-//
//根据数据长度自动设置指定列的列宽
//—————————————————————————-//
procedure SetOneColWidth(ASGrid: TStringGrid; ACol: Integer);
var
OneCharPixel: Integer; //一个字符所占的像素数
RightSpaceWidth: Integer; //右边距空隙
begin
RightSpaceWidth := 3; //设置为3达到和左边距一致的效果
OneCharPixel := 6; //6对应9号字[*此处最好写成一个根据字号获得像素值的函数*]
ASGrid.ColWidths[ACol] := GetColMaxDataLength(ASGrid, ACol, 0) * OneCharPixel
+ RightSpaceWidth;
end;
//—————————————————————————-//
//根据数据长度自动设置全部列的列宽
//—————————————————————————-//
procedure SetAllColWidth(ASGrid: TStringGrid);
var
ColIndex: Integer; //需要设置的列
begin
for ColIndex := 0 to ASGrid.ColCount – 1 do
begin
SetOneColWidth(ASGrid, ColIndex);
end;
end;
//—————————————————————————-//
//显示ClientDataSet中的数据
//—————————————————————————-//
procedure ShowClientDataSetData(ASGrid: TStringGrid; ACDSet: TClientDataSet;
AGridStartCol, AGridStartRow: Integer);
var
ColIndex: Integer;
RowIndex: Integer;
begin
try
with ASGrid do
begin
//没有记录时,清空StringGrid并返回
if ACDSet.RecordCount <= 0 then
begin
RowCount := 2;
for ColIndex := 0 to ColCount - 1 do
begin
Cells[ColIndex, 1] := '';
end;
exit;
end;
RowCount := AGridStartRow + ACDSet.RecordCount; //StringGrid行数
ColCount := AGridStartCol + ACDSet.FieldCount; //StringGrid列数
RowIndex := AGridStartRow; //当前行为起始行
while not ACDSet.Eof do
begin
//显示数据
for ColIndex := AGridStartCol to ColCount - 1 do
begin
Cells[ColIndex, RowIndex]
:= ACDSet.Fields.Fields[ColIndex - AGridStartCol].AsString
end;
//转到下一行
RowIndex := RowIndex + 1;
ACDSet.Next;
end;
end;
except
On e: Exception do
begin
ErrorMsgBox(e.Message);
end;
end;
end;
//----------------------------------------------------------------------------//
//显示ADOQuery中的数据
//----------------------------------------------------------------------------//
procedure ShowQueryData(ASGrid: TStringGrid; AQuery: TADOQuery;
AGridStartCol, AGridStartRow: Integer);
var
ColIndex: Integer;
RowIndex: Integer;
begin
try
with ASGrid do
begin
//没有记录时,清空StringGrid并返回
if AQuery.RecordCount <= 0 then
begin
RowCount := 2;
for ColIndex := 0 to ColCount - 1 do
begin
Cells[ColIndex, 1] := '';
end;
exit;
end;
RowCount := AGridStartRow + AQuery.RecordCount; //StringGrid行数
ColCount := AGridStartCol + AQuery.FieldCount; //StringGrid列数
RowIndex := AGridStartRow; //当前行为起始行
while not AQuery.Eof do
begin
//显示数据
for ColIndex := AGridStartCol to ColCount - 1 do
begin
Cells[ColIndex, RowIndex]
:= AQuery.Fields.Fields[ColIndex - AGridStartCol].AsString
end;
//转到下一行
RowIndex := RowIndex + 1;
AQuery.Next;
end;
end;
except
On e: Exception do
begin
ErrorMsgBox(e.Message);
end;
end;
end;
//----------------------------------------------------------------------------//
//判断是否含有数据
//----------------------------------------------------------------------------//
function HaveData(ASGrid: TStringGrid; AStartCol, AStartRow: Integer): Boolean;
var
ColIndex, RowIndex: Integer;
begin
with ASgrid do
begin
for ColIndex := AStartCol to ColCount - 1 do
begin
for RowIndex := AStartRow to RowCount - 1 do
begin
//包含数据,返回true
if Cells[ColIndex, RowIndex] <> ” then
begin
result := true;
exit;
end;
end;
end;
end;
//没有数据,返回false
result := false;
end;
//—————————————————————————-//
//取得9位以内整数位数
//—————————————————————————-//
function GetIntegerNumberLength(ANumber: Integer): Integer;
var
IsNegativeNumber: Boolean; //参数的正负,负数为true
LoopIndex: Integer; //循环变量
ComporeNumber: Integer; //用于比较的数
NumberLength: Integer; //返回值,长度大于10返回-1
begin
if ANumber = null then
begin
result := 0; //空值返回0
exit;
end;
//判断参数的正负
if ANumber < 0 then
begin
ANumber := 0 - ANumber; //转换成正数用于计算长度
IsNegativeNumber := true; //是负数
end
else begin
if ANumber = 0 then
begin
result := 1; //是0,直接返回1
exit;
end;
IsNegativeNumber := false; //是正数
end;
//开始比较
ComporeNumber:= 10;
for LoopIndex:= 1 to 9 do
begin
//长度符合要求
if (ComporeNumber div ANumber) > 0 then
begin
//得到长度
if ComporeNumber = ANumber then NumberLength := LoopIndex + 1
else NumberLength := LoopIndex;
//如果是负数,则长度加1,即包含负号
if IsNegativeNumber then result:= NumberLength + 1
else result := NumberLength;
exit;
end;
//增大1位继续比较
ComporeNumber := ComporeNumber * 10;
continue;
end;
result := -1; //长度大于9,返回-1
end;
//—————————————————————————-//
//为指定的序号列赋值
//—————————————————————————-//
procedure SetNumberFields(ASGrid: TStringGrid; ACol, AStartRow: Integer);
var
RowIndex: Integer; //当前序号
begin
with ASGrid do
begin
for RowIndex := 1 to RowCount – AStartRow do
begin
//添加序号
Cells[ACol, AStartRow + RowIndex – 1] := VarToStr(RowIndex);
end;
end;
end;
//—————————————————————————-//
//设置指定的列的对齐方式为右对齐
//—————————————————————————-//
procedure SetColAlignRight(ASGrid: TStringGrid; ACol, AStartRow: Integer);
var
RowIndex: Integer;
MaxDataLength: Integer; //该列最大的数据长度
begin
MaxDataLength := GetColMaxDataLength(ASGrid, ACol, 0); //取得该列最大的数据长度
with ASGrid do
begin
for RowIndex := AStartRow to RowCount – 1 do
begin
while length(Cells[ACol, RowIndex]) < MaxDataLength do
begin
Cells[ACol, RowIndex] := ' ' + Cells[ACol, RowIndex]; //在前面补空格
end;
end;
end;
end;
//----------------------------------------------------------------------------//
//设置指定行的左边距
//----------------------------------------------------------------------------//
procedure SetRowLeftSpace(ASGrid: TStringGrid; ARow, SpaceLength: Integer);
var
ColIndex, LoopIndex: Integer;
begin
with ASGrid do
begin
for ColIndex := 0 to ColCount - 1 do
begin
Cells[ColIndex, ARow] := TrimLeft(Cells[ColIndex, ARow]); //去掉左边空格
for LoopIndex := 1 to SpaceLength do
begin
Cells[ColIndex, ARow] := ' ' + Cells[ColIndex, ARow]; //在左边补空格
end;
end;
end;
end;
//----------------------------------------------------------------------------//
//设置指定行的最小右边距
//----------------------------------------------------------------------------//
procedure SetRowMinRightSpace(ASGrid: TStringGrid; ARow, SpaceLength: Integer);
var
ColIndex, LoopIndex: Integer;
begin
with ASGrid do
begin
for ColIndex := 0 to ColCount - 1 do
begin
Cells[ColIndex, ARow] := TrimRight(Cells[ColIndex, ARow]); //去掉右边空格
for LoopIndex := 1 to SpaceLength do
begin
Cells[ColIndex, ARow] := Cells[ColIndex, ARow] + ' '; //在右边补空格
end;
end;
end;
end;
//----------------------------------------------------------------------------//
//设置指定行的最小边距
//----------------------------------------------------------------------------//
procedure SetRowMinSpaceWidth(ASGrid: TStringGrid; ARow, SpaceLength: Integer);
var
ColIndex, LoopIndex: Integer;
begin
with ASGrid do
begin
for ColIndex := 0 to ColCount - 1 do
begin
Cells[ColIndex, ARow] := Trim(Cells[ColIndex, ARow]); //去掉两边空格
for LoopIndex := 1 to SpaceLength do
begin
Cells[ColIndex, ARow] := ' ' + Cells[ColIndex, ARow] + ' '; //在两边补空格
end;
end;
end;
end;
//----------------------------------------------------------------------------//
//获得当前X坐标所在的列
//----------------------------------------------------------------------------//
function GetColByCX(ASGrid: TStringGrid; AX: Integer): Integer;
var
ColIndex: Integer;
CurCellRect: TRect; //当前列的矩形区域
begin
with ASGrid do
begin
for ColIndex := 0 to ColCount - 2 do
begin
CurCellRect := CellRect(ColIndex, 0);
//当前列被隐藏,继续判断下一列
if CurCellRect.Left = CurCellRect.Right then continue;
//X坐标在当前列的范围内
if (AX >= CurCellRect.Left) and (AX < CurCellRect.Right) then
begin
result := ColIndex;
exit;
end;
end;
result := ColCount - 1; //返回最后一列的索引
end;
end;
{$R *.dfm}
end.