由于网页开发不能用opendialog和savedialog,给导入和导出文件造成了很大的麻烦。下面以Excel文件为例,说一下怎么导入和导出。
1.导入文件
首先在按钮的点击事件里添加如下代码。
procedure TForm_CompanyProject.btn_ImportBudgetClick(Sender: TObject);
begin
dwUpload(Self, '.xls,.xlsx', '');
end;
dwUpload完成会自动激活OnEndDock事件
procedure TForm_CompanyProject.FormEndDock(Sender, Target: TObject; X, Y: Integer);
var
sFile, gsMainDir: string;
I, j, MaxRow, MaxCol: Integer;
ExcelApp: Variant;
begin
//上传完成时激活事件
sFile := dwGetProp(TForm1(Self.Owner), '__upload');
ExcelApp := CreateOleObject('Excel.Application');
gsMainDir := ExtractFilePath(Application.ExeName);
ExcelApp.workBooks.Open(gsMainDir + 'upload\' + sFile);
ExcelApp.Worksheets[1].Activate;
MaxRow := ExcelApp.WorkSheets[1].UsedRange.Rows.Count;
MaxCol := ExcelApp.WorkSheets[1].UsedRange.Columns.count;
SG_Main.RowCount := MaxRow;
for I := 0 to MaxRow - 1 do
begin
for j := 0 to MaxCol - 1 do
begin
SG_Main.Cells[j, I] := ExcelApp.Worksheets[1].cells[I + 1, j + 1];
end;
end;
end;
2.导出文件
在按钮的点击事件里添加如下代码。点击按钮时会弹出保存文件对话框
procedure TForm_Alarm.Button_DownloadClick(Sender: TObject);
var
sFile : String;
begin
sFile := '项目预警.xlsx';
dwsgSaveToExcel(SG_Main, ExtractFilePath(Application.ExeName)+'media\temp\'+sFile,
ExtractFilePath(Application.ExeName));
dwOpenUrl(self,'/media/temp/'+sFile,'_blank');
end;