unit TExcelExportUnit;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComObj, Grids, AdvObj, BaseGrid, Math, ShellApi,
AdvGrid,tmsUFlxFormats,
VCL.FlexCel.Core, FlexCel.XlsAdapter, FlexCel.Render,
tmsAdvGridExcel;
type
TExcelExport =
class
strict protected
FExcelApp: OleVariant;
FWorkbook: OleVariant;
Fxls: TExcelFile;
FAutor:
string;
FFirma:
string;
FLogo_Pfad:
string;
//Pfad des Logos
FQuer_Format: boolean;
//Wenn true, dann Querformat, ansonsten Hochformat
FSeiten_Ver: integer;
FSeiten_Hor: integer;
FSheet: integer;
FSheets: Integer;
FTitel:
string;
//Das ist die Procedure, die ich zuweise.
procedure GridExcelCellFormat(Sender: TAdvStringGrid;
const GridCol,
GridRow, XlsCol, XlsRow: Integer;
const Value: WideString;
var Format: tmsUFlxFormats.TFlxFormat);
public
procedure OpenTemp;
procedure ShowTemp(Pfad:
string);
procedure SaveFile(SG: TAdvStringGrid; dateiname:
string);
procedure CreateXLSFile(Sheets: integer);
procedure Basis_Design(Sheet: integer; Titel:
string);
procedure Zellen_Groesse(SG: TAdvStringGrid);
procedure Zellen(fmt: TFlxFormat; SG: TAdvStringGrid);
property Autor:
string read FAutor
write FAutor;
property Firma:
string read FFirma
write FFirma;
property Logo_Pfad:
string read FLogo_Pfad
write FLogo_Pfad;
property Quer_Format: boolean
read FQuer_Format
write FQuer_Format;
property Seiten_Ver: integer
read FSeiten_Ver
write FSeiten_Ver;
property Seiten_Hor: integer
read FSeiten_Hor
write FSeiten_Hor;
property ExcelApp: OleVariant
read FExcelApp;
property Workbook: OleVariant
read FWorkbook;
property Anzahl_Sheets: integer
read FSheets
write FSheets;
property Aktives_Sheet: integer
read FSheet
write FSheet;
property Sheet_Titel:
string read FTitel
write FTitel;
end;
var ExportGrid: TExcelExport;
implementation
procedure TExcelExport.GridExcelCellFormat(Sender: TAdvStringGrid;
const GridCol,
GridRow, XlsCol, XlsRow: Integer;
const Value: WideString;
var Format: tmsUFlxFormats.TFlxFormat);
//Hier musste ich den Typen mit dem Unitnamen ansprechen.
begin
if (Sender
as TAdvStringGrid).Cells[GridCol,0]='
Menge'
then
Format.Format:='
#,##0';
end;
procedure TExcelExport.SaveFile(SG: TAdvStringGrid; dateiname:
string);
var fmt: TFlxFormat;
Pfad:
string;
GridExcel: TAdvGridExcelIO;
begin
try
Pfad:=ExtractFilePath(ParamStr(0))+'
Temp\Export'+StringReplace(DateToStr(now)+'
-'+TimeToStr(now),'
:','
-',[rfIgnoreCase, rfReplaceAll])+'
.xlt';
GridExcel:= TAdvGridExcelIO.Create(
nil);
GridExcel.AdvStringGrid:=SG;
GridExcel.GridStartRow:=0;
GridExcel.GridStartCol:=0;
GridExcel.OnCellFormat:=GridExcelCellFormat;
//Hier wird sie zugewiesen
GridExcel.XLSExport(Pfad,SG.
Name);
Fxls := TXlsFile.Create(true);
Fxls.Open(Pfad);
DeleteFiles(Pfad);
Basis_Design(FSheet, FTitel);
Zellen(fmt, SG);
if dateiname='
'
then begin
Fxls.Save(Pfad);
ShowTemp(Pfad);
DeleteFiles(Pfad);
end
else begin
//Save the file as XLS
Fxls.Save(dateiname);
end;
finally
Fxls.Free;
GridExcel.Free;
end
end;
initialization
ExportGrid := TExcelExport.Create;
finalization
if ExportGrid <>
nil then
ExportGrid.Free;