Тема: запись excel
Показать сообщение отдельно
Старый 16.09.2013, 11:21     # 1
fcdk_pavel
Guest
 
Сообщения: n/a

запись excel

Код:
unit Unit7;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,  StdCtrls, Grids, DBGrids, DB, DBTables;

type
  TForm7 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    StringGrid1: TStringGrid;
    SaveDialog1: TSaveDialog;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);

  private
       function   GetExcelSaveAs: String;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form7: TForm7;

implementation

uses Unit6, Unit5, ComObj;

{$R *.dfm}

    function TForm7.GetExcelSaveAs: String;
begin
Result:= SaveDialog1.FileName + '.xls';
end;

    procedure TForm7.Button1Click(Sender: TObject);
var
ExcelApp, Sheet: variant;
Col, Row: Word;
  begin
if SaveDialog1.Execute then
  begin
ExcelApp:= CreateOleObject('Excel.Application');
  try
ExcelApp.Visible:= False;
ExcelApp.Workbooks.Add;
Sheet:= ExcelApp.ActiveWorkBook.WorkSheets[1];
  for Col:= 0 to StringGrid1.ColCount -1 do
  for Row:= 0 to StringGrid1.RowCount -1 do
Sheet.Cells[Row + 1, Col +1]:= StringGrid1.Cells[Col, Row];
ExcelApp.ActiveWorkbook.SaveAs(GetExcelSaveAs);
  finally
ExcelApp.Application.Quit;
ExcelApp:= Unassigned;
Sheet:= Unassigned;
    end;
  end;
ShowMessage('Сохранение завершено!');
end;


procedure TForm7.Button2Click(Sender: TObject);

begin
 StringGrid1.RowCount:=31;
 StringGrid1.ColCount:=9;
 StringGrid1.ColWidths[1]:=150;
 StringGrid1.ColWidths[2]:=85;
 StringGrid1.ColWidths[3]:=85;
 StringGrid1.ColWidths[4]:=85;
 StringGrid1.ColWidths[5]:=90;
 StringGrid1.ColWidths[6]:=85;
 StringGrid1.ColWidths[7]:=85;
 StringGrid1.ColWidths[8]:=85;
 StringGrid1.Cells[1,0]:='Дата';
 StringGrid1.Cells[2,0]:='Вода питьевая';
 StringGrid1.Cells[3,0]:='Вода техническая';
 StringGrid1.Cells[4,0]:='Вода оборотная';
 StringGrid1.Cells[5,0]:='Собственные нужды';
 StringGrid1.Cells[6,0]:='Итог по сводке';
 StringGrid1.Cells[7,0]:='Итог по цеху';
 StringGrid1.Cells[1,1]:=Form5.Edit3.Text+Form5.ComboBox1.Text+'2013';
 StringGrid1.Cells[2,1]:=Form6.edit1.Text;
 StringGrid1.Cells[3,1]:=Form6.edit2.Text;
 StringGrid1.Cells[4,1]:=Form6.edit3.Text;
 StringGrid1.Cells[5,1]:=Form6.edit4.Text;
 StringGrid1.Cells[6,1]:=Form6.edit8.Text;
 StringGrid1.Cells[7,1]:=Form6.edit10.Text;
 end;


end.
Вот кусок кода который создает файл Экселя Как сделать так чтобы при следующем расчете программы(в предыдущих модулях) новые расчеты попадали уже в созданный файл(добавление в файл новых результатов расчета) а старые не исчезал(не перезаписывались)??? Помогите пожалуйста!