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, cxLookAndFeels, cxLookAndFeelPainters, dxSkinsCore, dxSkinsDefaultPainters, cxNavigator, dxDateRanges; 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(); { Private declarations } public ExportFields,IniName:string; { Public declarations } end; var frmSelExportField: TfrmSelExportField; implementation uses U_Fun; {$R *.dfm} procedure TfrmSelExportField.CreateCheckBox(); var i,j,FTop,FLeft,Fdiv,FMod,z:Integer;// mod 余数,div商 FCheckBox:TCheckBox; begin z:=0; for i:=0 to ExpGrid.ColumnCount-1 do begin if ExpGrid.Columns[i].Visible=True then begin Fdiv:=(z+1) div 3; FMod:=(z+1) mod 3; FCheckBox:=TCheckBox.Create(Self); FCheckBox.Caption:=Trim(ExpGrid.Columns[i].Caption); FCheckBox.Tag:=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; z:=z+1; end; 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]).Tag].Visible:=True end else begin ExpGrid.Columns[TCheckBox(Controls[i]).Tag].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.