NativeExcel3使用示例

除了XLSReadWriteII5,還有個NativeExcel也是比較好的操做excel的組件,現將NativeExcel3的使用示例寫一下,如下是代碼和生成的excel表格的效果:spa

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;
相關文章
相關標籤/搜索