1、保存DBGRID数据到EXCEL文件中
对excel的读写:
unit UMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables, StdCtrls, Grids, DBGrids,Excel97,Comctrls,OleCtnrs,ComObj;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
Button1: TButton;
Button2: TButton;
DataSource1: TDataSource;
Table1: TTable;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
XlsApp,XlsSheet,XlsWBk : Variant;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
I,J : integer;
begin
if VarIsEmpty(XlsApp) then
XlsApp := CreateOleObject('Excel.Application');
XLsApp.Workbooks.Add;
XlsSheet := XLsApp.Worksheets['Sheet1'];
for I := 0 to Table1.Fields.Count - 1 do
begin
XlsSheet.Cells[3,I + 1] := dbgrid1.Columns.Title.Caption;
end;
Table1.first;
for J := 0 to Table1.RecordCount - 1 do
begin
for I := 0 to Table1.Fields.Count - 1 do
begin
XlsSheet.Cells[J + 4,I + 1] := Table1.Fields.AsString;
end;
Table1.Next;
end;
XlsApp.Visible := true;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
I,J : integer;
TxtFile : TextFile;
TmpString : String;
begin
try
if VarIsEmpty(XlsApp) then
XlsApp := CreateOleObject('Excel.Application');
XlsSheet := XlsApp.workbooks.open('c:/my documents/book3.xls');
AssignFile(TxtFile,'C:/My Documents/Test.txt');
Rewrite(TxtFile);
try
for I := 3 to 21 do
begin
TmpString := '';
for J := 1 to 5 do
begin
TmpString := TmpString + XlsSheet.ActiveSheet.Cells[I,J].Text + '|';
end;
Writeln(TxtFile,Tmpstring);
end;
finally
CloseFile(TxtFile);
end;
XlsApp.Visible := true;
except
XlsSheet.close;
XlsApp.Application.quit;
XlsApp := Unassigned;
XlsSheet := Unassigned;
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if not VarIsEmpty(XlsApp) then
begin
XlsApp.DisplayAlerts := True; // 7Discard unsaved files....
try
XlsApp.Application.Quit;
except
end;
end;
end;
end.
2、导出到Html
控件单元
unit DsExport;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
DB;
type
TKHTMLFormatCellEvent = procedure(Sender: TObject; CellRow, CellColumn: Integer; FieldName: string;
var CustomAttrs, CellData: string) of object;
TDataSetToHTML = class(TComponent)
private
FDataSet: TDataSet;
FHeader: TStrings;
FFooter: TStrings;
FOnFormatCell: TKHTMLFormatCellEvent;
procedure SetHeader(Value: TStrings);
procedure SetFooter(Value: TStrings);
protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Transfer(const FileName: string);
published
property DataSet: TDataSet read FDataSet write FDataSet;
property Header: TStrings read FHeader write SetHeader;
property Footer: TStrings read FFooter write SetFooter;
property OnFormatCell: TKHTMLFormatCellEvent read FOnFormatCell write FOnFormatCell;
end;
implementation
constructor TDataSetToHTML.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDataSet := nil;
FHeader := TStringList.Create;
FFooter := TStringList.Create;
FOnFormatCell := nil;
end;
destructor TDataSetToHTML.Destroy;
begin
FFooter.Free;
FHeader.Free;
inherited;
end;
procedure TDataSetToHTML.SetHeader(Value: TStrings);
begin
FHeader.Assign(Value);
end;
procedure TDataSetToHTML.SetFooter(Value: TStrings);
begin
FFooter.Assign(Value);
end;
procedure TDataSetToHTML.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (AComponent = FDataSet) then
FDataSet := nil;
end;
procedure TDataSetToHTML.Transfer(const FileName: string);
var
HTML: TStrings;
i, RowNum, ColNum: integer;
CustomAttrs: string;
S: string;
begin
HTML := TStringList.Create;
try
if FHeader.Count > 0 then
HTML.Assign(FHeader)
else
begin
HTML.Add('<html>');
HTML.Add('<head> <title> DataSet </title> </head>');
HTML.Add('<body>');
end;
HTML.Add('<table border = "1" rows = "' + IntToStr(FDataSet.RecordCount) +
'" bordercolor="#000000" cellspacing="0" cellpadding="2">');
with FDataSet do
begin
//filling table header
HTML.Add('<tr>'); ColNum := 1;
for i := 0 to FieldCount - 1 do
begin
if Fields.Visible then
begin
CustomAttrs := '';
S := Fields.DisplayLabel;
if Assigned(FOnFormatCell) then
FOnFormatCell(Self, 1, ColNum,
Fields.FieldName, CustomAttrs, S);
HTML.Add('<th ' + CustomAttrs + ' >' + S + ' </th>');
inc(ColNum)
end;
end;
HTML.Add('</tr>');
First; RowNum := 2;
while not EOF do
begin
HTML.Add('<tr>'); ColNum := 1;
for i := 0 to FieldCount - 1 do
begin
if Fields.Visible then
begin
CustomAttrs := '';
{
if Fields.DataType = ftCurrency then
begin
S := FloatToStrF(Fields.AsCurrency, ffCurrency, 20, 2)
end
else if Fields.DataType = ftFloat then
begin
S := FloatToStrF(Fields.AsFloat, ffGeneral, 4, 1);
end
else
S := Fields.AsString;
}
S := Fields.Text;
if S = '' then S := '&nbsp';
if Assigned(FOnFormatCell) then
FOnFormatCell(Self, RowNum, ColNum,
Fields.FieldName, CustomAttrs, S);
HTML.Add('<td nowrap ' + CustomAttrs + ' >' + S + ' </td>');
inc(ColNum)
end;
end;
HTML.Add('</tr>');
FDataSet.Next; inc(RowNum);
end;
end;
HTML.Add('</table>');
if FFooter.Count > 0 then
HTML.AddStrings(FFooter)
else
begin
HTML.Add('</body>');
HTML.Add('</html>');
end;
HTML.SaveToFile(FileName);
finally
HTML.Free;
end;
end;
end.
使用:
procedure TfrmCustomers.btnExportClick(Sender: TObject);
var
DSExp: TDataSetToHtml;
saveDlg: TSaveDialog;
fn: string;
begin
saveDlg := TSaveDialog.Create(self);
DSExp := TDataSetToHtml.Create(self);
try
saveDlg.Filter := 'HTML 文件(*.HTM)|*.HTM';
saveDlg.FileName := reportHeader;
if saveDlg.Execute then
begin
fn := saveDlg.FileName + '.HTM';
DsExp.DataSet := ADODS_Customer;
DsExp.DataSet.DisableControls;
DsExp.Header.Text := reportHeader;
DsExp.Transfer(fn);
DsExp.DataSet.EnableControls;
ShowMessage('导出完毕。');
end;
finally
saveDlg.Free;
DSExp.Free;
end;
end;