cxgridexportlink.pas

来自「胜天进销存源码,国产优秀的进销存」· PAS 代码 · 共 1,695 行 · 第 1/5 页

PAS
1,695
字号
    function GetCard: TcxGridCard;
    function GetCategoryIndent: Integer;
    function GetDataAlignment: TAlignment;
    function GetDataBounds: TRect;
    function GetDataStyle: TcxViewParams;
    function GetDataValue: Variant;
    function GetHasIndent: Boolean;
    function GetHasSeparator: Boolean;
    function GetHeight: Integer;
    function GetIndentBounds: TRect;
    function GetIndentStyle: TcxViewParams;
    function GetSeparatorBounds: TRect;
    function GetSeparatorWidth: Integer;
    function GetShowCaption: Boolean;
    function GetShowData: Boolean;
    function GetVisibleCaption: string;
    function GetWidth: Integer;
    procedure SetHeight(AValue: Integer);
    procedure SetWidth(AValue: Integer);
  protected
    procedure AddToScales(AColumnScale, ARowScale: TcxExportScale);
  public
    Bounds: TRect;
    CaptionStyleIndex: Integer;
    CaptionWidth: Integer;
    DataStyleIndex: Integer;
    IndentStyleIndex: Integer;
    Row: TcxGridCardViewRow;
    constructor Create(AOwner: TcxExportCard);

    property CaptionBounds: TRect read GetCaptionBounds;
    property CaptionStyle: TcxViewParams read GetCaptionStyle;
    property Card: TcxGridCard read GetCard;
    property CategoryIndent: Integer read GetCategoryIndent;
    property DataAlignment: TAlignment read GetDataAlignment;
    property DataBounds: TRect read GetDataBounds;
    property DataStyle: TcxViewParams read GetDataStyle;
    property DataValue: Variant read GetDataValue;
    property HasIndent: Boolean read GetHasIndent;
    property HasSeparator: Boolean read GetHasSeparator;
    property Height: Integer read GetHeight write SetHeight;
    property IndentBounds: TRect read GetIndentBounds;
    property IndentStyle: TcxViewParams read GetIndentStyle;
    property Owner: TcxExportCard read FOwner;
    property SeparatorBounds: TRect read GetSeparatorBounds; 
    property SeparatorWidth: Integer read GetSeparatorWidth;
    property ShowCaption: Boolean read GetShowCaption;
    property ShowData: Boolean read GetShowData;
    property VisibleCaption: string read GetVisibleCaption;
    property Width: Integer read GetWidth write SetWidth;
  end;

  TcxExportCard = class
  protected
    FBounds: TRect; 
    FCard: TcxGridCard;
    FLayersList: TcxObjectList;
    FOwner: TcxGridCardViewExport;
    function GetBorderWidth: Integer;
    function GetHasSeparators: Boolean;
    function GetLayer(AIndex: Integer): TList;
    function GetLayerCount: Integer;
    function GetLayerSeparator(AIndex: Integer): TRect;
    function GetRow(ALayerIndex, ARowIndex: Integer): TcxExportCardRow;
    function GetRowCount(ALayerIndex: Integer): Integer;
    function GetSeparatorWidth: Integer;
    procedure SetBounds(const ABounds: TRect);
  protected
    function AddLayer: TList;
    function AddRow(ALayerIndex: Integer; ARow: TcxGridCardViewRow): TcxExportCardRow;
    procedure AddLayerSeparators;
    procedure AdjustLayersWidthToWidth;
    function AdjustRowsHeightInLayer(ALayer, ATop: Integer): Integer;
    procedure AdjustRowsWidthToWidth(ALayer: Integer);
    procedure CheckCategorySeparators(AHorizontalLayout: Boolean);
  public
    constructor Create(AOwner: TcxGridCardViewExport; ACard: TcxGridCard);
    destructor Destroy; override;
    procedure AddToScales(AColumnScale, ARowScale: TcxExportScale);
    procedure CalculateLayersCaptionWidth(AWidths: TcxExportIntList; AFistRowInLayerOnly: Boolean);
    function GetRowCaptionWidth(ALayerIndex, ARowIndex: Integer; AMaxWidth: Integer = 0): Integer;
    procedure SetLayersCaptionWidth(AWidths: TcxExportIntList; AFistRowInLayerOnly: Boolean);
    procedure SetRowCaptionWidth(ALayerIndex, ARowIndex, AWidth: Integer);

    property BorderWidth: Integer read GetBorderWidth;
    property Bounds: TRect read FBounds write SetBounds;
    property Card: TcxGridCard read FCard;
    property HasSeparators: Boolean read GetHasSeparators;
    property LayerCount: Integer read GetLayerCount;
    property Layers[AIndex: Integer]: TList read GetLayer;
    property LayerSeparators[Index: Integer]: TRect read GetLayerSeparator;
    property LayersList: TcxObjectList read FLayersList;
    property Owner: TcxGridCardViewExport read FOwner;
    property RowCount[ALayerIndex: Integer]: Integer read GetRowCount;
    property Rows[ALayerIndex, ARowIndex: Integer]: TcxExportCardRow read GetRow;
    property SeparatorWidth: Integer read GetSeparatorWidth;
  end;

  TcxExportCardLayoutBuilder = class
  private
    FExportCard: TcxExportCard;
    FOwner: TcxGridCardViewExport;
    FRowsList: TList;
  protected
    function GetLayerIndex(ARow: TcxGridCardViewRow): Integer; virtual;
    procedure SplitRowsToLayers; virtual; 
  public
    constructor Create(AOwner: TcxGridCardViewExport);
    destructor Destroy; override;
    procedure BuildLayout(ACard: TcxGridCard; AExportCard: TcxExportCard);
    
    property ExportCard: TcxExportCard read FExportCard;  
    property Owner: TcxGridCardViewExport read FOwner;
    property RowsList: TList read FRowsList;
  end;

  TcxGridCardViewExport = class(TcxGridCustomExport)
  private
    FCardBorderStyle: Integer;
    FCardSeparators: TcxExportScale;
    FCardSeparatorStyleIndex: Integer;

    FCategorySeparatorStyleIndex: Integer;
    FColumnCardCount: Integer;
    FKeepRowsSameHeight: Boolean;
    FLayerSeparatorStyleIndex: Integer;
    FLayoutBuilder: TcxExportCardLayoutBuilder;
    FRowCardCount: Integer;
    FExportCardsList: TcxObjectList;
    function GetCard(AIndex: Integer): TcxGridCard;
    function GetCardBorderWidth: Integer;
    function GetCardCount: Integer;
    function GetCardHeight: Integer;
    function GetCardIndent: Integer;
    function GetCardSeparator(AIndex: Integer): TRect;
    function GetCardSeparatorCount: Integer;
    function GetCardWidth: Integer;
    function GetCategoryIndent: Integer;
    function GetCategorySeparatorWidth: Integer;
    function GetExportCard(AIndex: Integer): TcxExportCard;
    function GetGridView: TcxGridCardView;
    function GetInterCardHorzSpace: Integer;
    function GetInterCardVertSpace: Integer;
    function GetIsHorizontalRows: Boolean;
    function GetIsSimpleLayout: Boolean;
    function GetLayerSeparatorWidth: Integer;
    function GetLayoutDirection: TcxGridCardViewLayoutDirection;
    function GetOptionsView: TcxGridCardViewOptionsView;
  protected
    procedure AddCardSeparator(APosition: Integer);
    function AddExportCard(AColumnPosition, ARowPosition: Integer; ACard: TcxGridCard): TcxExportCard;
    procedure AdjustRowCaptionWidth;
    procedure CalculateVisibleInfo;
    function CreateCardLayoutBuilder: TcxExportCardLayoutBuilder;
    procedure CreateExportCells; override;
    procedure ExportCardRow(ACard: TcxExportCard; ARow: TcxExportCardRow; const ACardLogicalBounds: TRect);
    procedure ExportCells; override;
    procedure Finalize; override;
    procedure RegisterStyles; override;
    procedure SetRowSameHeight; virtual;

    property ExportCardsList: TcxObjectList read FExportCardsList;
  public
    property CardBorderStyle: Integer read FCardBorderStyle;
    property CardBorderWidth: Integer read GetCardBorderWidth;
    property CardCount: Integer read GetCardCount;
    property CardHeight: Integer read GetCardHeight;
    property CardIndent: Integer read GetCardIndent;
    property Cards[Index: Integer]: TcxGridCard read GetCard;
    property CardSeparatorCount: Integer read GetCardSeparatorCount;
    property CardSeparators[Index: Integer]: TRect read GetCardSeparator;
    property CardSeparatorStyleIndex: Integer read FCardSeparatorStyleIndex;
    property CardWidth: Integer read GetCardWidth;
    property CategoryIndent: Integer read GetCategoryIndent;
    property CategorySeparatorStyleIndex: Integer read FCategorySeparatorStyleIndex;
    property CategorySeparatorWidth: Integer read GetCategorySeparatorWidth;
    property ColumnCardCount: Integer read FColumnCardCount write FColumnCardCount;
    property ExportCards[Index: Integer]: TcxExportCard read GetExportCard;
    property GridView: TcxGridCardView read GetGridView;
    property InterCardHorzSpace: Integer read GetInterCardHorzSpace;
    property InterCardVertSpace: Integer read GetInterCardVertSpace;
    property IsHorizontalRows: Boolean read GetIsHorizontalRows;
    property IsSimpleLayout: Boolean read GetIsSimpleLayout;
    property KeepRowsSameHeight: Boolean read FKeepRowsSameHeight;
    property LayerSeparatorStyleIndex: Integer read FLayerSeparatorStyleIndex;
    property LayerSeparatorWidth: Integer read GetLayerSeparatorWidth;
    property LayoutBuilder: TcxExportCardLayoutBuilder read FLayoutBuilder;
    property LayoutDirection: TcxGridCardViewLayoutDirection read GetLayoutDirection;
    property OptionsView: TcxGridCardViewOptionsView read GetOptionsView;
    property RowCardCount: Integer read FRowCardCount write FRowCardCount;
  end;

  { TcxGridChartViewExport }
  
  TcxGridChartViewExport = class(TcxGridCustomExport)
  private
    function GetGridView: TcxGridChartView;
  protected
    procedure CreateExportCache; override;
    procedure ExportAsGraphic; virtual;
    procedure ExportAsData; virtual;
  public
    property GridView: TcxGridChartView read GetGridView;
  end;

