1. 程式人生 > >匯出寫Excel檔案,Sheet名與匯出檔名相同

匯出寫Excel檔案,Sheet名與匯出檔名相同

使用方法是
Var  MyExcel : TDS2Excel
Begin
  :  :  :  :
    MyExcel := TDS2Excel.Create(aDataSet: TDataSet;aDBGrid:TDBGrid)
    Save2File(XLS檔名, WillWriteHead); //檔名,欄位名做表格列頭。
    // Save2Files(WillWriteHead: Boolean); 該過程會自動彈出檔案對話方塊,供使用者自己選擇檔名
  :  :  :  :
End ;

實際上Express的cxGride控制元件也是採用這種方法實現資料表格匯出到Excal的。
下面是實現的類
===============================
DELPHI 寫EXCEL的XLS格式檔案
===============================
unit ObjectUnit;

interface

Uses
DB, Classes, Dialogs,DBGrids,Controls;
var
CXlsBof: array[0..5] of Word = ($809, 8, 0, $10, 0, 0);
CXlsEof: array[0..1] of Word = ($0A, 00);
CXlsLabel: array[0..5] of Word = ($204, 0, 0, 0, 0, 0);
CXlsNumber: array[0..4] of Word = ($203, 14, 0, 0, 0);
CXlsRk: array[0..4] of Word = ($27E, 10, 0, 0, 0);
CXlsBlank: array[0..4] of Word = ($201, 6, 0, 0, $17);


Type
TDS2Excel = Class(TObject)
Private
  FCol: word;
  FRow: word;
  FDataSet: TDataSet;
  FDbGrid  :TDbGrid;
  Stream: TStream;
  FWillWriteHead: boolean;
  FBookMark: TBookmark;
  procedure IncColRow;
  procedure WriteBlankCell;
  procedure WriteFloatCell(const AValue: Double);
  procedure WriteIntegerCell(const AValue: Integer);
  procedure WriteStringCell(const AValue: string);
  procedure WritePrefix;
  procedure WriteSuffix;
  procedure WriteTitle;
  procedure WriteDataCell;

  procedure Save2Stream(aStream: TStream);
Public
  procedure Save2File(FileName: string; WillWriteHead: Boolean);
  procedure Save2Files(WillWriteHead: Boolean);
  Constructor Create(aDataSet: TDataSet;aDBGrid:TDBGrid);
end;

implementation

uses SysUtils;

Constructor TDS2Excel.Create(aDataSet: TDataSet;aDBGrid:TDBGrid);
begin
inherited Create;
FDataSet := aDataSet;
FDbGrid  :=aDbGrid;
end;


procedure TDS2Excel.IncColRow;
begin
if FDbGrid <>nil then
begin
if FCol = FDbGrid.Columns.Count - 1 then
begin
  Inc(FRow);
  FCol :=0;
end
else
  Inc(FCol);
end else
begin
if FCol = FDataSet.FieldCount - 1 then
begin
  Inc(FRow);
  FCol :=0;
end
else
  Inc(FCol);
end;
end;

procedure TDS2Excel.WriteBlankCell;
begin
CXlsBlank[2] := FRow;
CXlsBlank[3] := FCol;
Stream.WriteBuffer(CXlsBlank, SizeOf(CXlsBlank));
IncColRow;
end;

procedure TDS2Excel.WriteFloatCell(const AValue: Double);
begin
CXlsNumber[2] := FRow;
CXlsNumber[3] := FCol;
Stream.WriteBuffer(CXlsNumber, SizeOf(CXlsNumber));
Stream.WriteBuffer(AValue, 8);
IncColRow;
end;

procedure TDS2Excel.WriteIntegerCell(const AValue: Integer);
var
V: Integer;
begin
CXlsRk[2] := FRow;
CXlsRk[3] := FCol;
Stream.WriteBuffer(CXlsRk, SizeOf(CXlsRk));
V := (AValue shl 2) or 2;
Stream.WriteBuffer(V, 4);
IncColRow;
end;

