If possible, with the code.

Closed due to the fact that off-topic participants ReinRaus , Athari , velikodniy , DeKaNszn , Nofate 9 May '15 at 13:18 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "Questions asking for help with debugging (" why does this code not work? ") Should include the desired behavior , a specific problem or error, and the minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, complete, repeatable example . " - ReinRaus, Athari, velikodniy, DeKaNszn, Nofate
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • I don’t know how to print from StringGrid itself. Can first display in Excel, and then print? - DelphiM0ZG
  • It is possible and so) And how to do it, do not tell me? - Margo

2 answers 2

Here is my version of the output from StringGrid to CSV, and print from CSV

var Out:TstringList; i,j:integer; s:String; begin Out:=TstingList.create; for i:=0 to StringGid1.RowCounter-1 do begin s:=''; for j:= 0 to ColCounter-1 do s:=s+StringGrid.Cells[j,i]+';'; Out.Add(s); end; out.SaveToFile('out.csv'); out.free; end; 

He wrote right here, so that errors are possible.

  • Thanks to everyone) I will try) - Margo
  • SoftR, it turned out to be printed, only now how to make lines between cells so that it looks like a table ?? - Margo
  • one
    Well, as an option, open a CSV in Excell burden it with lines and print with Excell. In general, for the output of tabular data, it is customary to use report generators, such as FastReopt and so on. Another Option Generate the HTML code of your table and print from the browser, not the most difficult option, but not the most beautiful. - SoftR
  • And, for example, if you want to display the headers in bold (or, if the headers already exist in the Excel document, then how to start the output from a specific line). How to do this with CVS, please tell me if you can? I need this to generate a report. I tried another version of data output in Excel: using COM automation servers. - DelphiM0ZG
  • CSV This is one of the variations of the text file format, namely the text file separator ";". That is, the text does not have an attribute of the fonts, so that with font selection the font does not work out, if you need support for fonts, look towards RTF / HTML . .RowCounter-1 do 0 is replaced with the number of the desired string, remembering that the strings in a StringGrid are numbered from zero. - SoftR

Outputting data from a StringGrid to an Excel sheet. Do not forget in the Uses section to connect the module ComObj !!! Here is the code:

 procedure TForm1.BitBtn2Click(Sender: TObject); Var ExcelApp, ExcelSheet, ExcelCol, ExcelRow: Variant; Size: Byte; i, j, N, M: Word; begin // Запускаю приложение Excel ExcelApp:=CreateOleObject('Excel.Application'); ExcelApp.Visible:=True; // Создаю рабочую книгу ExcelApp.Workbooks.Add(-4167); ExcelApp.Workbooks[1].WorkSheets[1].Name:='Отчёт'; ExcelCol:=ExcelApp.Workbooks[1].WorkSheets['Отчёт'].Columns; Size:=StringGrid1.DefaultRowHeight; // Ширина ячейки N:=StringGrid1.ColCount-1; // Число ячеек в строке //Если StringGrid содержит переменное число строк, то пользуемся циклом: For j:=0 To N Do ExcelCol.Columns[j+1].ColumnWidth:=Size; ExcelRow:=ExcelApp.Workbooks[1].WorkSheets['Отчёт'].Rows; ExcelRow.Rows[1].Font.Bold:=True; // Заголовки столбцов - жирные // Ввод данных в лист Excel из StringGrid ExcelSheet:=ExcelApp.Workbooks[1].WorkSheets['Отчёт']; For i:=0 To StringGrid1.RowCount-1 Do For j:=0 To StringGrid1.ColCount-1 Do ExcelSheet.Cells[i+1, j+1]:=StringGrid1.Cells[j, i]; end; 
  • You can, of course, still, as an option, print the data from the StringGrid using its canvas and printer's outline, but then you can’t fix something, edit it, as is the case with Excel. - DelphiM0ZG
  • one
    Not the best transfer algorithm with StingGrid in Exel. If there are 1000 records and the fields in record 10, then half a day will be transferred. I recommend instead transferring each cell separately, transferring by a range. - SoftR