<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[My Visual Database — [Sort of Solved]Problem exporting table grid to Excel]]></title>
		<link>https://myvisualdatabase.com/forum/viewtopic.php?id=7280</link>
		<atom:link href="https://myvisualdatabase.com/forum/extern.php?action=feed&amp;tid=7280&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in [Sort of Solved]Problem exporting table grid to Excel.]]></description>
		<lastBuildDate>Sun, 24 Jan 2021 07:26:44 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: [Sort of Solved]Problem exporting table grid to Excel]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=39848#p39848</link>
			<description><![CDATA[<p>Due to a problem somewhere in my DCU file I&#039;m unable at this time to use Derek&#039;s answer.</p><br /><p>However, I have finally managed to get my code above to work by tweaking the for loops. At this time though I&#039;m still unable to&nbsp; include column headers in the spreadsheet, unless I put them at the bottom of the form, which would be a bit unusual! <img src="https://myvisualdatabase.com/forum/img/smilies/smile.png" width="15" height="15" alt="smile" /></p><br /><p>The tweaked code is below - note the substitution of a for loop to an&nbsp; inc(rownumber,1).</p><br /><div class="codebox"><pre><code>procedure frmMain_btnExcel_OnClick (Sender: TObject; var Cancel: boolean);
var
  xls, wb, range : Variant;
  arrData : Variant;
  rowCnt : integer;
 ExcelRowNum:integer;
begin
  if frmMain.tgOrderPlace.RowCount &gt; 0 then
  begin
   
     {allocate 2d space to excel array}
     arrData := VarArrayCreate([0,frmMain.tgOrderPlace.RowCount , 0,frmMain.tgOrderPlace.Columns.Count ], varVariant);


    //excel format = row,column  table grid = column, row
     {row 0 contains tablegrid headers}
    { arrData[0,0] := frmMain.tgOrderPlace.Columns[1].Header.Caption;
     arrData[0,1] := frmMain.tgOrderPlace.Columns[2].Header.Caption;
     arrData[0,2] := frmMain.tgOrderPlace.Columns[4].Header.Caption; }

     ExcelRowNum := 0;

    {fill array with cell contents by row}
    for  rowCnt := 1 to frmMain.tgOrderPlace.RowCount  do
    begin
      arrData[ExcelRowNum,0] := frmMain.tgOrderPlace.Cells[1,rowCnt-1];
      arrData[ExcelRowNum,1] := frmMain.tgOrderPlace.Cells[2,rowCnt-1];
      arrData[ExcelRowNum,2] := frmMain.tgOrderPlace.Cells[4,rowCnt-1];
      inc (ExcelRowNum,1);
    end;


    xls := CreateOLEObject(&#039;Excel.Application&#039;);

    wb := xls.WorkBooks.add;

   range := wb.WorkSheets[1].Range[wb.WorkSheets[1].Cells[1,1],wb.WorkSheets[1].Cells[frmMain.tgOrderPlace.RowCount, frmMain.tgOrderPlace.Columns.Count]];

    Range.Value := arrData;
    Range.Columns.Autofit();  //In Excel columns must be filled first and then autofit called
    xls.Visible := true;
  end
  else
   MessageBox(&#039;Supplier has no items to order, unable to open spreadsheet.&#039;+#13+&#039;Please select a supplier with order items&#039;, &#039;INFORMATION&#039;, MB_OK);

  xls.Quit;
end;</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (CDB)]]></author>
			<pubDate>Sun, 24 Jan 2021 07:26:44 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=39848#p39848</guid>
		</item>
		<item>
			<title><![CDATA[Re: [Sort of Solved]Problem exporting table grid to Excel]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=39789#p39789</link>
			<description><![CDATA[<p>Thanks Derek,</p><br /><p>I had considered using your suggestion before I decided to go the difficult route.&nbsp; I&#039;m going to follow your suggestion.</p>]]></description>
			<author><![CDATA[null@example.com (CDB)]]></author>
			<pubDate>Mon, 18 Jan 2021 09:40:09 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=39789#p39789</guid>
		</item>
		<item>
			<title><![CDATA[Re: [Sort of Solved]Problem exporting table grid to Excel]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=39773#p39773</link>
			<description><![CDATA[<p>Hi CDB,<br />In the past when I don&#039;t want to export all columns, I&#039;ve used a hidden grid - please see attached (you might need to change the &#039;Export to Spreadsheet&#039; button configuration - it currently calls LibreOffice Calc rather than Excel - see screenshot1 in the attachment).<br />Is this approach an option for you?<br />Derek.</p>]]></description>
			<author><![CDATA[null@example.com (derek)]]></author>
			<pubDate>Sun, 17 Jan 2021 15:35:05 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=39773#p39773</guid>
		</item>
		<item>
			<title><![CDATA[[Sort of Solved]Problem exporting table grid to Excel]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=39768#p39768</link>
			<description><![CDATA[<p>I am attempting to write my own tablegrid to Excel function due to the fact I don&#039;t want all the columns exported to Excel as the inbuilt function does, and decided that hiding columns and then using the inbuilt function could raise further problems. How wrong I have been! <img src="https://myvisualdatabase.com/forum/img/smilies/smile.png" width="15" height="15" alt="smile" /></p><br /><p>To that end I &#039;borrowed&#039; some code I found on various websites by Mike Shkolnik ,</p><br /><br /><p>I am obviously not understanding something, as no matter what I try I can only get 1 item to appear in the spreadsheet in addition to the header.</p><br /><p>I filter the table grid, so that only the rows (variable in number) are exported. The tablegrid row count reflects the current number of rows correctly.</p><br /><div class="codebox"><pre><code>procedure frmMain_btnNothing_OnClick (Sender: TObject; var Cancel: boolean);
var
  xls, wb, range : Variant;
  arrData : Variant;
  rowCnt : integer;
  numOfRows,numCol:integer;
begin
  if frmMain.tgOrderPlace.RowCount &gt; 0 then
  begin
     numOfRows := frmMain.tgOrderPlace.RowCount ;
     arrData := VarArrayCreate([0,frmMain.tgOrderPlace.RowCount, 0,3], varVariant);


    //excel format = row,column  table grid = column, row

     {row 0 contains tablegrid headers}
     arrData[0,0] := frmMain.tgOrderPlace.Columns[1].Header.Caption;
     arrData[0,1] := frmMain.tgOrderPlace.Columns[2].Header.Caption;
     arrData[0,2] := frmMain.tgOrderPlace.Columns[4].Header.Caption;

    {fill array with cell contents by row}
    for  rowCnt := 1 to numOfRows do
    begin
      arrData[rowCnt,0] := frmMain.tgOrderPlace.Cells[1,rowCnt-1];
      arrData[rowCnt,1] := frmMain.tgOrderPlace.Cells[2,rowcnt-1];
      arrData[rowCnt,2] := frmMain.tgOrderPlace.Cells[4,rowcnt-1];
    end;

    xls := CreateOLEObject(&#039;Excel.Application&#039;);

    wb := xls.WorkBooks.add;

    range := wb.WorkSheets[1].Range[wb.WorkSheets[1].Cells[1,1],wb.WorkSheets[1].Cells[frmMain.tgOrderPlace.RowCount, 3]];

    Range.Value := arrData;
    Range.Columns.Autofit();  //In Excel columns must be filled first and then autofit called
    xls.Visible := true;
  end;
  //wb.Close;
  xls.Quit;
end;</code></pre></div><br /><p>The link to the original code is <a href="https://stackoverflow.com/questions/16641897/export-delphi-stringgrid-to-excel">https://stackoverflow.com/questions/166 … d-to-excel</a> .</p><p>I&#039;ve attached a snipshot of what I want to appear in Excel and what actually appears in Excel.</p>]]></description>
			<author><![CDATA[null@example.com (CDB)]]></author>
			<pubDate>Sun, 17 Jan 2021 10:50:39 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=39768#p39768</guid>
		</item>
	</channel>
</rss>