procedure TDS2Excel.WriteStringCell(const AValue: string);
var
L: Word;
begin
L := Length(AValue);
CXlsLabel[1] := 8 + L;
CXlsLabel[2] := FRow;
CXlsLabel[3] := FCol;
CXlsLabel[5] := L;
Stream.WriteBuffer(CXlsLabel, SizeOf(CXlsLabel));
Stream.WriteBuffer(Pointer(AValue)^, L);
IncColRow;
end;

procedure TDS2Excel.WritePrefix;
begin
Stream.WriteBuffer(CXlsBof, SizeOf(CXlsBof));
end;

procedure TDS2Excel.WriteSuffix;
begin
Stream.WriteBuffer(CXlsEof, SizeOf(CXlsEof));
end;

procedure TDS2Excel.WriteTitle;
var
n: word;
begin
if FDbGrid <> nil then
for n := 0 to FDBGrid.Columns.Count - 1 do
  WriteStringCell(FDBGrid.Columns[n].Title.Caption)
else
for n := 0 to FDataSet.FieldCount - 1 do
  WriteStringCell(FDataSet.Fields[n].FieldName);
end;

procedure TDS2Excel.WriteDataCell;
var
n: word;
begin

WritePrefix;
if FWillWriteHead then WriteTitle;
FDataSet.DisableControls;
FBookMark := FDataSet.GetBookmark;
FDataSet.First;

if FDbGrid=nil then
begin
  while not FDataSet.Eof do
  begin
  for n := 0 to FDataSet.FieldCount - 1 do
  begin
    try
    if FDataSet.Fields[n].IsNull then
      WriteBlankCell
    else begin
      case FDataSet.Fields[n].DataType of
        ftSmallint, ftInteger, ftWord, ftAutoInc, ftBytes:
            WriteIntegerCell(FDataSet.Fields[n].AsInteger);
        ftFloat, ftCurrency, ftBCD:
            WriteFloatCell(FDataSet.Fields[n].AsFloat);
        ftTypedBinary:
      else
        WriteStringCell(FDataSet.Fields[n].AsString);
      end;
    end;
    except
      WriteBlankCell;
    end;
  end;
  FDataSet.Next;
  end;
end

else

begin
  while not FDbGrid.DataSource.DataSet.Eof do
  begin

for n := 0 to FDbGrid.Columns.Count - 1 do
  begin

    if FDbGrid.Columns[n].Field.IsNull then
      WriteBlankCell
    else begin
      case FDbGrid.Columns[n].Field.DataType of
        ftSmallint, ftInteger, ftWord, ftAutoInc, ftBytes:
            WriteIntegerCell(FDbGrid.Columns[n].Field.AsInteger);
        ftFloat, ftCurrency, ftBCD:
            WriteFloatCell(FDbGrid.Columns[n].Field.AsFloat);
      else
        WriteStringCell(FDbGrid.Columns[n].Field.AsString);
      end;
    end;
  end;
  FDbGrid.DataSource.DataSet.Next
  end;
end;


WriteSuffix;
if FDataSet.BookmarkValid(FBookMark) then FDataSet.GotoBookmark(FBookMark);
FDataSet.EnableControls;
end;

procedure TDS2Excel.Save2Stream(aStream: TStream);
begin
FCol := 0;
FRow := 0;
Stream := aStream;
WriteDataCell;
end;

procedure TDS2Excel.Save2File(FileName: string; WillWriteHead: Boolean);
var
aFileStream: TFileStream;
begin
FWillWriteHead := WillWriteHead;
if FileExists(FileName) then DeleteFile(FileName);
aFileStream := TFileStream.Create(FileName, fmCreate);
Try
  Save2Stream(aFileStream);
Finally
  aFileStream.Free;
end;
end;

procedure TDS2Excel.Save2FileS(WillWriteHead: Boolean);
var
SaveDialog11: TSaveDialog;
begin
SaveDialog11 := TSaveDialog.Create(nil);
Try
  SaveDialog11.Filter := 'Excel文件|*.xls';
  SaveDialog11.InitialDir := 'D:/';
  SaveDialog11.FileName:='*.xls';
  if not SaveDialog11.Execute then exit;
  if FileExists(SaveDialog11.FileName) then DeleteFile(SaveDialog11.FileName);
  Save2File(SaveDialog11.FileName, WillWriteHead);
Finally
  SaveDialog11.Free;
end;
end;

end.