uses ComObj;function SaveAsExcelFile(AGrid: TStringGrid; ASheetName, AFileName: string): Boolean; const xlWBATWorksheet = -4167; var XLApp, Sheet, Data: OLEVariant; i, j: Integer; begin // Prepare Data Data := VarArrayCreate([1, AGrid.RowCount, 1, AGrid.ColCount], varVariant); for i := 0 to AGrid.ColCount - 1 do for j := 0 to AGrid.RowCount - 1 do Data[j + 1, i + 1] := AGrid.Cells[i, j]; // Create Excel-OLE Object Result := False; XLApp := CreateOleObject('Excel.Application'); try XLApp.Visible := true; // 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 // Error end; finally // Quit Excel if not VarIsEmpty(XLApp) then begin XLApp.DisplayAlerts := False; XLApp.Quit; XLAPP := Unassigned; Sheet := Unassigned; end; end; end;function RefToCell(ARow, ACol: Integer): string; begin Result := Chr(Ord('A') + ACol - 1) + IntToStr(ARow); end;procedure TForm1.Button1Click(Sender: TObject); begin if SaveAsExcelFile(stringGrid1, 'My Stringgrid Data', ExtractFilePath (Application.ExeName)+'MyExcelFile.xls') then ShowMessage('StringGrid saved!'); end;
var MSExcel :variant; i,j,k:integer; excel_line:integer; //excel 的文件头空行数 begin MSExcel:=createOLEobject('excel.application'); MSExcel.workBooks.add; MSExcel.visible:=false; excel_line:=3; for i:=0 to stringgrid1.ColCount do begin MSExcel.cells[excel_line+1,i+1].value:=stringgrid1.Cells[i,0] //这里能输出 end; for j:=1 to stringgrid1.RowCount do begin for i:=0 to stringgrid1.ColCount do begin k:=excel_line+j; MSExcel.cells[k,i].value:=stringgrid1.Cells[i,j]; //好像这里错了.. end; end; MSExcel.visible:=true;这是为什么呢..
const
xlWBATWorksheet = -4167;
var
XLApp, Sheet, Data: OLEVariant;
i, j: Integer;
begin
// Prepare Data
Data := VarArrayCreate([1, AGrid.RowCount, 1, AGrid.ColCount], varVariant);
for i := 0 to AGrid.ColCount - 1 do
for j := 0 to AGrid.RowCount - 1 do
Data[j + 1, i + 1] := AGrid.Cells[i, j];
// Create Excel-OLE Object
Result := False;
XLApp := CreateOleObject('Excel.Application');
try
XLApp.Visible := true;
// 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
// Error
end;
finally
// Quit Excel
if not VarIsEmpty(XLApp) then
begin
XLApp.DisplayAlerts := False;
XLApp.Quit;
XLAPP := Unassigned;
Sheet := Unassigned;
end;
end;
end;function RefToCell(ARow, ACol: Integer): string;
begin
Result := Chr(Ord('A') + ACol - 1) + IntToStr(ARow);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if SaveAsExcelFile(stringGrid1, 'My Stringgrid Data', ExtractFilePath (Application.ExeName)+'MyExcelFile.xls') then
ShowMessage('StringGrid saved!');
end;
MSExcel :variant;
i,j,k:integer;
excel_line:integer; //excel 的文件头空行数
begin
MSExcel:=createOLEobject('excel.application');
MSExcel.workBooks.add;
MSExcel.visible:=false;
excel_line:=3;
for i:=0 to stringgrid1.ColCount do
begin
MSExcel.cells[excel_line+1,i+1].value:=stringgrid1.Cells[i,0] //这里能输出
end;
for j:=1 to stringgrid1.RowCount do
begin
for i:=0 to stringgrid1.ColCount do
begin
k:=excel_line+j;
MSExcel.cells[k,i].value:=stringgrid1.Cells[i,j]; //好像这里错了..
end;
end;
MSExcel.visible:=true;这是为什么呢..