procedure ExportGridToHTML(const AFileName: string; AGrid: TcxGrid;
  AExpand: Boolean = True; ASaveAll: Boolean = True; const AFileExt: string = 'html');
procedure ExportGridToXML(const AFileName: string; AGrid: TcxGrid;
  AExpand: Boolean = True; ASaveAll: Boolean = True; const AFileExt: string = 'xml');
procedure ExportGridToExcel(const AFileName: string; AGrid: TcxGrid;
  AExpand: Boolean = True; ASaveAll: Boolean = True; AUseNativeFormat: Boolean = True;
  const AFileExt: string = 'xls');
procedure ExportGridToText(const AFileName: string; AGrid: TcxGrid;
  AExpand: Boolean = True; ASaveAll: Boolean = True; const ASeparator: string = '';
  const ABeginString: string = ''; const AEndString: string = '';
  const AFileExt: string = 'txt');

const
  ExportImagesAsGraphic: Boolean = True; 

implementation

uses
  cxXLSExport, cxHtmlXmlTxtExport, Math;

const
  Alignment2ExportAlignment: array[TAlignment] of TcxAlignText =
    (catLeft, catRight, catCenter);

  GridLines2Borders: array[TcxGridLines] of TcxBorders =
    (cxBordersAll, [], [bRight, bLeft], [bTop, bBottom]);

  BorderWidths: array[Boolean] of Integer = (0, 1);

  ButtonTexts: array[Boolean] of string = ('+', '-');

  CardRowDefaultWidth = 20;
  cxDefaultRowHeight  = 19;

