【备忘】导出Excel,使用NativeExcel控件

本文介绍了一个用于导出数据到Excel文件的简单组件实现。该组件支持创建Excel工作簿、添加工作表、设置单元格样式及合并单元格等功能。通过示例展示了如何使用此组件填充数据并保存为Excel文件。

主要引用单元 nExcel。

unit uExportExcel;

interface

uses
  SysUtils, Windows, Classes, Graphics, nExcel;

type
  TExportExcel = class(TObject)
  private
    FFileName: string;   //Excel文件名
  protected
    procedure CreateTitle(ASheet: IXLSWorksheet; Cell1, Cell2: string; ATitle: string;
      AAlign: Integer = 1; AHeight: Integer = 14);
    
    procedure SetTitleInfo(ARange: IXLSRange; ATitle: string; AAlign: Integer = 1;
      AWidth: Integer = 20; AHeight: Integer = 14);
	  
    procedure SetCellItemInfo(ACell: IXLSRange; AValue: string; Align: Integer = 0;
      AWarp: Boolean = False);

    function AddSheet(AExcel: IXLSWorkbook; AName: string): IXLSWorksheet;
	
    procedure AddMatter(ASheet: IXLSWorksheet);

    function YMFBExportSheets(AExcel: IXLSWorkbook): Boolean;
  public
    constructor Create(AFileName: string); virtual;
    destructor Destroy; overload;

    function YMFBExecute: Boolean;
  end;


implementation  

constructor TExportExcel.Create(AFileName: string);
begin
  FFileName := AFileName;
end;

destructor TExportExcel.Destroy;
begin
  FProcessDlg.Free;
end;

//新增一个sheet
function TExportExcel.AddSheet(AExcel: IXLSWorkbook; AName: string): IXLSWorksheet;
begin
  Result := AExcel.Sheets.Add;
  Result.DisplayGridLines := True;
  Result.Cells.Font.Name := '宋体';
  Result.Cells.Font.Size := 10;
  Result.Name := AName;
end;

//创建标题行
procedure TExportExcel.CreateTitle(ASheet: IXLSWorksheet; Cell1, Cell2,
  ATitle: string; AAlign, AHeight: Integer);
begin
  //合并单元格
  ASheet.Range[Cell1, Cell2].Merge;
  SetTitleInfo(ASheet.Range[Cell1, Cell2], ATitle, AAlign, 20, AHeight);
end;

//指定标题单元格写入值
procedure TExportExcel.SetTitleInfo(ARange: IXLSRange; ATitle: string;
  AAlign, AWidth, AHeight: Integer);
begin
  With ARange do
  begin
    Font.Name := '宋体';
    Font.Size := 11;
    Font.Bold := True;      
    ColumnWidth := AWidth;     
    RowHeight := AHeight;
    //Interior.Color := clSilver;
    WrapText := True;   
    Borders.ColorIndex  := xlColorIndexAutomatic;     
    VerticalAlignment   := xlHAlignCenter;
    case AAlign of
      0: HorizontalAlignment := xlHAlignLeft;
      1: HorizontalAlignment := xlHAlignCenter;
      2: HorizontalAlignment := xlHAlignRight;
    else
      HorizontalAlignment := xlHAlignCenter;
    end;
    Value := ATitle;
  end;
end;

//指定单元格写入值
procedure TExportExcel.SetCellItemInfo(ACell: IXLSRange; AValue: string; Align: Integer = 0;
  AWarp: Boolean = False);
begin
  With ACell do
  begin
    Borders.ColorIndex := xlColorIndexAutomatic;
    Value := AValue;
    RowHeight := 13.5;
    VerticalAlignment := xlHAlignCenter;
    if AWarp then WrapText := True;
    case Align of
      1 : HorizontalAlignment := xlHAlignCenter;
      2 : HorizontalAlignment := xlHAlignRight;
    end;
  end;    
end;

//执行导出Excel
function TExportExcel.YMFBExecute Boolean;
var
  LExcel: IXLSWorkbook;
begin
  Result := False;

  LExcel:= TXLSWorkbook.Create;
  try
    YMFBExportSheets(LExcel);

    Result := LExcel.SaveAs(FFileName) = 1;
  finally
    LExcel := nil;
  end;
end;

function TExportExcel.YMFBExportSheets(AExcel: IXLSWorkbook): Boolean;
var
  LSheet: IXLSWorksheet;
begin
  Result := True;
  try
    LSheet := AddSheet(AExcel, '测试Sheet');
    AddMatter(LSheet);
  except
    Result := False;
  end;
end;

procedure TExportExcel.AddMatter(ASheet: IXLSWorksheet);
begin
  CreateTitle(ASheet, 'A1', 'C1', '测试', 1, 20);
  SetTitleInfo(ASheet.Range['A2', 'A2'], '序号', 1, 15);
  SetTitleInfo(ASheet.Range['B2', 'B2'], '名称', 1, 45);
  SetTitleInfo(ASheet.Range['C2', 'C2'], '型号规格', 1, 20);
  
  SetCellItemInfo(Item[3, 1], '1', 1);
  SetCellItemInfo(Item[3, 2], '百事可乐', 0, True);
  SetCellItemInfo(Item[3, 3], '250ML', 0, True);
  
  SetCellItemInfo(Item[4, 1], '2', 1);
  SetCellItemInfo(Item[4, 2], '可口可乐', 0, True);
  SetCellItemInfo(Item[4, 3], '250ML', 0, True);
end;

end.


上述代码为不完整的精简代码,懒,没测试了,用的时候再改改



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值