Сохранить StringGrid в Excel файле
Сохранить StringGrid в Excel файле
Сохранить StringGrid в Excel файле function saveasexcelfile(stringgrid: tstringgrid; filename: string): boolean; const xlwbatworksheet = -4167; var row, col: integer; gridprevfile: string; xlapp, sheet: olevariant; begin result := false; xlapp := createoleobject('excel.application'); try xlapp.visible := false; xlapp.workbooks.add(xlwbatworksheet); sheet := xlapp.workbooks[1].worksheets[1]; sheet.name := 'my sheet name'; for col := 0 to stringgrid.colcount - 1 do for row := 0 to stringgrid.rowcount - 1 do sheet.cells[row + 1, col + 1] := stringgrid.cells[col, row]; try xlapp.workbooks[1].saveas(filename); result := true; except // error ? end; finally if not varisempty(xlapp) then begin xlapp.displayalerts := false; xlapp.quit; xlapp := unassigned; sheet := unassigned; end; end; end; procedure tform1.button1click(sender: tobject); begin if saveasexcelfile(stringgrid1, 'c:\myexcelfile.xls') then showmessage('stringgrid saved!'); end; {*************************************************} // reiner schlay http://www.assu-assist.nl/ function reftocell(arow, acol: integer): string; begin result := chr(ord('a') + acol - 1) + i2s(arow); end; procedure stringgridtoexcel(agrid: tstringgrid); var i, j: integer; excel: application_; book: workbook; sheet: _worksheet; data: olevariant; begin 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]; excel := coapplication_.create; book := excel.workbooks.add(emptyparam, 0); sheet := excel.sheets.add(emptyparam, emptyparam, 1, xlworksheet, 0) as _worksheet; sheet.range[reftocell(1, 1), reftocell(agrid.rowcount, agrid.colcount)].value := data; excel.visible[0] := true; excel.usercontrol := true; end;