如何将显示数据导出为EXCEL文件(*.xls)? 怎样在系统没有安装Office的情况下导出为EXCEL文件,如果有这方面的控件最好 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 給你一個控件代碼!unit ToExcel;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, comobj, dbgrids;type TToExcel = class(TComponent) private FExcelCreated: Boolean; FVisible: Boolean; FExcel: Variant; FWorkBook: Variant; FWorkSheet: Variant; FCellFont: TFont; FTitleFont: TFont; FFontChanged: Boolean; FIgnoreFont: Boolean; FFileName: TFileName; procedure SetExcelCellFont(var Cell: Variant); procedure SetExcelTitleFont(var Cell: Variant); procedure GetGridcolumnName(const Grid: TDbGrid; var Cell: Variant); protected procedure SetCellFont(NewFont: TFont); procedure SetTitleFont(NewFont: TFont); procedure SetVisible(DoShow: Boolean); function GetCell(ACol, ARow: Integer): string; procedure SetCell(ACol, ARow: Integer; const Value: string); function GetDateCell(ACol, ARow: Integer): TDateTime; //得到日期型的?元格 procedure SetDateCell(ACol, ARow: Integer; const Value: TDateTime); public constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure CreateExcelInstance; //?建Excel?例 property Cell[ACol, ARow: Integer]: string read GetCell write SetCell; property DateCell[ACol, ARow: Integer]: TDateTime read GetDateCell write SetDateCell; function IsCreated: Boolean; procedure DbGridToExcel(const Grid: TDbgrid); published property TitleFont: TFont read FTitleFont write SetTitleFont; property CellFont: TFont read FCellFont write SetCellFont; property Visible: Boolean read FVisible write SetVisible; property IgnoreFont: Boolean read FIgnoreFont write FIgnoreFont; property FileName: TFileName read FFileName write FFileName; end;procedure Register;implementationconstructor TToExcel.Create(AOwner: TComponent);begin inherited Create(AOwner); FIgnoreFont := True; //忽略字体 FCellFont := TFont.Create; FTitleFont := TFont.Create; FExcelCreated := False; FVisible := False; FFontChanged := False;end;destructor TToExcel.Destroy;begin FCellFont.Free; //?放?元格字体 FTitleFont.Free; //?放列??的字体 inherited Destroy;end;procedure TToExcel.SetExcelCellFont(var Cell: Variant);begin if FIgnoreFont then exit; with FCellFont do begin Cell.Font.Name := Name; Cell.Font.Size := Size; Cell.Font.Color := Color; Cell.Font.Bold := fsBold in Style; //返回一?boolean值 Cell.Font.Italic := fsItalic in Style; Cell.Font.UnderLine := fsUnderline in Style; Cell.Font.Strikethrough := fsStrikeout in Style; end;end;procedure TToExcel.SetExcelTitleFont(var Cell: Variant);begin if FIgnoreFont then exit; with FTitleFont do begin Cell.Font.Name := Name; Cell.Font.Size := Size; Cell.Font.Color := clred; Cell.Font.Bold := fsBold in Style; Cell.Font.Italic := fsItalic in Style; Cell.Font.UnderLine := fsUnderline in Style; Cell.Font.Strikethrough := fsStrikeout in Style; end;end;procedure TToExcel.SetVisible(DoShow: Boolean);begin if not FExcelCreated then exit; if DoShow then FExcel.Visible := True else FExcel.Visible := False;end;function TToExcel.GetCell(ACol, ARow: Integer): string;begin if not FExcelCreated then exit; result := FWorkSheet.Cells[ARow, ACol];end;procedure TToExcel.SetCell(ACol, ARow: Integer; const Value: string);var Cell: Variant;begin if not FExcelCreated then exit; Cell := FWorkSheet.Cells[ARow, ACol];// SetExcelCellFont(Cell); Cell.Value := Value;end;function TToExcel.GetDateCell(ACol, ARow: Integer): TDateTime;begin if not FExcelCreated then begin result := 0; exit; end; result := StrToDateTime(FWorkSheet.Cells[ARow, ACol]);end;procedure TToExcel.SetDateCell(ACol, ARow: Integer; const Value: TDateTime);var Cell: Variant;begin if not FExcelCreated then exit; Cell := FWorkSheet.Cells[ARow, ACol];// SetExcelCellFont(Cell); Cell.Value := '''' + DateTimeToStr(Value);end;//?建Excel?例procedure TToExcel.CreateExcelInstance;begin try FExcel := CreateOLEObject('Excel.Application'); FWorkBook := FExcel.WorkBooks.Add; FWorkSheet := FWorkBook.WorkSheets.Add; FExcelCreated := True; except FExcelCreated := False; end;end;function TToExcel.IsCreated: Boolean;begin result := FExcelCreated;end;procedure TToExcel.SetTitleFont(NewFont: TFont);begin if NewFont <> FTitleFont then FTitleFont.Assign(NewFont);end;procedure TToExcel.SetCellFont(NewFont: TFont);begin if NewFont <> FCellFont then FCellFont.Assign(NewFont);end;procedure Register;begin RegisterComponents('Toexcel', [TToExcel]);end;procedure TToExcel.DbGridToExcel(const Grid: TDbgrid);var Col, Row: LongInt; Cell: Variant;begin if not FExcelCreated then exit; if Grid.DataSource.DataSet.Active = False then exit; GetGridColumnName(Grid, Cell); Row := 2; with Grid.DataSource.DataSet do begin first; while not EOF do begin for Col := 0 to Grid.Columns.Count - 1 do if Grid.Columns[Col].Visible = True then begin Cell := FWorkSheet.Cells[Row, Col + 1];// SetExcelCellFont(Cell); Cell.Value := Grid.Columns[Col].Field.AsString; end; next; Inc(Row); end; end;end;procedure TToExcel.GetGridcolumnName(const Grid: TDbGrid; var Cell: Variant);var Col: integer;begin for Col := 0 to Grid.Columns.Count - 1 do if Grid.Columns[col].Visible = true then begin fworksheet.cells[1,col+1].font.bold:=true; fworksheet.cells[1,col+1].font.size:=14; Cell :=FWorkSheet.Cells[1, Col + 1]; fworksheet.cells[1,col+1].font.color:=clred; Cell.Value := Grid.Columns[col].Title.Caption; //?列??到CELL中去 end;end;end. Query1.ParamByName传值问题。请求大家帮助。 大家觉得d7和d2007哪个好用? 哪位大虾有时间进来看看这个图像全部变黑的问题 如何实现IE窗口中的屏幕取字?急! 打印报表问题 求AgentObjects_TLB控件 关于Delphi程序如何调用外可执行文件? 求教RichEdit问题 DELPHI中有没有这样的dbcombobox控件,自己在items中加上数据,在保存时自动保存indexof(dbcombobox.text)值 模拟浏览器 100分征解:Delphi中是否能对运算符进行重载? 关于delphi7's IntraWeb...
unit ToExcel;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
comobj, dbgrids;
type
TToExcel = class(TComponent)
private
FExcelCreated: Boolean;
FVisible: Boolean;
FExcel: Variant;
FWorkBook: Variant;
FWorkSheet: Variant;
FCellFont: TFont;
FTitleFont: TFont;
FFontChanged: Boolean;
FIgnoreFont: Boolean;
FFileName: TFileName;
procedure SetExcelCellFont(var Cell: Variant);
procedure SetExcelTitleFont(var Cell: Variant);
procedure GetGridcolumnName(const Grid: TDbGrid; var Cell: Variant);
protected
procedure SetCellFont(NewFont: TFont);
procedure SetTitleFont(NewFont: TFont);
procedure SetVisible(DoShow: Boolean);
function GetCell(ACol, ARow: Integer): string;
procedure SetCell(ACol, ARow: Integer; const Value: string);
function GetDateCell(ACol, ARow: Integer): TDateTime; //得到日期型的?元格
procedure SetDateCell(ACol, ARow: Integer; const Value: TDateTime);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure CreateExcelInstance; //?建Excel?例
property Cell[ACol, ARow: Integer]: string read GetCell write SetCell;
property DateCell[ACol, ARow: Integer]: TDateTime read GetDateCell write
SetDateCell;
function IsCreated: Boolean;
procedure DbGridToExcel(const Grid: TDbgrid);
published
property TitleFont: TFont read FTitleFont write SetTitleFont;
property CellFont: TFont read FCellFont write SetCellFont;
property Visible: Boolean read FVisible write SetVisible;
property IgnoreFont: Boolean read FIgnoreFont write FIgnoreFont;
property FileName: TFileName read FFileName write FFileName;
end;procedure Register;implementationconstructor TToExcel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FIgnoreFont := True; //忽略字体
FCellFont := TFont.Create;
FTitleFont := TFont.Create;
FExcelCreated := False;
FVisible := False;
FFontChanged := False;
end;destructor TToExcel.Destroy;
begin
FCellFont.Free; //?放?元格字体
FTitleFont.Free; //?放列??的字体
inherited Destroy;
end;procedure TToExcel.SetExcelCellFont(var Cell: Variant);
begin
if FIgnoreFont then
exit;
with FCellFont do
begin
Cell.Font.Name := Name;
Cell.Font.Size := Size;
Cell.Font.Color := Color;
Cell.Font.Bold := fsBold in Style; //返回一?boolean值
Cell.Font.Italic := fsItalic in Style;
Cell.Font.UnderLine := fsUnderline in Style;
Cell.Font.Strikethrough := fsStrikeout in Style;
end;
end;procedure TToExcel.SetExcelTitleFont(var Cell: Variant);
begin
if FIgnoreFont then
exit;
with FTitleFont do
begin
Cell.Font.Name := Name;
Cell.Font.Size := Size;
Cell.Font.Color := clred;
Cell.Font.Bold := fsBold in Style;
Cell.Font.Italic := fsItalic in Style;
Cell.Font.UnderLine := fsUnderline in Style;
Cell.Font.Strikethrough := fsStrikeout in Style;
end;
end;procedure TToExcel.SetVisible(DoShow: Boolean);
begin
if not FExcelCreated then
exit;
if DoShow then
FExcel.Visible := True
else
FExcel.Visible := False;
end;function TToExcel.GetCell(ACol, ARow: Integer): string;
begin
if not FExcelCreated then
exit;
result := FWorkSheet.Cells[ARow, ACol];
end;procedure TToExcel.SetCell(ACol, ARow: Integer; const Value: string);
var
Cell: Variant;
begin
if not FExcelCreated then
exit;
Cell := FWorkSheet.Cells[ARow, ACol];
// SetExcelCellFont(Cell);
Cell.Value := Value;
end;function TToExcel.GetDateCell(ACol, ARow: Integer): TDateTime;
begin
if not FExcelCreated then
begin
result := 0;
exit;
end;
result := StrToDateTime(FWorkSheet.Cells[ARow, ACol]);
end;procedure TToExcel.SetDateCell(ACol, ARow: Integer; const Value: TDateTime);
var
Cell: Variant;
begin
if not FExcelCreated then
exit;
Cell := FWorkSheet.Cells[ARow, ACol];
// SetExcelCellFont(Cell);
Cell.Value := '''' + DateTimeToStr(Value);
end;
//?建Excel?例procedure TToExcel.CreateExcelInstance;
begin
try
FExcel := CreateOLEObject('Excel.Application'); FWorkBook := FExcel.WorkBooks.Add;
FWorkSheet := FWorkBook.WorkSheets.Add;
FExcelCreated := True;
except
FExcelCreated := False;
end;
end;function TToExcel.IsCreated: Boolean;
begin
result := FExcelCreated;
end;procedure TToExcel.SetTitleFont(NewFont: TFont);
begin
if NewFont <> FTitleFont then
FTitleFont.Assign(NewFont);
end;procedure TToExcel.SetCellFont(NewFont: TFont);
begin
if NewFont <> FCellFont then
FCellFont.Assign(NewFont);
end;
procedure Register;
begin
RegisterComponents('Toexcel', [TToExcel]);
end;procedure TToExcel.DbGridToExcel(const Grid: TDbgrid);
var
Col, Row: LongInt;
Cell: Variant;
begin
if not FExcelCreated then
exit;
if Grid.DataSource.DataSet.Active = False then
exit;
GetGridColumnName(Grid, Cell);
Row := 2;
with Grid.DataSource.DataSet do
begin
first;
while not EOF do
begin
for Col := 0 to Grid.Columns.Count - 1 do
if Grid.Columns[Col].Visible = True then
begin
Cell := FWorkSheet.Cells[Row, Col + 1];
// SetExcelCellFont(Cell);
Cell.Value := Grid.Columns[Col].Field.AsString;
end;
next;
Inc(Row);
end;
end;
end;procedure TToExcel.GetGridcolumnName(const Grid: TDbGrid;
var Cell: Variant);
var
Col: integer;
begin
for Col := 0 to Grid.Columns.Count - 1 do
if Grid.Columns[col].Visible = true then
begin
fworksheet.cells[1,col+1].font.bold:=true;
fworksheet.cells[1,col+1].font.size:=14;
Cell :=FWorkSheet.Cells[1, Col + 1];
fworksheet.cells[1,col+1].font.color:=clred;
Cell.Value := Grid.Columns[col].Title.Caption; //?列??到CELL中去
end;
end;end.