procedure TfrmCQS_QUOTATION.sbtnOutPutClick(Sender: TObject);
var
Path: string;
fielname: string;
i: integer;
sFileName: string;
lstFileName: TStringList;
fExcel: string;
ExcelApp: Variant;
isOpen: boolean;
begin
//todo:導出申請單EXCEL進行
isopen := False;
fexcel := 'Quotation.XLS';
Path := SysUtils.ExtractFilePath(Application.ExeName);
Path := Path + 'Samples\' + fEXCEL;
sFileName := cdsMainFAT_PN.AsString;
//替換文件名中的不規則字符
lstFileName := TStringList.Create;
with lstFileName do
begin
Add('"');
Add('>');
Add('<');
Add('/');
Add('\');
Add(':');
add('?');
Add('|');
end;
for i := 0 to lstFileName.Count - 1 do
begin
if pos(lstFileName.Strings[i], sFileName) > 0 then
begin
sFileName := StringReplace(sFileName, '"', '~', [rfReplaceAll, rfIgnoreCase]);
end;
end;
lstFileName.Free;
SaveDialog.FileName := sFileName;
if SaveDialog.Execute then
fielname := SaveDialog.FileName
else
exit;
try
waitfm.TfrmWait.showWaitForm('導出EXCEL,請稍候...');
try
ExcelApp := CreateOleObject('Excel.Application'); // 創建 Excel
except
raise Exception.Create('系統可能沒有安裝Excel,請檢查!');
end;
ExcelApp.Visible := false; // 是否顯示 Excel
ExcelApp.WorkBooks.Open(path); // 打開 Excel
ExcelApp.WorkSheets[1].Activate;
with GlobeFunlibs.GetTmpCds do
begin
commandtext := 'select bh, x, y, field, descs,Lb,lbName '
+ ' from cqs_quotation_excel'
+ ' where (x is not null or y is not null or LB is not null) and Name= ' + quotedstr(self.Name)
+ ' and bs= ' + quotedstr(self.cdsMain.Name);
Open;
while not Eof do
begin
if MasterDataset.FindField(Fields[3].asstring) <> nil then
begin
if Fields[5].AsString = '' then
ExcelApp.ActiveSheet.Cells.Item[Fields[2].asinteger, Fields[1].asinteger] :=
MasterDataset.fieldbyname(Fields[3].asstring).AsString
else
begin
if Fields[5].AsString = 'chk' then
begin
for I := 1 to ExcelApp.ActiveSheet.Checkboxes.count do // Iterate
if (ExcelApp.ActiveSheet.CheckBoxes[i].Name = Fields[6].AsString) then
begin
// ShowMessage('3:' + Fields[3].asstring + #13#10 +
// MasterDataset.fieldbyname(Fields[3].asstring).AsString
// + #13#10 + Fields[1].AsString
// );
if MasterDataset.fieldbyname(Fields[3].asstring).AsString = Fields[1].AsString then
begin
ExcelApp.ActiveSheet.Checkboxes[I].value := 1;
Continue;
end;
end;
end;
if Fields[5].AsString = 'lab' then
begin
for I := 1 to ExcelApp.ActiveSheet.Labels.count do // Iterate
if (ExcelApp.ActiveSheet.Labels[I].Name = Fields[6].AsString) then
begin
ExcelApp.ActiveSheet.Labels[I].text := MasterDataset.fieldbyname(Fields[3].asstring).AsString;
Continue;
end;
end;
end;
end;
next;
end;
close;
free;
end;
ExcelApp.ActiveWorkBook.SaveAs(fielname);
isOpen := true;
finally
//ExcelApp.DisplayAlerts := False; //提示是否存盤
ExcelApp.Quit;
ExcelApp.Application.quit;
ExcelApp := Unassigned;
TfrmWait.CloseWaitform;
end;
if isOpen then
if Application.MessageBox('EXCEL導出完成,是否要打開文件?', '提示', MB_YESNO +
MB_ICONQUESTION) = IDYES then
begin
ShellExecute(Self.Handle, 'open', PChar(fielname), '', '', 1);
end;
end;