unit U_SelExportField; interface uses {Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Grids, DBGrids, RM_FormReport, RM_PDBGrid, DB,IniFiles, RM_Common, RM_Class, RM_e_Xls, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, cxGridLevel, cxClasses, cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid; } Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, ToolWin, StdCtrls, BtnEdit, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit,DBGrids, DB, cxDBData, cxGridLevel, cxClasses, cxControls, cxGridCustomView, ADODB,StrUtils, Midas,cxGridCustomTableView, cxGridTableView, cxGridDBTableView,cxTimeEdit, cxTreeView, cxGrid,cxDBLookupComboBox,cxCalendar, cxCurrencyEdit,cxExportGrid4Link, ExtCtrls, Buttons,DBClient,FTComboBox,cxDropDownEdit,cxGridBandedTableView, cxGridDBBandedTableView,cxRichEdit,cxButtonEdit,IniFiles,WinSock; type TfrmSelExportField = class(TForm) Button1: TButton; Button2: TButton; ExportDataSource: TDataSource; ExpGrid: TcxGridDBTableView; cxGrid1Level1: TcxGridLevel; cxGrid1: TcxGrid; Panel2: TScrollBox; Label4: TLabel; CheckBox1: TCheckBox; CheckBox2: TCheckBox; procedure Button1Click(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure FormDestroy(Sender: TObject); procedure FormShow(Sender: TObject); procedure Button2Click(Sender: TObject); procedure CheckBox1Click(Sender: TObject); procedure CheckBox2Click(Sender: TObject); private procedure CreateCheckBox(); procedure ExportData(); procedure ReadINIFile(fieldname:string); procedure WriteINIFile(fieldname:string); Function IsINIFile(fieldname:string):Boolean; procedure GetExportFields(); procedure IsCheck(); procedure TcxGridToExcel(mfileName:string;gridName:TcxGrid); { Private declarations } public ExportFields,IniName:string; { Public declarations } end; var frmSelExportField: TfrmSelExportField; implementation //uses U_SelPrintField,U_FormPas; {$R *.dfm} procedure TfrmSelExportField.CreateCheckBox(); var i,j,FTop,FLeft,Fdiv,FMod:Integer;// mod 余数,div商 FCheckBox:TCheckBox; begin for i:=0 to ExpGrid.ColumnCount-1 do begin Fdiv:=(i+1) div 3; FMod:=(i+1) mod 3; FCheckBox:=TCheckBox.Create(Self); FCheckBox.Caption:=Trim(ExpGrid.Columns[i].Caption); FCheckBox.TabOrder:=i; FCheckBox.Parent:=Panel2; FCheckBox.Checked:=True; if FMod>0 then FCheckBox.Top:=36*(Fdiv+1) else FCheckBox.Top:=36*Fdiv; if FMod=1 then FCheckBox.Left:=29 else if FMod=2 then FCheckBox.Left:=163 else if FMod=0 then FCheckBox.Left:=305; end; end; procedure TfrmSelExportField.TcxGridToExcel(mfileName:string;gridName:TcxGrid); var saveDialog:TSaveDialog; begin try saveDialog:=TSaveDialog.Create(nil); saveDialog.Filter:='xls(*.xls)|*.xls|全部(*.*)|*.*'; saveDialog.Options:=[ofOverwritePrompt]; saveDialog.FileName:=mfileName; if saveDialog.Execute then if Assigned(gridName) then begin try ExportGrid4ToExcel(saveDialog.FileName,gridName); except application.MessageBox('创建失败,源文件可能处于编辑状态!','提示信息',0); exit; end; application.MessageBox('成功导出!','提示信息',0); end else application.MessageBox('导出失败!','提示信息',0); finally saveDialog.Free; end; end; procedure TfrmSelExportField.Button1Click(Sender: TObject); begin //ShowMessage('10除以3取余'+inttostr(10 mod 3)+',取整'+inttostr(10 div 3)); ExportData(); GetExportFields(); if IsINIFile(IniName)=True then begin DeleteFile(IniName); end; WriteINIFile(IniName); end; procedure TfrmSelExportField.FormClose(Sender: TObject; var Action: TCloseAction); begin Action:=caFree; end; procedure TfrmSelExportField.FormDestroy(Sender: TObject); begin frmSelExportField:=nil; end; procedure TfrmSelExportField.FormShow(Sender: TObject); begin CreateCheckBox(); ReadINIFile(IniName); IsCheck(); end; procedure TfrmSelExportField.IsCheck(); var i:Integer; fsj:string; begin with Panel2 do begin for i:=0 to Panel2.ControlCount-1 do begin if Controls[i] is TCheckBox then begin fsj:=Trim(TCheckBox(Controls[i]).Caption); if Pos(fsj,ExportFields)>0 then TCheckBox(Controls[i]).Checked:=True else TCheckBox(Controls[i]).Checked:=False; end; end; end; end; procedure TfrmSelExportField.ExportData(); var i,j:Integer; begin j:=0; with Panel2 do begin for i:=0 to Panel2.ControlCount-1 do begin if Controls[i] is TCheckBox then begin if TCheckBox(Controls[i]).Checked=True then begin j:=1; ExpGrid.Columns[TCheckBox(Controls[i]).TabOrder].Visible:=True end else begin ExpGrid.Columns[TCheckBox(Controls[i]).TabOrder].Visible:=False; end; end; end; end; TcxGridToExcel(Trim(IniName),cxGrid1); end; procedure TfrmSelExportField.Button2Click(Sender: TObject); begin Close; end; procedure TfrmSelExportField.ReadINIFile(fieldname:string); var programIni:Tinifile; //配置文件名 FName:string; begin FName:=ExtractFilePath(Application.ExeName)+'FieldExportSet\' +trim(fieldname)+'.INI'; programIni:=Tinifile.create(FName); ExportFields:=programIni.ReadString('导出设置','导出字段',''); programIni.Free; end; procedure TfrmSelExportField.GetExportFields(); var i:Integer; begin ExportFields:='Begin'; with Panel2 do begin for i:=0 to Panel2.ControlCount-1 do begin if Controls[i] is TCheckBox then begin if TCheckBox(Controls[i]).Checked then begin ExportFields:=ExportFields+'/'+TCheckBox(Controls[i]).Caption; end; end; end; end; end; ////////////////////////////////////////////////////////////////// //写设置信息到INI文件 //参数 ////////////////////////////////////////////////////////////////// procedure TfrmSelExportField.WriteINIFile(fieldname:string); var programIni:Tinifile; //配置文件名 FName:string; begin FName:=ExtractFilePath(Application.ExeName)+'FieldExportSet\'+trim(fieldname)+'.INI'; if not DirectoryExists(ExtractFileDir(FName)) then CreateDir(ExtractFileDir(FName)); programIni:=Tinifile.create(FName); programIni.WriteString('导出设置','导出字段',ExportFields); programIni.Free; end; ////////////////////////////////////////////////////////////////// //判断InI文件是否存在 ////////////////////////////////////////////////////////////////// Function TfrmSelExportField.IsINIFile(fieldname:string):Boolean; var FName:string; begin FName:=ExtractFilePath(Application.ExeName)+'FieldExportSet\'+trim(fieldname)+'.INI'; if FileExists(FName) then Result:=True else Result:=false; end; procedure TfrmSelExportField.CheckBox1Click(Sender: TObject); var i:Integer; begin if CheckBox1.Checked then begin CheckBox2.Checked:=False; with Panel2 do begin for i:=0 to Panel2.ControlCount-1 do begin if Controls[i] is TCheckBox then begin if (TCheckBox(Controls[i]).Name<>'CheckBox1') and (TCheckBox(Controls[i]).Name<>'CheckBox2') then begin TCheckBox(Controls[i]).Checked:=True; end; end; end; end; end; end; procedure TfrmSelExportField.CheckBox2Click(Sender: TObject); var i:Integer; begin if CheckBox2.Checked then begin CheckBox1.Checked:=False; with Panel2 do begin for i:=0 to Panel2.ControlCount-1 do begin if Controls[i] is TCheckBox then begin if (TCheckBox(Controls[i]).Name<>'CheckBox1') and (TCheckBox(Controls[i]).Name<>'CheckBox2') then begin TCheckBox(Controls[i]).Checked:=False; end; end; end; end; end; end; end.