Topic: Export to csv with header

Greetings to all fans of a cool tool for making wishes come true. Maybe there are active gurus who can help. It is necessary to create an export procedure (I already borrowed it). It should download only those data that are selected (by shift, for example, or filtered by search) from TableGrid. But the problem is that the first line of the file must contain the sum of the downloaded data and the number of downloaded lines. There is no way to write this summary line to a file.
Help, please.

Post's attachments

Attachment icon PaymentsRegister m_3.zip 361.3 kb, 24 downloads since 2024-02-27 

Re: Export to csv with header

you can do this using a hidden staging table.
In the main table you select the records you need. By clicking the Export button, your rows are duplicated into an intermediate table. Once the operation is complete, you insert the first row into a staging table with the data you need and export to CSV.

Re: Export to csv with header

Thank you for your reply. I have already tried a hundred versions of the script, but the error is always the same. could not convert variant of type (array variant) into type (integer).

Post's attachments

Attachment icon PaymentsRegister m_4.zip 361.46 kb, 26 downloads since 2024-02-28 

Re: Export to csv with header

The message to you indicates the line number where the error is.
If you search the forum for the word TSaveDialog, then on the first page
you will see an example of using the file save dialog.

This will allow you to resolve the error.

5 (edited by Step-in 2024-02-29 09:58:18)

Re: Export to csv with header

And thanks again for the tips and care. It worked, but again got stuck on the data sample. That is, I need to "copy" into the temporary table only those data that are selected in Tablegrid. Any tips on how to do this? I tried this way

for i := 0 to Form1.TableGrid1.RowCount - 1 do
  begin
    SQLExecute('INSERT INTO IntermediateTable (PayeeSurname, Payment) VALUES (''' + Form1.TableGrid1.Cell[3, i].AsString + ''', ' + IntToStr(Trunc(StrToFloat(Form1.TableGrid1.Cell[4, i].AsString) * 100)) + ')');
  end;

but I understand that it is somehow broken.

Post's attachments

Attachment icon PaymentsRegister m_5.zip 361.56 kb, 19 downloads since 2024-02-29 

Re: Export to csv with header

Once again you ask a question that is different from your project (does not contain such a part) and what is broken is not clear.

You took a more universal route. By using SQL queries.
1. For such purposes, you can create a temporary SQL table (CREATE TEMPORARY TABLE ...).
   This will save you from growing the database in the future.
2. To generate the final result, pay attention to the SQL function - union.

If you export all the records that are on the screen, you just need to use a query that displays the records on the screen.

MVD also has its own function ExportToCSV(,,,,,,,).