write into the last worksheet lazarus / 49-FPSpreadsheet ---------------------------------------------------------------------- Hansvb · 2016-06-17 18:55 After opening a workbook and adding a worksheet I want to write data to the last added worksheet. The worksheets are well created but the data that is written will be placed in all worksheets and not only in the last one. How can I open an Excel file , and then export the data to the last worksheet? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TExport_To_File.Export_To_xlsx(Adbgrid: TDBGrid;  WorksheetName: String);var  workbook       : TsWorkbook;  Worksheet      : TsWorksheet;  nRecordteller  : Integer;begin  screen.Cursor:=crHourglass;  if (Active_DBGrid.DataSource = nil) or   (Active_DBGrid.DataSource.DataSet.IsEmpty = True) then    begin      screen.Cursor:=crHourglass;      MessageDlg('Het Datagrid is leeg. Export is niet mogelijk.', mtWarning,[mbOk],0);    end  else    begin     nRecordteller :=  Adbgrid.DataSource.DataSet.RecordCount;        try          workbook := TsWorkbook.Create;          workbook.ReadFromFile(SaveDialog_FileName);          try            worksheet := workbook.AddWorksheet(WorksheetName);  //add new worksheet How to write ONLY to the new worksheet and not to all the worksheets?             workbook.Options := [boVirtualMode, boBufStream];            workbook.VirtualColCount := Adbgrid.DataSource.DataSet.FieldCount;            workbook.VirtualRowCount := nRecordteller +1;             workbook.OnWriteCellData := @WriteCellDataHandler;  //see fpspreadsheet wiki                                    try            Adbgrid.DataSource.DataSet.DisableControls;               workbook.WriteToFile(SaveDialog_FileName, sfOOXML, true);  //save the file             finally              Adbgrid.DataSource.DataSet.First;  //back to the first record in the dataset              Adbgrid.DataSource.DataSet.EnableControls;          end;          finally            workbook.Free;          end;      finally          screen.Cursor:=crDefault;        end;    end;end; ---------------------------------------------------------------------- wp · 2016-06-17 19:18 Oh - I think you found a bug in virtual mode... Inituitivley I'd change the Sender of the OnWriteCellData event to the worksheet (instead of the workbook) - this way you could check whether the currently written cell is on the correct sheet. Another bug is that VirtualColCount and VirtualRowCount are properties of the workbook - they must be porperties of the worksheet. With this modification the virtualmode could be applied to several sheets. Buf before I change this let me think about it for some time because the mentioned modification will break existing code. ---------------------------------------------------------------------- Hansvb · 2016-06-17 19:24 If you change it please let me know. I will test it then.