Forum > Beginners
Align numbers before saving to a file.
seghele0:
The code below works.
90% of the code is not mine (TOO complex).
What I would like to add is the alignment of the scores (numbers) on the right side.
Thanks already.
:-[
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// 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 cellvar 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; // Columnbegin // 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 + PadRight(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 + PadRight(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 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 LibreOfficefinally // Destroy the workbook after usage. VarWORKBOOK.Free;end;end;
Thaddy:
There should never be white space in a data file. You are barking up the wrong tree.
paweld:
Write as number:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// 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 cellvar 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 // cell value 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 + PadRight(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 + PadRight(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 if TryStrToFloat(StringGrid1.Cells[VarC, VarR], tmpValue) then VarCELL := VarWORKSHEET.WriteNumber(VarR, VarC, tmpValue) else 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;
seghele0:
;)
Many thanks for your answer.
Can only implement the code from Monday due to illness.
Will keep you informed.
Thaddy:
Get well soon :)
Navigation
[0] Message Index
[#] Next page