Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

1

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

unit Unit1;
{$mode objfpc}{$H+}
interface
uses
LCLType, Types,
LCLIntf, db,
LazUTF8, LConvEncoding,
sqldb,
fpstypes, fpspreadsheet, fpsallformats, laz_fpspreadsheet,
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
DBGrids, DbCtrls, Grids, StdCtrls, ExtCtrls, Buttons;

type
TMyDBGrid = class(TDBGrid); //Create a new class to access the protected properties
type
{ Tfprincipal }
Tfprincipal = class(TForm)
cbempresa: TDBLookupComboBox;
cbplanilla: TDBLookupComboBox;
cbsucursal: TDBLookupComboBox;
g: TDBGrid;
DBNavigator1: TDBNavigator;
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
GroupBox3: TGroupBox;
imglst2: TImageList;
sbexcel: TSpeedButton;
procedure Button1Click(Sender: TObject);
procedure cbplanillaChange(Sender: TObject);
procedure cbsucursalChange(Sender: TObject);
procedure ckbokClick(Sender: TObject);
procedure cbempresaChange(Sender: TObject);
procedure gMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure gTitleClick(Column: TColumn);
procedure gUserCheckboxState(Sender: TObject; Column: TColumn;
var AState: TCheckboxState);
procedure FormActivate(Sender: TObject);
procedure
procedure
procedure
procedure

SaveBoolean;
checkall;
sbexcelClick(Sender: TObject);
uncheckall;

procedure xsql(xempresa : String ; xplanilla : String ; xsucursal : String);


procedure generaexcel;

procedure SetUpFields(Q: TSqlQuery);


procedure GetText(Sender: TField; var aText: string;
Displayext: Boolean);
procedure SetText(Sender: TField; const aText: string);
private
{ private declarations }
RecList: TBookmarklist;
public
{ public declarations }
end;
var
fprincipal: Tfprincipal;
incrementar : Integer;

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

implementation
uses modulo;
{$R *.lfm}
{ Tfprincipal }

procedure Tfprincipal.generaexcel;
var
vgrabarexcel : TSaveDialog;
MyWorkbook: TsWorkbook;
MyWorksheet: TsWorksheet;
OUTPUT_FORMAT : TsSpreadsheetFormat ;
MyDir, s, x : string;
i, j, ruc: Integer ;
TempBookmark: TBookMark;
begin
// Initialization
MyDir := ExtractFilePath(ParamStr(0));
vgrabarexcel := TSaveDialog.Create(Nil);
// Create the spreadsheet
MyWorkbook := TsWorkbook.Create;
try
MyWorksheet := MyWorkbook.AddWorksheet('My Worksheet');
g.Datasource.Dataset.DisableControls;
with g.SelectedRows do
if Count > 0 then
begin
TempBookmark:= g.Datasource.Dataset.GetBookmark;
for i:= 0 to Count - 1 do
begin
if IndexOf(Items[i]) > -1 then
begin
g.Datasource.Dataset.Bookmark:= Items[i];
MyWorksheet.WriteUTF8Text(count + 1 - i, 0,
g.Datasource.Dataset.Fields[1].AsString);
MyWorksheet.WriteUTF8Text(count + 1 - i, 1,
ISO_8859_1ToUTF8(g.Datasource.Dataset.Fields[2].AsString));
MyWorksheet.WriteUTF8Text(count + 1 - i, 2,
ISO_8859_1ToUTF8(g.Datasource.Dataset.Fields[3].AsString));
end;
end;
g.Datasource.Dataset.GotoBookmark(TempBookmark);
g.Datasource.Dataset.FreeBookmark(TempBookmark);
end;
g.DataSource.DataSet.First;
g.Datasource.Dataset.EnableControls;
vgrabarexcel.Filter := 'Libro de Excel (*.xlsx)|*.xlsx';
OUTPUT_FORMAT := sfOOXML;
vgrabarexcel.FileName:= 'PERSONAL DESCANSOS' + FormatDateTime('ddmmyyyhhmmss',
Now);
if vgrabarexcel.Execute then
MyWorkbook.WriteToFile(vgrabarexcel.FileName , OUTPUT_FORMAT);
finally
dm.qpersonal.EnableControls;
MyWorkbook.Free;
vgrabarexcel.Free;
ShowMessage('Proceso terminado ..!');
end;
end;

procedure Tfprincipal.xsql(xempresa : String ; xplanilla : String ; xsucursal :


String);
begin

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