type
  TcxCustomGridRecordAccess = class(TcxCustomGridRecord);

function cxCompareGroupSummaryItems(AItem1, AItem2: TcxExportGroupSummaryItem): Integer;
begin
  if AItem1.Column =  AItem2.Column then
    Result := AItem1.Index - AItem2.Index
  else
    if AItem1.Column = nil then
      Result := -1
    else
      if AItem2.Column = nil then
        Result := 1
      else
        Result := AItem1.Column.VisibleIndex - AItem2.Column.VisibleIndex;
end;

function GetExportClassByGridView(AGridView: TcxCustomGridView): TcxGridCustomExportClass;
begin
  if AGridView is TcxGridBandedTableView then
    Result := TcxGridBandedTableViewExport
  else
    if AGridView is TcxGridTableView then
      Result := TcxGridTableViewExport
    else
      if AGridView is TcxGridCardView then
        Result := TcxGridCardViewExport
      else
        if AGridView is TcxGridChartView then
          Result := TcxGridChartViewExport
        else
          raise EcxGridExport.Create(cxGetResourceString(@scxNotExistGridView));
end;

procedure CalculateCardRowWidths(var AWidths: array of Integer; AAvailableWidth: Integer);
var
  AAutoWidths: TcxAutoWidthObject;
  AAllFixed: Boolean;
  I: Integer;
