除了XLSReadWriteII5,还有个NativeExcel也是比较好的操作excel的组件,现将NativeExcel3的使用示例写一下,以下是代码和生成的excel表格的效果:

procedure TForm1.Button2Click(Sender: TObject);
var
  i, n: Integer;
  XLS: IXLSWorkbook; // 引用nExcel, ShellAPI
  ws: IXLSWorksheet;
begin
  XLS := TXLSWorkbook.Create;
  try
    ws := XLS.Sheets.Add;
    ws.Name := '导出';
    // 注意NativeExcel是从1开始的,不是0开始
    for i := 1 to 10 do
      ws.Cells.Item[1, i].Value := '标题' + IntToStr(i);
    for i := 1 to 10 do
      for n := 2 to 20 do
        With ws.Cells.Item[n, i] do
        begin
          Value := IntToStr(i) + ':' + IntToStr(n - 1);
          if ColumnWidth < Length(AnsiString(Value)) then // 自动列宽
            ColumnWidth := Length(AnsiString(Value));
        end;
 
    for i := 1 to 10 do // 从第一列到最后一列
    begin
      for n := 1 to 20 do // 从第一行到最后一行
      begin
        With ws.Cells.Item[n, i] do
        begin
          Borders.LineStyle := xlContinuous;
          Borders.Color := clBlack;
          // 黑色#0
          if n = 1 then
          begin
            Interior.Color := clWebOrange; // RGB(255, 140, 0); // 橘黄#FF8000
            Font.Color := clWhite;
            HorizontalAlignment := xlHAlignCenter;
          end
          else
            Interior.Color := RGB(255, 248, 220); // 杏仁灰#FFFFCD
        end;
      end;
    end;
    XLS.SaveAs(ExtractFilePath(paramstr(0)) + 'temp.xls', xlOpenXMLWorkbook);
    ShellExecute(0, 'Open', PChar(ExtractFilePath(paramstr(0)) + 'temp.xls')
      , nil, nil, SW_SHOW);
  finally
    XLS.close;
  end;
end;