G
gz85521734
Unregistered / Unconfirmed
GUEST, unregistred user!
以下代码是将StringGrid的内容通过剪切板倒到excel里边,但是倒出后却只有StringGrid的前十多条数据呀,奇怪
procedure QuickSaveToExcelFile;
var ExcelHWND : THandle; //handle for detect whether Excel had already open
ExcelApp : TExcelApplication;
ExcelWbk : TExcelWorkBook;
ExcelSht : TExcelWorkSheet;
SaveDialog : TSaveDialog;
ExcelOpened : Boolean;
i,j : integer;
StringTemp : String;
begin
ExcelApp := TExcelApplication.Create(nil);
ExcelApp.Connect;
ExcelWbk := TExcelWorkBook.Create(nil);
ExcelWbk.ConnectTo(ExcelApp.Workbooks.Add(EmptyParam,0));
ExcelSht := TExcelWorkSheet.Create(nil);
ExcelSht.ConnectTo(ExcelWbk.Worksheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _worksheet);
ExcelSht.Columns.AutoFit;
StringTemp:='';
for i := 0 to StringGrid.ColCount - 1 do
begin
for j := 0 to StringGrid.RowCount - 1 do
StringTemp := StringTemp + StringGrid.Cells[j,i] + #9;
StringTemp := StringTemp + #13;
end;
clipboard.Clear;
clipboard.Open;
clipboard.astext := StringTemp;
clipboard.Close;
ExcelSht.paste;
//弹出保存窗口
SaveDialog := TSaveDialog.Create(nil);
SaveDialog.Filter := 'Excel Files|*.xls';
if SaveDialog.Execute then
ExcelSht.SaveAs(SaveDialog.FileName);
//释放控件
SaveDialog.Free;
ExcelSht.Disconnect;
ExcelWbk.Close;
ExcelWbk.Disconnect;
ExcelApp.Disconnect;
ExcelSht.Free;
ExcelWbk.Free;
ExcelApp.Quit;
ExcelApp.Free;
end;
procedure QuickSaveToExcelFile;
var ExcelHWND : THandle; //handle for detect whether Excel had already open
ExcelApp : TExcelApplication;
ExcelWbk : TExcelWorkBook;
ExcelSht : TExcelWorkSheet;
SaveDialog : TSaveDialog;
ExcelOpened : Boolean;
i,j : integer;
StringTemp : String;
begin
ExcelApp := TExcelApplication.Create(nil);
ExcelApp.Connect;
ExcelWbk := TExcelWorkBook.Create(nil);
ExcelWbk.ConnectTo(ExcelApp.Workbooks.Add(EmptyParam,0));
ExcelSht := TExcelWorkSheet.Create(nil);
ExcelSht.ConnectTo(ExcelWbk.Worksheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _worksheet);
ExcelSht.Columns.AutoFit;
StringTemp:='';
for i := 0 to StringGrid.ColCount - 1 do
begin
for j := 0 to StringGrid.RowCount - 1 do
StringTemp := StringTemp + StringGrid.Cells[j,i] + #9;
StringTemp := StringTemp + #13;
end;
clipboard.Clear;
clipboard.Open;
clipboard.astext := StringTemp;
clipboard.Close;
ExcelSht.paste;
//弹出保存窗口
SaveDialog := TSaveDialog.Create(nil);
SaveDialog.Filter := 'Excel Files|*.xls';
if SaveDialog.Execute then
ExcelSht.SaveAs(SaveDialog.FileName);
//释放控件
SaveDialog.Free;
ExcelSht.Disconnect;
ExcelWbk.Close;
ExcelWbk.Disconnect;
ExcelApp.Disconnect;
ExcelSht.Free;
ExcelWbk.Free;
ExcelApp.Quit;
ExcelApp.Free;
end;