begin
  AAutoWidths := TcxAutoWidthObject.Create(Length(AWidths));
  try
    AAllFixed := True;
    for I := 0 to High(AWidths) do
      with AAutoWidths.AddItem do
      begin
        Width := AWidths[I];
        Fixed := Width <> 0;
        AAllFixed := AAllFixed and Fixed;
        if Width = 0 then Width := CardRowDefaultWidth;
      end;
    if AAllFixed or (AAutoWidths.Width > AAvailableWidth) then
      for I := 0 to AAutoWidths.Count - 1 do
        AAutoWidths[I].Fixed := False;
    AAutoWidths.AvailableWidth := AAvailableWidth;
    AAutoWidths.Calculate;
    for I := 0 to High(AWidths) do
      AWidths[I] := AAutoWidths[I].AutoWidth;
  finally
    AAutoWidths.Free;
  end;
end;

procedure ExportGridToFile(AFileName: string; AExportType: Integer;
  AGrid: TcxGrid; AExpand, ASaveAll, AUseNativeFormat: Boolean;
  const ASeparator, ABeginString, AEndString: string; const AFileExt: string);
var
  AView: TcxCustomGridView;
  AGridExport: TcxGridCustomExport;
begin
  if AGrid <> nil then
  begin
    if AFileExt <> '' then
      AFileName := ChangeFileExt(AFileName, '.' + AFileExt);
    AView := AGrid.ActiveLevel.GridView;
    AGridExport := GetExportClassByGridView(AGrid.FocusedView).Create(AFileName, AExportType, AView, AGrid, nil);
    AGridExport.SaveAll := ASaveAll;
    AGridExport.Expand := AExpand;
    AGridExport.UseNativeFormat := AUseNativeFormat;
    AGridExport.AddSeparators([ASeparator, ABeginString, AEndString]);
    AGrid.BeginUpdate;
    try
      try
        AGridExport.DoExport;
      finally
        AGridExport.Free;
      end;
    finally
      AGrid.EndUpdate;
    end;
  end;
end;

// ***

{ TcxGridCustomExport }

constructor TcxGridCustomExport.Create(AFileName: string;  AExportType: Integer;
    AGridView: TcxCustomGridView; AGrid: TcxCustomGrid; AViewInfo: TcxCustomGridViewInfo);
begin

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?