7

我正在尝试将数据从 delphi 7 中的 stringgrid 导出到 microsoft excel。我一直在使用这段代码来做到这一点:

  objExcel := TExcelApplication.Create(nil);
  objExcel.Visible[LOCALE_USER_DEFAULT] := true;
  objWB := objExcel.workbooks.add(null,LOCALE_USER_DEFAULT);
  lineNumber := 1;

  for i:=1 to stringgrid1.rowcount-1 do begin
    for j:=0 to stringgrid1.ColCount-1 do begin
      objWB.Worksheets.Application.Cells.Item[i+lineNumber,j+1] := ''''+stringgrid1.Cells[j,i];
    end;
  end;

但是当数据很大时,需要很长时间才能完成。还有其他更快的方法可以将数据从 delphi 7 stringgrid 导出到 excel 吗?

4

3 回答 3

24

最快的方法是使用 Variant 数组,然后将整个数组传递给 Excel:

uses OleAuto;

var
  xls, wb, Range: OLEVariant;
  arrData: Variant;
  RowCount, ColCount, i, j: Integer;
begin
  {create variant array where we'll copy our data}
  RowCount := StringGrid1.RowCount;
  ColCount := StringGrid1.ColCount;
  arrData := VarArrayCreate([1, RowCount, 1, ColCount], varVariant);

  {fill array}
  for i := 1 to RowCount do
    for j := 1 to ColCount do
      arrData[i, j] := StringGrid1.Cells[j-1, i-1];

  {initialize an instance of Excel}
  xls := CreateOLEObject('Excel.Application');

  {create workbook}
  wb := xls.Workbooks.Add;

  {retrieve a range where data must be placed}
  Range := wb.WorkSheets[1].Range[wb.WorkSheets[1].Cells[1, 1],
                                  wb.WorkSheets[1].Cells[RowCount, ColCount]];

  {copy data from allocated variant array}
  Range.Value := arrData;

  {show Excel with our data}
  xls.Visible := True;
end;
于 2013-05-20T03:18:18.703 回答
4

问题是您正在为每个单元格调用 Excel 对象;在最好的情况下,这是一个缓慢的操作,因此对大量单元执行此操作将需要很长时间。不久前我有一个案例:4000 行 9 列需要大约 44 秒才能传输到 Excel。

我目前的解决方案包括创建一个 csv 文件,然后将该 csv 导入 Excel。

const
 fn = 'c:\windows\temp\csv.csv';

var
 csv: tstringlist;
 row, col: integer;
 s: string;

begin
 csv:= tstringlist.create;
 for row:= 1 to stringgrid1.rowcount do 
  begin
   s:= '';
   for col:= 0 to stringgrid1.ColCount-1 do 
    s:= s + stringgrid1.Cells[col, row-1] + ',';
   csv.add (s)
  end;

 csv.savetofile (fn);
 csv.free;

 objExcel := TExcelApplication.Create(nil);
 objExcel.workbooks.open (fn);
 deletefile (fn);
end;

另一种方法来自Mike Shkolnik,我按原样引用:

var
 xls, wb, Range: OLEVariant;
 arrData: Variant;

begin
{create variant array where we'll copy our data}
 arrData := VarArrayCreate([1, yourStringGrid.RowCount, 1, yourStringGrid.ColCount], varVariant);

 {fill array}
 for i := 1 to yourStringGrid.RowCount do
  for j := 1 to yourStringGrid.ColCount do
   arrData[i, j] := yourStringGrid.Cells[j-1, i-1];

 {initialize an instance of Excel}
 xls := CreateOLEObject('Excel.Application');

 {create workbook}
 wb := xls.Workbooks.Add;

 {retrieve a range where data must be placed}
 Range := wb.WorkSheets[1].Range[wb.WorkSheets[1].Cells[1, 1],
                              wb.WorkSheets[1].Cells[yourStringGrid.RowCount, yourStringGrid.ColCount]];

 {copy data from allocated variant array}
 Range.Value := arrData;

 {show Excel with our data}
 xls.Visible := True;
end;

我建议您尝试这两种方法,看看哪种方法更适合您的目的。

于 2013-05-20T03:19:07.713 回答
1
procedure WriteToExcel();
var
  txt : TextFile;
  Str : string;
  i : integer;
begin
  try
    SaveDialog1.FileName := 'excelFile('+FormatDateTime('yyyy-dd-mm hh-nn-ss' ,(Now))+')';
    if SaveDialog1.Execute then
      begin
        AssignFile(txt, SaveDialog1.FileName+'.csv');
        try
          if FileExists(SaveDialog1.FileName) then
            Append(txt)
          else
            ReWrite(txt);
          Str := 'title1, title2, title3, title4, title5';
          WriteLn(txt, Str);
          ShowQuery.First();
          for i:=1 to StringGrid1.RowCount do
            begin
              Str := StringGrid1.Cols[i][1] + ',';
              Str := Str + StringGrid1.Cols[i][2] + ',';
              Str := Str + StringGrid1.Cols[i][3] + ',';
              Str := Str + StringGrid1.Cols[i][4] + ',';
              Str := Str + StringGrid1.Cols[i][5];
              WriteLn(txt,  Str);
            end;
        finally
          CloseFile(txt);
        end;
      end;
  except

  end;
end;
于 2018-08-26T05:23:15.127 回答