delphi xe10.4.2, window 10, excel 2016
从stringgrid复制到粘贴板时,如果stringgrid的单元格内有换行的话,或者有双引号,或者有逗号,空格的话,都需要在字符串的前后加上双引号,并且行内的换行符替换为#10,因为delphi认可#13#10,而excel用#10表示换行",
Procedure CopySelectCells(sg: tstringgrid);
var
i, j: integer;
s, s2, cellStr: string;
begin
clipboard.Clear;
clipboard.Open;
s2 := '';
for i := sg.Selection.Top to sg.Selection.Bottom do
begin
s := '';
for j := sg.Selection.Left to sg.Selection.Right do
begin
cellStr := sg.Cells[j, i];
// 需要循环把字符串中双引号重复一次
// cellStr := stringreplace(cellStr, '"', '""',
// [rfignorecase, rfReplaceAll]);
// 当读取commatext时,如果字符串中有空格,逗号和引号的话,都会在首尾加上双引号。
// When retrieving CommaText, any string in the list that include spaces, commas