procedure TFormExportExcel.Button2Click(Sender: TObject); //导出 var SaveDialog: TSaveDialog; XLS: TXLSReadWriteII5; i: Integer; savaPath: string; begin SaveDialog := TSaveDialog.Create(nil); XLS := TXLSReadWriteII5.Create(nil); if RadioGroup1.ItemIndex = 0 then SaveDialog.Filter := 'EXCEL(*.xls)|*.xls'; if RadioGroup1.ItemIndex = 1 then SaveDialog.Filter := '文本格式(*.txt)|*.txt'; if SaveDialog.Execute then begin // cxgrid自带导出方法 // if RadioGroup1.ItemIndex = 0 then // ExportGridToExcel(SaveDialog.FileName, cxGrid1, true, true, true, 'xls'); // // if RadioGroup1.ItemIndex = 1 then // ExportGridToText(SaveDialog.FileName, cxGrid1, true, true, 'txt'); if Pos('xls', SaveDialog.FileName) > 0 then XLS.Filename := SaveDialog.FileName else XLS.Filename := SaveDialog.FileName + '.xls'; cxGrid1DBTableView1.DataController.DataSet.DisableControls; i := 0; try cxGrid1DBTableView1.DataController.DataSet.First; while not cxGrid1DBTableView1.DataController.DataSet.Eof do begin XLS.Sheets[0].AsString[0, i] := cxGrid1DBTableView1.DataController.DataSet.FieldByName('param1').AsString; XLS.Sheets[0].AsString[1, i] := cxGrid1DBTableView1.DataController.DataSet.FieldByName('param2').AsString; XLS.Sheets[0].AsString[2, i] := cxGrid1DBTableView1.DataController.DataSet.FieldByName('param3').AsString; // XLS.CmdFormat.BeginEdit(XLS[0]); // XLS.CmdFormat.Border.Color.RGB := $0000FF; // XLS.CmdFormat.Border.Style := cbsThick; // XLS.CmdFormat.Border.Side[cbsLeft] := True; // XLS.CmdFormat.Border.Side[cbsRight] := True; // XLS.CmdFormat.ApplyCols(1, 3); cxGrid1DBTableView1.DataController.DataSet.Next; Inc(i); end; finally cxGrid1DBTableView1.DataController.DataSet.EnableControls; end; XLS.Write; end; SaveDialog.Free; XLS.Destroy; end;