fpspreadsheet: Copy to clipboard as csv or text format

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4357 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2015-09-24 18:35:11 +00:00
parent 86b7e99ac9
commit c84939b956
2 changed files with 34 additions and 7 deletions

View File

@ -49,6 +49,7 @@ type
public
constructor Create(AWorkbook: TsWorkbook); override;
procedure WriteToClipboardStream(AStream: TStream); override;
procedure WriteToStream(AStream: TStream); override;
procedure WriteToStrings(AStrings: TStrings); override;
end;
@ -394,6 +395,11 @@ begin
end;
end;
procedure TsCSVWriter.WriteToClipboardStream(AStream: TStream);
begin
WriteToStream(AStream);
end;
procedure TsCSVWriter.WriteToStream(AStream: TStream);
var
n: Integer;
@ -421,7 +427,7 @@ end;
initialization
InitFormatSettings(CSVParams.FormatSettings);
RegisterSpreadFormat(TsCSVReader, TsCSVWriter, sfCSV);
RegisterSpreadFormat(TsCSVReader, TsCSVWriter, sfCSV, false, true);
end.

View File

@ -465,11 +465,12 @@ implementation
uses
Types, Math, StrUtils, TypInfo, LCLType, LCLProc, Dialogs, Forms, Clipbrd,
fpsStrings, fpsUtils, fpsNumFormat, fpsHTMLUtils;
fpsStrings, fpsUtils, fpsNumFormat, fpsHTMLUtils, fpsCSV;
var
cfBiff8Format: Integer = 0;
cfBiff5Format: Integer = 0;
cfCSVFormat: Integer = 0;
{@@ ----------------------------------------------------------------------------
Registers the spreadsheet components in the Lazarus component palette,
@ -1147,6 +1148,7 @@ var
sel: TsCellRangeArray;
cell: PCell;
stream: TStream;
csv: TsCSVParams;
begin
sel := FWorksheet.GetSelection;
if Length(sel) = 0 then
@ -1156,16 +1158,21 @@ begin
try
Clipboard.Clear;
// Ensure that the 'Biff8' clipboard format is registered
// Ensure that the Biff8 clipboard format is registered
cfBiff8Format := Clipboard.FindFormatID('Biff8');
If cfBiff8Format = 0 Then
if cfBiff8Format = 0 then
cfBiff8Format := RegisterClipboardFormat('Biff8');
// Ensure that the 'Biff5' clipboard format is registered
// dto with Biff5 clipboard format
cfBiff5Format := Clipboard.FindFormatID('Biff5');
If cfBiff5Format = 0 Then
if cfBiff5Format = 0 then
cfBiff5Format := RegisterClipboardFormat('Biff5');
// dto with CSV clipboard format
cfCSVFormat := Clipboard.FindFormatID('CSV');
if cfCSVFormat = 0 then
cfCSVFormat := RegisterClipboardFormat('CSV');
stream := TMemoryStream.Create;
try
// At first write BIFF8 format
@ -1177,7 +1184,21 @@ begin
FWorkbook.CopyToClipboardStream(stream, sfExcel5);
Clipboard.AddFormat(cfBiff5Format, stream);
// To do: HTML format, CSV format, XML format
// Then write CSV format
csv := CSVParams;
CsvParams.Delimiter := ';';
CsvParams.AutoDetectNumberFormat := false;
CsvParams.SheetIndex := FWorkbook.GetWorksheetIndex(FWorkbook.ActiveWorksheet);
FWorkbook.CopyToClipboardStream(stream, sfCSV);
Clipboard.AddFormat(cfCSVFormat, stream);
// Finally write TEXT format
CsvParams.Delimiter := #9;
FWorkbook.CopyToClipboardStream(stream, sfCSV);
Clipboard.AddFormat(CF_TEXT, stream);
CSVParams := csv;
// To do: HTML format, XML format
// I don't know which format is written by xlsx and ods natively.
finally
stream.Free;