Recent

Author Topic: Copy to clipboard and paste in Excel  (Read 2500 times)

vertnik

  • New member
  • *
  • Posts: 15
Copy to clipboard and paste in Excel
« on: October 01, 2018, 03:03:21 pm »
Hy!

I have a question about possibility of coping the worksheet content into Windows clipboard and
paste it to Microsoft Excel?
I found the following routine CopyToClipboardStream, but I am not sure if this procedure can do that?

Thanks,

Robert

wp

  • Hero Member
  • *****
  • Posts: 5652
Re: Copy to clipboard and paste in Excel
« Reply #1 on: October 01, 2018, 03:08:51 pm »
I suppose you have your data in a Worksheet Grid. Then simply select the range to be copied and press CTRL+C to copy the cells to the clipboard. In Excel click at the top-left cell of the range to which the copied cells should go and press CTRL+V.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

vertnik

  • New member
  • *
  • Posts: 15
Re: Copy to clipboard and paste in Excel
« Reply #2 on: October 01, 2018, 08:43:10 pm »
No, i create workbook and worksheet at run-time. Than fill some cells with writetext routine.

Robert

wp

  • Hero Member
  • *****
  • Posts: 5652
Re: Copy to clipboard and paste in Excel
« Reply #3 on: October 01, 2018, 09:35:42 pm »
You can extract the pretty much self-contained procedure TsWorkbookSource.CopyCellsToClipboard from unit fpspreadsheetctrls and you only must register the clipboard formats.

Note that I was not able so far to write the xlsx and ods formats to the clipboard. But the old xls format is working and can be read by Excel and LibreOffice Calc (and other programs); and for the few other programs which do not understand xls, the routine also writes the worksheet selection as HTML and CSV.

In the attachment there is an example which contains everything (tested).
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

vertnik

  • New member
  • *
  • Posts: 15
Re: Copy to clipboard and paste in Excel
« Reply #4 on: October 02, 2018, 11:10:18 am »
Thank you wp,

paste in Excel works for HTML, CSV and text formats. But the problem is
for sfExcel5 and sfExcel8 formats. When use paste, Excel returns "Microsoft Excel cannot paste the data."
However, using html format looks just fine.

Robert

wp

  • Hero Member
  • *****
  • Posts: 5652
Re: Copy to clipboard and paste in Excel
« Reply #5 on: October 02, 2018, 11:19:51 am »
Strange... I tested with Excel 2016, and when I wrote the clipboard support I had Excel 2007 - both of them accept the xls formats in the clipboard. Is there a configuration issue? What is your Exel and Windows version?
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

vertnik

  • New member
  • *
  • Posts: 15
Re: Copy to clipboard and paste in Excel
« Reply #6 on: October 02, 2018, 11:34:43 am »
Windows 8.1
Excel 2010

wp

  • Hero Member
  • *****
  • Posts: 5652
Re: Copy to clipboard and paste in Excel
« Reply #7 on: October 02, 2018, 01:35:56 pm »
I also have Excel97 on a VM with Windows 2000, and it also behaves correctly. So, I think that, in general, clipboard should work for xls for "all" Excel and Windows versions. But there are some xls features which fpspreadsheet does not support or maybe are not written correctly so that Excel refuses to load the stream (http://wiki.lazarus.freepascal.org/FPSpreadsheet#Progress_by_supported_cell_content).

Does my demo posted above already have the xls issues for you? Your answer was not specific on this aspect.
What exactly is the cell content/formatting that you copy to the clipboard when the xls issue happens? Images? Comments? Hyperlinks?

The structure of the data copied to the clipboard is exactly the same as in a written xls file. So, just to make sure, please save the spreadsheet to xls format (sfExel8 or sfExcel5) and post the file here for me to have a closer look (or if you don't want to publish it send me a PM and I can give you my e-mail address)

Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

vertnik

  • New member
  • *
  • Posts: 15
Re: Copy to clipboard and paste in Excel
« Reply #8 on: October 02, 2018, 02:52:48 pm »
I just run your demo you posted, where you put all format variants of copytoclipboard procedure. When I use the line

CopyToClipboard(stream, sfExcel8, cfBiff8Format);

I got the error in Excel.