Now i get a file (6 MB), But Excel doesn't open it.
procedure TMain_frm.Button2Click(Sender: TObject);
var
workbook: TsWorkbook;
worksheet: TsWorksheet;
headerTemplate: PCell;
t: TTime;
i,j : Integer;
begin
try
workbook := TsWorkbook.Create;
try
worksheet := workbook.AddWorksheet('Sheet1');
worksheet.WriteFontStyle(0, 1, [fssBold]);
workbook.Options := [boVirtualMode, boBufStream];
workbook.VirtualColCount := DBGrid1.DataSource.DataSet.FieldCount; // Define number of columns - we want a column for each field
workbook.VirtualRowCount := DBGrid1.DataSource.DataSet.RecordCount; // Define number of rows - we want every record, plus 1 row for the title row
//headers
for i := 0 to DBGrid1.DataSource.DataSet.Fields.Count - 1 do
worksheet.WriteUTF8Text(0, i, DBGrid1.DataSource.DataSet.Fields[i].FieldName);
// Write all cells to the worksheet
{ DBGrid1.DataSource.DataSet.First;
j := 0;
while not DBGrid1.DataSource.DataSet.EOF do
begin
for i := 0 to DBGrid1.DataSource.DataSet.Fields.Count - 1 do
worksheet.WriteUTF8Text(j + 1, i, DBGrid1.DataSource.DataSet.Fields[i].AsString);
DBGrid1.DataSource.DataSet.Next;
Inc(j);
end; }
workbook.OnWriteCellData := @WriteCellDataHandler;
t := Now;
workbook.WriteToFile('test_virtual_01.xlsx', sfOOXML, true);
t := Now - t;
finally
workbook.Free;
end;
finally
//
end;
end;
procedure TMain_frm.WriteCellDataHandler(Sender: TObject; ARow, ACol: Cardinal; var AValue: variant; var AStyleCell: PCell);
var
s: String;
begin
if ARow = 0 then begin
AValue := DBGrid1.DataSource.DataSet.Fields[ACol].FieldName; //MyDatabase.Fields[ACol].FieldName;
DBGrid1.DataSource.DataSet.First;
end else begin
AValue := DBGrid1.DataSource.DataSet.Fields[ACol].AsVariant;
if ACol = DBGrid1.DataSource.DataSet.FieldCount-1 then DBGrid1.DataSource.DataSet.Next;
end;
end;