凤
凤冠坡
Unregistered / Unconfirmed
GUEST, unregistred user!
调用方法:后面2个参数可选
1. ExportToExcel(DBGridEH1);
2. ExportToExcel(DBGridEH1, SB);
3. ExportToExcel(DBGridEH1, SB, 'ABCDE);
---------------------------------------------------------------------------
程序主过程如下:
procedure ExportToExcel(Grid: TDBGridEH; const SB: TStatusBar = nil; const HTittle: string = '请输入报表名称');
Type
SummType = array[0..200] of Real;
var
Str: string;
Book: TBookMark;
Data: TDataSet;
C1, C2: Char;
I, FN, MN, RN: integer;
ExpApp, ExpBook, Sheet: Variant;
FP: TField;
TF: TextFile;
LS: TStringList;
RG: Variant;
Summ: ^SummType;
begin
try
ExpApp := CreateOleObject('Excel.Application');
except
TellMe('没有安装 EXCEL 驱动程序,打印失败');
Exit;
end;
ShowForm.Text := '系统正在读取数据库+';
Screen.Cursor := crHourGlass;
Data := Grid.DataSource.DataSet;
FN := Grid.Columns.Count - 1;
AssignFile(TF, 'C:/Temp.Txt');
Rewrite(TF);
Str := StringOfChar(#9, FN div 2) + HTittle + #13#10;
Writeln(TF, Str);
Str := '';
//生成标题
New(Summ);
for i := 0 to FN do begin
Summ := 0;
Str := Str + Grid.Columns.FieldName + #9;
end;
Delete(Str, Length(Str), 1);
Writeln(TF, Str);
Data.DisableControls;
Book := Data.GetBookmark;
Data.First;
//生成内容
RN := Data.RecordCount;
MN := 0;
repeat
Str := '';
for I := 0 to FN do begin
FP := Grid.Columns.Field;
if FP is TNumericField then begin
Summ := Summ + FP.AsFloat;
end;
Str := Str + FP.Text + #9;
end;
Delete(Str, Length(Str), 1);
Writeln(TF, Str);
Data.Next;
MN := MN + 1;
if (MN mod 100 = 0) and (SB <> nil) then begin
SB.Panels[1].Text := Format('读取进度:%d/%d,按[ESC]键终止取数', [MN, RN]);
SB.Update;
end;
Application.ProcessMessages;
if GetKeyState(VK_ESCAPE) and 128 = 128 then Break;
until Data.eof;
if SB <> nil then begin
SB.Panels[1].Text := '';
SB.Update;
end;
Str := '合计';
for i := 0 to FN do begin
if Summ = 0 then Str := Str + #9 else
Str := Str + FloatToStr(Summ) + #9;
end;
Writeln(TF, Str);
CloseFile(TF);
Data.EnableControls;
Data.GotoBookmark(Book);
Data.FreeBookmark(Book);
ShowForm.Hide;
ShowForm.Text := '系统正在启动Execel程序+';
ExpApp.Caption := 'Delphi 程序导出的数据表格';
ExpBook := ExpApp.Workbooks.Add;
// ExpBook := ExpApp.Workbooks.Open('File01.XLS');
Sheet := ExpBook.WorkSheets[1];
Byte(C1) := FN div 26 + 64;
Byte(C2) := FN mod 26 + 65;
if C1 = #64 then C1 := #32;
Str := 'A3:' + C1 + C2 + IntToStr(MN + 4);
Sheet.Cells.NumberFormatLocal := '@';
LS := TStringList.Create;
LS.LoadFromFile('C:/Temp.TXT');
DeleteFile('C:/Temp.TXT');
Clipboard.AsText := LS.Text;
LS.Free;
Sheet.Paste;
Clipboard.Clear;
RG := Sheet.Range[Str];
RG.Font.Size := 10;
ShowForm.Hide;
ShowForm.Text := '系统正在设置表格线类型+';
RG.HorizontalAlignment := xlCenter;
RG.Borders.Item[5].LineStyle := xlNone;
RG.Borders.Item[6].LineStyle := xlNone;
for i := 7 to 12 do begin
RG.Borders.Item.LineStyle := xlContinuous;
// RG.Borders.Item.Weight := xlThin;
// RG.Borders.Item.ColorIndex := xlAutomatic;
end;
Sheet.Cells.EntireColumn.AutoFit;
Sheet.Rows['1:1'].Font.Size := 25;
Sheet.Rows['1:1'].Font.Bold := True;
Sheet.Range['A4'].Select;
Sheet.Range['A2'] := FormatDateTime('"日期:"yyyy"年"mm"月"dd"日"', Date);
ExpBook.Saved := True;
ShowForm.Hide;
ExpApp.Visible := True;
Dispose(Summ);
Screen.Cursor := crDefault;
end;
1. ExportToExcel(DBGridEH1);
2. ExportToExcel(DBGridEH1, SB);
3. ExportToExcel(DBGridEH1, SB, 'ABCDE);
---------------------------------------------------------------------------
程序主过程如下:
procedure ExportToExcel(Grid: TDBGridEH; const SB: TStatusBar = nil; const HTittle: string = '请输入报表名称');
Type
SummType = array[0..200] of Real;
var
Str: string;
Book: TBookMark;
Data: TDataSet;
C1, C2: Char;
I, FN, MN, RN: integer;
ExpApp, ExpBook, Sheet: Variant;
FP: TField;
TF: TextFile;
LS: TStringList;
RG: Variant;
Summ: ^SummType;
begin
try
ExpApp := CreateOleObject('Excel.Application');
except
TellMe('没有安装 EXCEL 驱动程序,打印失败');
Exit;
end;
ShowForm.Text := '系统正在读取数据库+';
Screen.Cursor := crHourGlass;
Data := Grid.DataSource.DataSet;
FN := Grid.Columns.Count - 1;
AssignFile(TF, 'C:/Temp.Txt');
Rewrite(TF);
Str := StringOfChar(#9, FN div 2) + HTittle + #13#10;
Writeln(TF, Str);
Str := '';
//生成标题
New(Summ);
for i := 0 to FN do begin
Summ := 0;
Str := Str + Grid.Columns.FieldName + #9;
end;
Delete(Str, Length(Str), 1);
Writeln(TF, Str);
Data.DisableControls;
Book := Data.GetBookmark;
Data.First;
//生成内容
RN := Data.RecordCount;
MN := 0;
repeat
Str := '';
for I := 0 to FN do begin
FP := Grid.Columns.Field;
if FP is TNumericField then begin
Summ := Summ + FP.AsFloat;
end;
Str := Str + FP.Text + #9;
end;
Delete(Str, Length(Str), 1);
Writeln(TF, Str);
Data.Next;
MN := MN + 1;
if (MN mod 100 = 0) and (SB <> nil) then begin
SB.Panels[1].Text := Format('读取进度:%d/%d,按[ESC]键终止取数', [MN, RN]);
SB.Update;
end;
Application.ProcessMessages;
if GetKeyState(VK_ESCAPE) and 128 = 128 then Break;
until Data.eof;
if SB <> nil then begin
SB.Panels[1].Text := '';
SB.Update;
end;
Str := '合计';
for i := 0 to FN do begin
if Summ = 0 then Str := Str + #9 else
Str := Str + FloatToStr(Summ) + #9;
end;
Writeln(TF, Str);
CloseFile(TF);
Data.EnableControls;
Data.GotoBookmark(Book);
Data.FreeBookmark(Book);
ShowForm.Hide;
ShowForm.Text := '系统正在启动Execel程序+';
ExpApp.Caption := 'Delphi 程序导出的数据表格';
ExpBook := ExpApp.Workbooks.Add;
// ExpBook := ExpApp.Workbooks.Open('File01.XLS');
Sheet := ExpBook.WorkSheets[1];
Byte(C1) := FN div 26 + 64;
Byte(C2) := FN mod 26 + 65;
if C1 = #64 then C1 := #32;
Str := 'A3:' + C1 + C2 + IntToStr(MN + 4);
Sheet.Cells.NumberFormatLocal := '@';
LS := TStringList.Create;
LS.LoadFromFile('C:/Temp.TXT');
DeleteFile('C:/Temp.TXT');
Clipboard.AsText := LS.Text;
LS.Free;
Sheet.Paste;
Clipboard.Clear;
RG := Sheet.Range[Str];
RG.Font.Size := 10;
ShowForm.Hide;
ShowForm.Text := '系统正在设置表格线类型+';
RG.HorizontalAlignment := xlCenter;
RG.Borders.Item[5].LineStyle := xlNone;
RG.Borders.Item[6].LineStyle := xlNone;
for i := 7 to 12 do begin
RG.Borders.Item.LineStyle := xlContinuous;
// RG.Borders.Item.Weight := xlThin;
// RG.Borders.Item.ColorIndex := xlAutomatic;
end;
Sheet.Cells.EntireColumn.AutoFit;
Sheet.Rows['1:1'].Font.Size := 25;
Sheet.Rows['1:1'].Font.Bold := True;
Sheet.Range['A4'].Select;
Sheet.Range['A2'] := FormatDateTime('"日期:"yyyy"年"mm"月"dd"日"', Date);
ExpBook.Saved := True;
ShowForm.Hide;
ExpApp.Visible := True;
Dispose(Summ);
Screen.Cursor := crDefault;
end;