Recent

Author Topic: SQLQuery/DBGrid Export to Excel  (Read 629 times)

sfdoony

  • Newbie
  • Posts: 3
SQLQuery/DBGrid Export to Excel
« on: August 11, 2025, 07:07:51 pm »
Hi Gurus,

I tried to dump the content in DBGrid or SQLQuery to excel.

After searched on the forum I found in some posts there mentioned

    if SaveDialog1.Execute then
    with CSVExporter1.Create(nil) do begin
       //Dataset:= BufDataSet1;
       Dataset:= SQLQueryWords.DataBase.DataSets[0];
       FileName:= SaveDialog1.FileName;
       Execute;
       Free;
    end;

However, when coming to "with CSVExporter1.Create(nil) do begin", here is one fatal error: Access violation reading from address $000...

Could you please kindly advise?

Thank you.

wp

  • Hero Member
  • *****
  • Posts: 13575
Re: SQLQuery/DBGrid Export to Excel
« Reply #1 on: August 11, 2025, 07:56:17 pm »
Code: Pascal  [Select][+][-]
  1. with TCSVExporter.Create(nil) do begin
  2.    ...

CharlyTango

  • Full Member
  • ***
  • Posts: 180
Re: SQLQuery/DBGrid Export to Excel
« Reply #2 on: August 12, 2025, 11:41:00 am »
Your variable names look like you have put at least a TSaveDialog component and a TCSVExporter component on your form.

That results in Lazarus creating two variables for you: SaveDialog1 and CSVExporter1.
If my assumption is right then this code could work:

Code: Pascal  [Select][+][-]
  1.  if SaveDialog1.Execute then
  2.     with CSVExporter1 do begin
  3.        Dataset:= SQLQueryWords.DataBase.DataSets[0];
  4.        FileName:= SaveDialog1.FileName;
  5.        Execute;
  6.        Free;
  7.     end;

Lazarus will create and destroy the TCSVExporter component for you.
Otherwise the suggestion of wp will work.

my next assumption is that you saw the code at https://wiki.freepascal.org/lazdbexport but in your case https://wiki.freepascal.org/TCSVExporter could be better.



Lazarus stable, Win32/64

 

TinyPortal © 2005-2018