4

I'm working on a tool which connects to a SQL Database, gets back a dataset, and displays that data in a grid. The user must be able to select a block of cells (just rectangular) and press CTRL+C to copy it to the clipboard.

How do I do this:

  • In a format that can be pasted into Excel? I'm hoping there's already something ready-made for this. It doesn't need all the clipboard features like Excel, just highlighting a rectangular group of cells and copying it to the clipboard.

  • If it can be done in a TStringGrid I would prefer to keep my functionality in that, but could also work with a component which supports this.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
  • 1
    @TLama posted an answer that works (he deleted it because he doesn't have Excel installed to test). I have tested it, and hopefully he will see this comment and come back to undelete it. – Ken White Aug 11 '12 at 19:43
  • @Ken, thanks for testing! I've fixed the code (at least what I found) and undeleted the post. – TLama Aug 11 '12 at 19:59
  • I think for insertion into Excel and most over tablets one have to make RTF or HTML table and copy them into clipboard. HTML construction is probably easier. – Arioch 'The Aug 13 '12 at 07:44

1 Answers1

7

You can try to copy you cell values as TAB delimited text, something like this code does:

procedure TForm1.Button1Click(Sender: TObject);
var
  S: string;
  X, Y: Integer;
begin
  S := '';
  for Y := StringGrid1.Selection.Top to StringGrid1.Selection.Bottom do
  begin
    for X := StringGrid1.Selection.Left to StringGrid1.Selection.Right - 1 do
      S := S + StringGrid1.Cells[X, Y] + #9;
    S := S + StringGrid1.Cells[StringGrid1.Selection.Right, Y] + sLineBreak;
  end;
  Delete(S, Length(S) - Length(sLineBreak) + 1, Length(sLineBreak));
  Clipboard.AsText := S;
end;
TLama
  • 75,147
  • 17
  • 214
  • 392
  • 2
    I was going to post an answer, saw your and wondered why it had been deleted. Then I saw your note about not having Excel, so I pretty much just checked to see if Excel would take tabbed text properly from the clipboard. It does, so it would have been unfair for me to post an answer. :-) – Ken White Aug 11 '12 at 20:06
  • 1
    The question was specifically about copying from a `TStringGrid` using Ctrl+C into the clipboard that the user can then paste into an Excel range. There's no need for any other format; your code works fine for that purpose. :-) – Ken White Aug 11 '12 at 20:48
  • 1
    @Jerry, Another way is to use natural Excel format, the XLS. I found [`this unit`](http://delphi.cz/img/nativexls/uNativeXLSExport.txt) based on the Excel file [`format description`](http://www.openoffice.org/sc/excelfileformat.pdf). There's a procedure for XLS export of a string grid, the `StringGridToXLS`, that you can try to modify for your purpose (to copy the content of the `TXLSWriter.FStream` member to clipboard instead to file), however, I can't verify it... – TLama Aug 11 '12 at 20:48
  • 2
    But tab-delimited text is pretty much universally acceptable. You can copy from NotePad and paste into any commercial text editor, etc. You can read it as a string, parse it on the tabs, and put it into another StringGrid. Using the Excel format would not allow any of the above. (Using the Excel format, for instance, wouldn't work for you anywhere; you don't have Excel. ) – Ken White Aug 11 '12 at 20:52
  • @Ken, *using the Excel format would not allow any of the above* - yup, but maybe it's the purpose. – TLama Aug 11 '12 at 20:55
  • 1
    I just think you're over-complicating your answer, which was perfect for the question asked before you started adding all the extra info. It was clear then, but has too much other stuff now. :-) – Ken White Aug 11 '12 at 20:58
  • To be quite frank, yes this perfectly answers my question as it was asked. However it would be awesome if it were in a more standard format which can be used in other scenarios too. – Jerry Dodge Aug 11 '12 at 21:03
  • 3
    @Jerry: Tab-delimited *works in other scenarios*. Excel-specific format *does not* - it would be Office app specific; tab-delimited works in Excel, Word, any text editor on the planet that recognizes tabs, etc. Besides, that's not your question here - it would be a new one instead. – Ken White Aug 12 '12 at 00:21
  • @Ken, maybe even those from other planets :-) – TLama Aug 12 '12 at 00:22
  • :-) I was going to say that, but didn't have any of those on this laptop to test first. – Ken White Aug 12 '12 at 00:23