// SAVE StringGrid
// Saving an XLSX and TXT file.
procedure BewaarStringGrid(StringGrid1: TStringGrid);
const
HEADER_FONT_SIZE = 11; // in Points
DEFAULT_FONT_SIZE = 15; // in Points
NICE_ROWHEIGHT_FACTOR = 1.5; // adds extra margin to the cell
var
varScoreBestandsnaam : String;
varLengteLangsteNaam : Integer;
varKolomBreedte : Integer;
varTitelLijn, varNamenLijn, varScoreLijn: String;
varRow : Integer;
varCol : Integer;
varFile1 : TextFile;
// Following for XLSX
VarWORKBOOK: TsWorkbook;
VarWORKSHEET: TsWorksheet;
VarCELL: pCell;
VarR: Integer; // Row
VarC: Integer; // Column
tmpvalue : Single;
begin
// Show vertical and horizontal lines
StringGrid1.Options := StringGrid1.Options + [goVertLine, goHorzLine];
// Adjust column widths automatically
StringGrid1.AutoSizeColumns;
// Create a TXT file
// First check the length of the longest name
varLengteLangsteNaam := 0;
for varCol := 0 to StringGrid1.ColCount - 1 do
begin
if (Length(StringGrid1.Cells[varCol, 0]) > varLengteLangsteNaam) then
begin
varLengteLangsteNaam := Length(StringGrid1.Cells[varCol, 0]);
end;
end;
// Extra space between columns
varKolomBreedte := varLengteLangsteNaam + 2;
// End of preparation
varScoreBestandsnaam := 'WIEZEN' + '.txt';
// Write to the file
AssignFile(varFile1, varScoreBestandsnaam);
Try
Rewrite(varFile1);
varTitelLijn := 'Resultaat Wiezen - ' + formatdatetime('dd/mm/YYYY HH:MM',
Now);
// Write the "title" line
Writeln(varFile1, varTitelLijn);
// Then put the names of the players in a line
varNamenLijn := '';
for varCol := 0 to StringGrid1.ColCount - 1 do
begin
varNamenLijn := varNamenLijn + PadLeft(StringGrid1.Cells[varCol, 0],
varKolomBreedte);
end;
// Write the names line
Writeln(varFile1, varNamenLijn);
// The lines with the scores
for varRow := 1 to StringGrid1.RowCount - 1 do
begin
varScoreLijn := '';
for varCol := 0 to StringGrid1.ColCount - 1 do
begin
varScoreLijn := varScoreLijn + PadLeft(StringGrid1.Cells[varCol,
varRow], varKolomBreedte);
end;
//Write the score line
Writeln(varFile1, varScoreLijn);
end;
Finally
//Close the file
CloseFile(varFile1);
End;
//**********************
//* CREATE a WORKBOOK *
//**********************
VarWORKBOOK := TsWorkbook.Create;
try
// Add a worksheet to the workbook
VarWORKSHEET := VarWORKBOOK.AddWorksheet('Export from grid');
// set default Font for the entire workbook //
// Later on, the Font Size for the "header" will be changed !!! //
VarWORKBOOK.SetDefaultFont('Arial',DEFAULT_FONT_SIZE);
VarWORKSHEET.WriteDefaultRowHeight(DEFAULT_FONT_SIZE*NICE_ROWHEIGHT_FACTOR, suPoints);
// set default Column widths like in the StrngGrid //
// Making use of "lpsUtils" in uses //
for VarC := 0 to StringGrid1.ColCount-1 do
VarWORKSHEET.writecolwidth
(VarC, pxToPts(StringGrid1.ColWidths[VarC],Screen.PixelsPerInch),suPoints);
//--------------------------------------------------------
// Write the title row
//---------------------------------------------------------
for VarC := 0 to StringGrid1.ColCount-1 do
begin
//Write the text of the header cell //
VarCELL:= VarWORKSHEET.WriteText(0, VarC, StringGrid1.Cells[VarC, 0]);
// Center text Horizontally //
VarWORKSHEET.WriteHorAlignment(VarCELL,haCenter);
// Center text Vertically //
VarWORKSHEET.WriteVertAlignment(VarCELL,vaCenter);
// Draw a default border around each cell
VarWORKSHEET.writeborders(VarCELL,[cbNorth,cbSouth,cbEast,cbWest]);
// text in header row in Bold //
VarWORKSHEET.WriteFontStyle(VarCELL, [fssBold]);
// text header font size //
VarWORKSHEET.WriteFontSize(VarCELL,HEADER_FONT_SIZE);
// name of the Font //
VarWORKSHEET.WriteFontName(VarCELL, 'Arial');
// Color Background of header cell //
VarWORKSHEET.WriteBackgroundColor(VarCELL,scYellow);
end;
// Set the row height for the header row which has a smaller font than
// the rest
VarWORKSHEET.WriteRowHeight(0, HEADER_FONT_SIZE*NICE_ROWHEIGHT_FACTOR, suPoints);
// Write the PageHeader: DATE.
VarWORKSHEET.PageLayout.Headers[HEADER_FOOTER_INDEX_ALL]:= '&C&D';
// Write the data cells
for VarR := 1 to StringGrid1.RowCount-1 do
begin
for VarC := 0 to StringGrid1.ColCount-1 do
begin
// check if value is number // from Forum
if trystrtofloat(Stringgrid1.Cells[VarC,VarR],tmpvalue)then
VarCELL:= VarWORKSHEET.Writenumber(VarR, VarC,tmpvalue)
else
// above from Forum
VarCELL:= VarWORKSHEET.WriteText
(VarR,VarC,StringGrid1.Cells[VarC,VarR]);
// write border around the cells //
VarWORKSHEET.WriteBorders
(VarCELL,[cbNorth,cbSouth,cbEast,cbWest]);
VarWORKSHEET.WriteHorAlignment
(VarCELL,haCenter);
// By default cells are bottom-aligned. It looks better if they are centered.
VarWORKSHEET.WriteVertAlignment
(VarCELL,vaCENTER);
end;
end;
// Just to show you automatic row height adjustment (not needed since the
// row heights have been handled already).
// Adjust the row heights
for VarR := 0 to VarWORKSHEET.GetLastRowIndex do
begin
VarWORKSHEET.WriteRowHeight
(VarR, NICE_ROWHEIGHT_FACTOR * VarWORKSHEET.CalcAutoRowHeight(VarR),
VarWORKBOOK.Units);
end;
//---------------------------------------------------------
// Save the workbook as xlsx file
//Showmessage('Will save to xlsx');
VarWORKBOOK.WriteToFile('WIEZEN.xlsx', sfOOXML, true);
// workbook.WriteToFile('Test.ods', sfOpenDocument, true);
// or save it for LibreOffice
finally
// Destroy the workbook after usage.
VarWORKBOOK.Free;
end;
end;
procedure waitproc;//SpeedButton1Click(Sender: TObject);
begin
try
Application.CreateForm(TFwait, Fwait);
Fwait.Show;
Fwait.Update;
Fwait.Width := 0;
Fwait.Height := 0;
while (Fwait.Width < 500) and (Fwait.Height < 300) do
begin
Fwait.Width := Fwait.Width + 5;
Fwait.Height := Fwait.Height + 1;
Fwait.Update;
sleep(1);
Fwait.Panelwait.caption := ' E I N D E !!!';
Fwait.Update;
end;
// eventueel uitvoeren van eigen proc.
finally
sleep(5000);
while (Fwait.Width > 0) and (Fwait.Height > 0) do
begin
Fwait.Width := Fwait.Width - 5;
Fwait.Height := Fwait.Height - 1;
Fwait.Update;
sleep(2);
end;
end;
Fwait.Hide;
Fwait.Release;
end;