dm.qpersonal.Close;
dm.qpersonal.SQL.Text:= 'SELECT PERSONAL.COD_PERSONAL,
LTRIM(RTRIM(PERSONAL.APE_PATERNO)) + '' '' + ' +
'LTRIM(RTRIM(PERSONAL.APE_MATERNO)) + '', '' +
LTRIM(RTRIM(PERSONAL.NOM_TRABAJADOR)) AS NOMBRE, ' +
'SUCURSAL.NOM_SUCURSAL ' +
'FROM PLA_PERSONAL PERSONAL ' +
'inner join MAE_SUCURSAL SUCURSAL ON
SUCURSAL.COD_SUCURSAL = PERSONAL.COD_SUCURSAL AND' +
' SUCURSAL.COD_EMPRESA = PERSONAL.COD_EMPRESA ' +
' WHERE PERSONAL.COD_EMPRESA = ' + QuotedStr(xempresa) +
' AND PERSONAL.TIP_ESTADO = ''AC'' AND
PERSONAL.COD_TIPO_PLANILLA = ' + QuotedStr(xplanilla) +
' AND PERSONAL.COD_SUCURSAL = ' + QuotedStr(xsucursal)+ '
ORDER BY PERSONAL.NOMBRE ASC';
dm.qpersonal.Open;
SetUpFields(dm.qpersonal);
checkall;
end;
//************** FUNCION ERROR AL CODIFICAR CARACTERES DE TEXTO *************//
procedure Tfprincipal.GetText(Sender: TField; var aText: string;
Displayext: Boolean);
begin
aText := ISO_8859_1ToUTF8(Sender.AsString);
end;
procedure Tfprincipal.SetText(Sender: TField; const aText: string);
begin
Sender.AsString := UTF8ToISO_8859_1(aText);
end;
procedure Tfprincipal.SetUpFields(Q: TSqlQuery);
var
i: Integer;
begin
if not Q.Active then
exit;
for i:=0 to Q.FieldCount-1 do begin
if (Q.Fields[i].DataType=ftFixedChar) OR
(Q.Fields[i].DataType=ftString)
then begin
Q.Fields[i].OnGetText:=@GetText;
Q.Fields[i].OnSetText:=@SetText;
end;
end;
end;

procedure Tfprincipal.checkall;
begin
with g.Datasource.Dataset do
begin
DisableControls;
try
First;
while not eof do
begin
g.SelectedRows.CurrentRowSelected := true;
next;
end;
finally
First;
EnableControls;
end
end;
end;
procedure Tfprincipal.sbexcelClick(Sender: TObject);
begin
generaexcel;
end;

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281

procedure Tfprincipal.uncheckall;
begin
with g.Datasource.Dataset do
begin
DisableControls;
try
First;
while not eof do
begin
g.SelectedRows.CurrentRowSelected := false;
next;
end;
finally
First;
EnableControls;
end
end;
end;
procedure Tfprincipal.FormActivate(Sender: TObject);
begin
cbempresa.KeyValue:= '0001'; cbplanilla.KeyValue:= '01'; cbsucursal.KeyValue:=
'001';
xsql(cbempresa.KeyValue, cbplanilla.KeyValue, cbsucursal.KeyValue);
RecList := g.SelectedRows;
end;
procedure Tfprincipal.SaveBoolean;
begin
//Self.DBGrid1.SelectedField.Dataset.edit;
RecList.CurrentRowSelected := not RecList.CurrentRowSelected;
//Self.DBGrid1.SelectedField.Dataset.Post;
end;
procedure Tfprincipal.Button1Click(Sender: TObject);
var
x: word;
TempBookmark: TBookMark;
begin
{Memo1.Lines.Clear;
g.Datasource.Dataset.DisableControls;
with g.SelectedRows do
if Count > 0 then
begin
TempBookmark:= g.Datasource.Dataset.GetBookmark;
Memo1.Lines.BeginUpdate;
for x:= 0 to Count - 1 do
begin
if IndexOf(Items[x]) > -1 then
begin
g.Datasource.Dataset.Bookmark:= Items[x];
Memo1.Lines.Add(g.Datasource.Dataset.Fields[1].AsString);
end;
end;
Memo1.Lines.EndUpdate;
g.Datasource.Dataset.GotoBookmark(TempBookmark);
g.Datasource.Dataset.FreeBookmark(TempBookmark);
end;
g.DataSource.DataSet.First;
g.Datasource.Dataset.EnableControls;}
end;
procedure Tfprincipal.cbplanillaChange(Sender: TObject);
begin
xsql(cbempresa.KeyValue, cbplanilla.KeyValue, cbsucursal.KeyValue);
end;
procedure Tfprincipal.cbsucursalChange(Sender: TObject);
begin
xsql(cbempresa.KeyValue, cbplanilla.KeyValue, cbsucursal.KeyValue);
end;

282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333

procedure Tfprincipal.ckbokClick(Sender: TObject);


begin
if RecList.CurrentRowSelected then uncheckall
else checkall;
end;
procedure Tfprincipal.cbempresaChange(Sender: TObject);
begin
xsql(cbempresa.KeyValue, cbplanilla.KeyValue, cbsucursal.KeyValue);
end;
procedure Tfprincipal.gMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
Coords : TGridCoord;
col , row : Integer;
begin
Coords := g.MouseCoord(X, Y);
col := Coords.x;
row := Coords.y;
if ((col = 1) and (row > 0))
then SaveBoolean;
if ((col = 1) and (row = 0))
then begin
if g.SelectedRows.Count > 0 then uncheckall
else checkall;
end;
Caption := Format('Col: %d, Row: %d', [col, row]);
end;
procedure Tfprincipal.gTitleClick(Column: TColumn);
begin
{if (Column.Field.DataType = ftBoolean) then
if g.SelectedRows.Count > 0 then uncheckall
else checkall; }
end;
procedure Tfprincipal.gUserCheckboxState(Sender: TObject;
Column: TColumn; var AState: TCheckboxState);
begin
if RecList.CurrentRowSelected then
AState := cbChecked
else
AState := cbUnchecked;
end;
end.

You might also like