0

I have a kind of report that I`ve created with the help of TZColorStringGrid (it is a descendant of the Delphi's TStringGrid) and I want to be able to export to doc, xls, pdf and also to be able to print this report. So I am thinking for the last two days on how to do this.

I found a post here where somebody asked about how to create a TClientDataSet from contents of a TStringGrid How to create a QuickReport from the contents of a TStringGrid and the answer provided there was to use the QuickReport.OnNeedData event handler. When I read that I was happy and I said to myself that this is it, but after I have realized I don't have Quick Reports on my DelphiXe2, so this solution fails. Can I do that in a similar way with Fast Reports?

The second way I thought of solving my issue is that I use JVCL Exporting Components, but the export only works for DBGrid not for TStringGrid. So I think that I need also to create that TClientDataSet from my TStringGrid and link the TClientDataSet to a DbGrid and export it. But I don't know how to create that ClientDataSet and if will keep my TStringGrid structure like in the image below

Later Edit: TJvPrint is the Component of JVCL to print a stringgrid and it works without the need of a TClientDataSet, but it doesn't keep my table structure(I mean the merged cells, font formatting and so on)

click to zoom image

The third way I am thinking that could solve my problem is to find a free component that exports my TStringGrid. I found one SMExport but it is not free and I don't have money at all to invest in it(unfortunately I am a student yet). Do you know any other free components that you can use to export a TStringGrid?

If anyone thinks of another solution of exporting TStringGrid and if he wants to share it I will be thankfull!

Community
  • 1
  • 1
Cristian Vasuica
  • 369
  • 9
  • 22
  • 1
    Where did you get the data from ? You can use dataset descendant component to get the data, isn't it? Then you can use fastreport to design the layout. There is free component 'PrintGrid' on (http://www.torry.net/pages.php?s=73) to print stringgrid, but you may need to modify it to run on xe2. I don't know and never use it before. As for exporting to excel, word, pdf, usually it comes with commercial product. Another way is to use ole automation with excel and populate the sheet with your data. (http://codeprogrammer.blogspot.com/2007/02/export-stringgrid-to-excel-file-way-two.html) – Hendra Jun 18 '12 at 09:59
  • hi Hendra. I have several querys to get the data in the stringgrid: it is a school scheduler app what i am doing so i need them structured like in the image , and i have chosen tstringgrid because i tried to merge cells with Reporting Services and failed; also in fast reports I can't build matrix reports because i belive that this functionality is missing from my FastReport installation. I tried to use Ole automation but i have found examples on how to do it that are a few years old and it doesn't work in xe2: this example http://www.swissdelphicenter.ch/torry/showcode.php?id=379 – Cristian Vasuica Jun 18 '12 at 10:06
  • how ole doesn't work? Do you have error msg? – Hendra Jun 18 '12 at 10:17
  • `// Add new Workbook XLApp.Workbooks.Add(xlWBatWorkSheet); Sheet := XLApp.Workbooks[1].WorkSheets[1];` -- here it appears the first error"Undeclared Identifier WorkSheets", and i have added ComObj to use clause – Cristian Vasuica Jun 18 '12 at 10:32
  • Excel automation works on XE2. – David Heffernan Jun 18 '12 at 10:44
  • hi David; I am not trying to argue if it works or not because i have used only the example at swissdelphicenter.ch and it didn't work for me, but i can't tell why but it gives me that error – Cristian Vasuica Jun 18 '12 at 10:51
  • @Cristian, I just try `xlApp.workbooks[1].worksheets[1]`, and it works. I notice that you commented the previous line with //. You need to add workbook first before try to access the worksheet. Could you post the actual code you using, so we can see what's missing? BTW, you could just write `XLApp.workbooks.add;` instead of `XLApp.Workbooks.Add(xlWBatWorkSheet);` – Hendra Jun 18 '12 at 11:02
  • I recently used oExport (http://www.kluug.at/xlsx-ods-delphi.php) to export my grid to xlsx. Ok, I know it's not XLS, and can't export to DOC or PDF, but I think it's worth mentioning. – balazs Jun 18 '12 at 11:06
  • I don't know if fastreport supports pdf. I do not use fastreport. One alternative is to install free WinPDF (http://www.winpdf.com/). It is a 'PDF' printer driver, so if you print to this 'pdf printer', it will create a pdf file for you, instead of printing to paper. – Hendra Jun 18 '12 at 11:15
  • @Hendra this is my project http://uploading.com/files/df376fdb/TStringGrid%2Bto%2Bxls%2Bwith%2BOLE.rar/ with errors regarding exporting to Excel using OLE – Cristian Vasuica Jun 18 '12 at 11:54
  • I run the code and it works using excel 2007. – Hendra Jun 18 '12 at 15:12
  • @Hendra - FastReport exports to PDF (and Excel, and ODS) directly. – Leonardo Herrera Jun 18 '12 at 18:04
  • @ Hendra I am using excel 2010 but the thing is that it is not even compiling; probabily it is something wrong with my xe2 installation; but i want to ask you something: I managed to export to excel not with ole and the cells are drawned without borders and the widths are not the same as in my TStringGrid. With OLE the widths and borders are kept? p.s. thank you for you interest in my question – Cristian Vasuica Jun 18 '12 at 18:06
  • 1
    @Cristian, No, the code from torry above just populate excel worksheet with data from stringgrid. With ole, you can store the value and adjust the cell formatting (font, number format, width, border), but these have to be done programatically. see (http://www.swissdelphicenter.ch/torry/showcode.php?id=156) for example. – Hendra Jun 18 '12 at 19:54
  • I tried Excel Automation on XE2 update 4 with Office 2003 and it works. I think that what caused me the error was the usage of Offfice 2010; – Cristian Vasuica Jun 20 '12 at 06:34

1 Answers1

1
procedure TForm1.exportClick(Sender: TObject);
var
  i: Integer;
  CSV: TStrings;
  SD: TSaveDialog;
  FileName: string;
begin
  SD := TSaveDialog.Create(nil);
  try
    SD.Filter := 'CSV (*.csv)|*.CSV';
    // filters file types to only allow you to select CSV files

    if SD.Execute then
    begin
      FileName := SD.FileName;
      if ExtractFileExt(FileName) <> '.csv' then
        FileName := FileName + '.csv';

      Screen.Cursor := crHourGlass;
      try
        CSV := TStringList.create;
        try
          for i := 0 to StringGrid1.RowCount - 1 do
            CSV.Add(StringGrid1.Rows[i].CommaText);

          CSV.SaveToFile(FileName);
        finally
          CSV.free;
        end;
      finally
        Screen.Cursor := crDefault;
      end;
    end;

  finally
    SD.Free;        
  end;
end;

try that, im not sure if it'll keep the merged cells

TLama
  • 75,147
  • 17
  • 214
  • 392
Nathan
  • 19
  • 2
  • 7