对不起,我很无知,我想知道为什么将 constlWBATWorkSheet
设置为 number -4167
。谢谢,杰克。
function TForm1.SaveAsExcelFile(AGrid: TStringGrid; ASheetName, AFileName: string): boolean;
const
xlWBATWorksheet = -4167;
var
righe, colonne: Integer;
GridPrevFile: string;
XLApp, Sheet, Data: OLEVariant;
l, s: Integer;
begin
// Prepare Data
Data := VarArrayCreate([1, AGrid.RowCount, 1, AGrid.ColCount], varVariant);
for l := 0 to AGrid.ColCount - 1 do
for s := 0 to AGrid.RowCount - 1 do
Data[s + 1, l + 1] := AGrid.Cells[l, s];
// Create Excel-OLE Object
Result := False;
XLApp := CreateOleObject('Excel.Application');
try
// Hide Excel
XLApp.Visible := False;
// Add new Workbook
XLApp.Workbooks.Add(xlWBatWorkSheet);
Sheet := XLApp.Workbooks[1].WorkSheets[1];
Sheet.Name := ASheetName;
// Fill up the sheet
Sheet.Range[RefToCell(1, 1), RefToCell(AGrid.RowCount,AGrid.ColCount)].Value := Data;
// Save Excel Worksheet
try
XLApp.Workbooks[1].SaveAs(AFileName);
Result := True;
except
ShowMessage('Fatal Error!');
end;
finally
// Quit Excel
if not VarIsEmpty(XLApp) then
begin
XLApp.DisplayAlerts := False;
XLApp.Quit;
XLAPP := Unassigned;
Sheet := Unassigned;
end;
end;
end;