⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cxexportvglink.pas

📁 delphi的的三方控件
💻 PAS
📖 第 1 页 / 共 3 页
字号:
  HeaderColumnsMap.AddLevel(-1, 0, MaxHeaderWidth);
  AddMapRightSide(HeaderColumnsMap, MaxHeaderWidth, MinHeaderWidth, True);
  HeaderColumnsMap.Build;
  CalculateRowsIndents;
  CalculateRowsCaptions;
end;

procedure TcxVerticalGridMapsInfo.CalculateRowsCaptions;
var
  I, J: Integer;
  AViewParams: TcxViewParams;
  ARow: TcxCustomRow;
  ACaptions: TcxRowCellsInfo;

  procedure AddCaption(AProperties: TcxCaptionRowProperties; ACellIndex: Integer);
  var
    AStart, AEnd: Integer;
    AStyle: TcxCacheCellStyle;
  begin
    HeaderColumnsMap.GetColumnInfoFromRowCell(ARow, ACellIndex, AStart, AEnd);
    cxViewParamsToCacheStyle(AViewParams, AStyle);
    AStyle.AlignText := AlignToCxAlign[AProperties.HeaderAlignmentHorz];
    cxCheckBorders(AStyle, [bTop, bBottom, bRight], GridLineColor, GridLines);
    ACaptions.AddCaption(AStart, AEnd - AStart, Provider.RegisterStyle(AStyle), AProperties.Caption);
  end;

begin
  for I := 0 to RowCount - 1 do
  begin
    ARow := Rows[I];
    ACaptions := RowsCaptions.AddCaption;
    AViewParams := VerticalGrid.Styles.GetHeaderParams(ARow);
    if ARow is TcxCustomEditorRow then
      AddCaption(TcxCustomEditorRowAccess(ARow).Properties, 0)
    else
      if ARow is TcxCustomMultiEditorRow then
        with GetMultiEditorRowProperties(ARow) do
          for J := 0 to Editors.Count - 1 do
            AddCaption(Editors[J], J);
  end;
end;

procedure TcxVerticalGridMapsInfo.CalculateRowsIndents;
var
  I: Integer;
  AIndents: TcxRowIndentsInfo;
begin
  for I := 0 to RowCount - 1 do
  begin
    AIndents := TcxRowIndentsInfo.Create(Rows[I]);
    AIndents.Calculate(HeaderColumnsMap, Provider);
    RowsIndents.Add(AIndents);
  end;
end;

procedure TcxVerticalGridMapsInfo.CalculateSize;
begin
  CalculateHeader;
  CalculateValuesMap;
  FRecordCount := VerticalGrid.RecordCount;
  FIsEmpty := FRecordCount = 0;
  if FIsEmpty then Inc(FRecordCount);
  FRecordsPerBand := Max(Owner.RecordsPerBand, 1);
  FBandCount := FRecordCount div FRecordsPerBand;
  FLastBandRecords := FRecordCount - FBandCount * FRecordsPerBand;
  if FLastBandRecords > 0 then
    Inc(FBandCount)
  else
    FLastBandRecords := FRecordsPerBand;
  FSize := cxSize(FirstValuesColumn + (ValueColumnsMap.MaxColumnIndex + 1) *
    cxSetValue(FBandCount = 1, FLastBandRecords, RecordsPerBand),
    (RowCount + 1) * FBandCount - 1);
end;

procedure TcxVerticalGridMapsInfo.CalculateValuesMap;
begin
  FFirstValuesColumn := HeaderColumnsMap.MaxColumnIndex + 1;
  FMinValueWidth := VerticalGrid.OptionsView.ValueMinWidth;
  FMaxValueWidth := Max(ValueColumnsMap.NeedWidth,
    VerticalGrid.OptionsView.ValueWidth);
  ValueColumnsMap.AddLevel(-1, 0, MaxValueWidth);
  AddMapRightSide(ValueColumnsMap, MaxValueWidth, MinValueWidth, False);
  ValueColumnsMap.Build;
end;

procedure TcxVerticalGridMapsInfo.DoCalculate;
begin
  CalculateSize;
  AlignCategories;
end;

procedure TcxVerticalGridMapsInfo.DoWrite;
begin
  Provider.SetRange(Size.cx, Size.cy, False);
  WriteColumnWidths;
  WriteValues;
end;

function TcxVerticalGridMapsInfo.GetDisplayText(ARecordIndex: Integer;
  ARow: TcxCustomEditorRowProperties): string;
begin
  with ARow.DisplayEditProperties[ARecordIndex] do
  begin
    if GetEditValueSource(False) = evsValue then
      Result := GetDisplayText(ARow.Values[ARecordIndex], True)
    else
      Result := ARow.DisplayTexts[ARecordIndex];
  end;
end;

function TcxVerticalGridMapsInfo.GetDisplayValue(ARecordIndex: Integer;
  ARow: TcxCustomEditorRowProperties): Variant;
var
  AProperties: TcxCustomEditProperties;
begin
  AProperties := ARow.DisplayEditProperties[ARecordIndex];
  if IsNativeFormatProperties(AProperties) then
    Result := ARow.Values[ARecordIndex]
  else
    Result := AProperties.GetDisplayText(ARow.Values[ARecordIndex], True);
end;

function TcxVerticalGridMapsInfo.GetMultiEditorRowProperties(
  ARow: TcxCustomRow): TcxMultiEditorRowProperties;
begin
  Result := TcxMultiEditorRowProperties(
    TcxCustomMultiEditorRowAccess(ARow).FProperties);
end;

function TcxVerticalGridMapsInfo.IsIncludeRow(ARow: TcxCustomRow): Boolean;
begin
  Result := VerticalGrid.IsRowVisible(ARow);
end;

function TcxVerticalGridMapsInfo.IsNativeFormatProperties(
  AProperties: TcxCustomEditProperties): Boolean;
begin
  Result := (AProperties is TcxDateEditProperties) or (AProperties is TcxCurrencyEditProperties) or
    (AProperties is TcxSpinEditProperties) or (AProperties is TcxCalcEditProperties) or
    (AProperties is TcxTimeEditProperties);
end;

procedure TcxVerticalGridMapsInfo.SetCellStyle(ACol, ARow, W, AStyleIndex: Integer);
begin
  with Provider do
    SetCellStyleEx(ACol, ARow, 1, W, AStyleIndex);
end;

procedure TcxVerticalGridMapsInfo.SetCellStyle(ACol, ARow, W: Integer;
  const AStyle: TcxCacheCellStyle);
begin
  with Provider do
    SetCellStyleEx(ACol, ARow, 1, W, RegisterStyle(AStyle));
end;

procedure TcxVerticalGridMapsInfo.WriteColumnWidths;
var
  I, J, AWidth, ACount: Integer;
begin
  for I := 0 to HeaderColumnsMap.MaxColumnIndex do
  begin
    AWidth := HeaderColumnsMap.GetColumnWidth(I);
    Provider.SetColumnWidth(I, AWidth);
  end;
  ACount := cxSetValue(RecordCount > FRecordsPerBand, FRecordsPerBand,
    FLastBandRecords);
  for J := 0 to ACount - 1 do
    for I := 0 to ValueColumnsMap.MaxColumnIndex do
    begin
      AWidth := ValueColumnsMap.GetColumnWidth(I);
      Provider.SetColumnWidth(FirstValuesColumn +
        J * (ValueColumnsMap.MaxColumnIndex + 1) + I, AWidth);
    end;
end;

procedure TcxVerticalGridMapsInfo.WriteHeaders(ARowIndex: Integer);
var
  I: Integer;
begin
  for I := 0 to RowCount - 1 do
    WriteRowHeader(ARowIndex + I, RowsIndents[I], RowsCaptions[I]);
end;

procedure TcxVerticalGridMapsInfo.WriteRecord(
  ACol, ARowIndex, ARecordIndex: Integer);
var
  I, J: Integer;
  ARow: TcxCustomRow;

  procedure WriteCell(ARowIndex: Integer;
    AProperties: TcxCustomEditorRowProperties; ACellIndex: Integer);
  var
    AStart, AEnd: Integer;
    AViewParams: TcxViewParams;
    AStyle: TcxCacheCellStyle;
  begin
    ValueColumnsMap.GetColumnInfoFromRowCell(ARow, ACellIndex, AStart, AEnd);
    AViewParams := VerticalGrid.Styles.GetContentParams(AProperties, False, ARecordIndex);
    cxViewParamsToCacheStyle(AViewParams, AStyle);
    AStyle.AlignText := AlignToCxAlign[TcxPropertiesAccess(AProperties.DisplayEditProperties[ARecordIndex]).Alignment.Horz];
    cxCheckBorders(AStyle, cxBordersAll, GridLineColor, GridLines);
    SetCellStyle(ACol + AStart, ARowIndex, AEnd - AStart, AStyle);
    WriteValue(ACol + AStart, ARowIndex, ARecordIndex, AProperties);
  end;

begin
  for I := 0 to RowCount - 1 do
  begin
    ARow := Rows[I];
    if ARow is TcxCustomEditorRow then
      WriteCell(ARowIndex + I, TcxCustomEditorRowAccess(ARow).Properties, 0)
    else
      if ARow is TcxCustomMultiEditorRow then
      with GetMultiEditorRowProperties(ARow) do
        for J := 0 to Editors.Count - 1 do
          WriteCell(ARowIndex + I, Editors[J], J)
  end;
end;

procedure TcxVerticalGridMapsInfo.WriteRowCaptions(ARowIndex: Integer;
  ACaptions: TcxRowCellsInfo);
var
  I: Integer;
begin
  with Provider do
    for I := 0 to ACaptions.Count - 1 do
      with ACaptions[I] do
      begin
        SetCellStyleEx(Column, ARowIndex, 1, Width, StyleIndex);
        SetCellDataString(Column, ARowIndex, Caption);
      end;
end;

procedure TcxVerticalGridMapsInfo.WriteRowHeader(ARowIndex: Integer;
  AIndents: TcxRowIndentsInfo; ACaptions: TcxRowCellsInfo);
const
  PlusMinus: array[Boolean] of Char = ('+', '-');
var
  I: Integer;
begin
  for I := 0 to AIndents.Count - 1 do
    with AIndents.Indents[I] do
    begin
      if (I = 0) and AIndents.Row.HasVisibleChildren then
        Provider.SetCellDataString(Column, ARowIndex, PlusMinus[AIndents.Row.Expanded]);
      SetCellStyle(Column, ARowIndex, Width, StyleIndex);
    end;
  WriteRowCaptions(ARowIndex, ACaptions);
end;

procedure TcxVerticalGridMapsInfo.WriteValue(ACol, ARow, ARecordIndex: Integer;
  AProperties: TcxCustomEditorRowProperties);
begin
  if IsEmpty then
    Provider.SetCellDataString(ACol, ARow, '')
  else
  begin
    if Owner.IsNativeFormat then
      Provider.SetCellValue(ACol, ARow, GetDisplayValue(ARecordIndex, AProperties))
    else
      Provider.SetCellDataString(ACol, ARow, GetDisplayText(ARecordIndex, AProperties));
  end;
end;

procedure TcxVerticalGridMapsInfo.WriteValues;
var
  I, J, ARow, ACol, ARecord: Integer;
begin
  ARow := 0;
  ARecord := 0;
  for I := 0 to FBandCount - 1 do
  begin
    ACol := HeaderColumnsMap.MaxColumnIndex + 1;
    WriteHeaders(ARow);
    for J := 0 to FRecordsPerBand - 1 do
    begin
      WriteRecord(ACol + J * (ValueColumnsMap.MaxColumnIndex + 1), ARow, ARecord);
      Inc(ARecord);
      if ARecord = RecordCount then Exit;
    end;
    Inc(ARow, RowCount + 1);
    Provider.SetRowHeight(ARow - 1, 8);
  end;
end;

function TcxVerticalGridMapsInfo.GetLevelIndent(Index: Integer): Integer;
begin
  Result := FLevelIndents[Index];
end;

function TcxVerticalGridMapsInfo.GetProvider: IcxExportProvider;
begin
  Result := FOwner.Provider;
end;

function TcxVerticalGridMapsInfo.GetRow(Index: Integer): TcxCustomRow;
begin
  Result := TcxCustomRow(FRows.List^[Index]);
end;

{ TcxVerticalGridExportHelper }

constructor TcxVerticalGridExportHelper.Create(
  AVerticalGrid: TcxCustomVerticalGrid; AExportType: Integer;
  const AFileName: string);

  function DefaultStyle: TcxCacheCellStyle;
  var
    I: Integer;
  begin
    cxViewParamsToCacheStyle(VerticalGrid.Styles.GetBackgroundParams, Result);
    for I := 0 to 3 do
    begin
      Result.Borders[I].IsDefault := True;
      Result.Borders[I].Width := 0;
    end;
  end;

begin
  FVerticalGrid := TcxVerticalGridAccess(AVerticalGrid);
  TcxExport.Provider(AExportType, AFileName).GetInterface(IcxExportProvider, FProvider);
  FProvider.SetDefaultStyle(DefaultStyle);
  MapsInfo := GetMapsInfoClass.Create(Self);
end;

destructor TcxVerticalGridExportHelper.Destroy;
begin
  try
    MapsInfo.Free;
    FProvider := nil;
  finally
    inherited Destroy;
  end;
end;

procedure TcxVerticalGridExportHelper.DoCalculateTableMap;
begin
  MapsInfo.DoCalculate;
end;

procedure TcxVerticalGridExportHelper.DoWriteCells;
begin
  MapsInfo.DoWrite;
  Provider.Commit;
end;

function TcxVerticalGridExportHelper.GetMapsInfoClass: TcxVerticalGridMapsInfoClass;
begin
  Result := TcxVerticalGridMapsInfo;
end;

// external procedures definition

{$IFNDEF DELPHI6}
type
  IInterface = IUnknown;
{$ENDIF}

function cxSupports(const Instance: IInterface; const IID: TGUID; out Intf): Boolean;
begin
  Result := (Instance <> nil) and (Instance.QueryInterface(IID, Intf) = 0);
end;

procedure cxExportVGToFile(AFileName: string;
  AVerticalGrid: TcxCustomVerticalGrid; AExportType: Integer;
  AExpand, AUseNativeFormat: Boolean; const ASeparators: array of string;
  ARecordPerBand: Integer; const AFileExt: string);
var
  I: Integer;
  AIntf: IcxExportWithSeparators;
begin
  if AFileExt <> '' then
    AFileName := ChangeFileExt(AFileName, '.' + AFileExt);
  if not AVerticalGrid.Visible then
    cxVerticalGridError(cxSvgExportNotVisibleControl);
  with TcxVerticalGridExportHelper.Create(AVerticalGrid, AExportType, AFileName) do
  try
    FExpand := AExpand;
    if AExpand then AVerticalGrid.FullExpand;
    AVerticalGrid.FocusedRow := nil;
    FRecordsPerBand := Max(ARecordPerBand, 1);
    FIsNativeFormat :=  AUseNativeFormat;
    if cxSupports(Provider, IcxExportWithSeparators, AIntf) and (Length(ASeparators) > 0) then
    begin
      for I := Low(ASeparators) to High(ASeparators) do
        AIntf.AddSeparator(ASeparators[I]);
    end;
    DoCalculateTableMap;
    DoWriteCells;
  finally
    Free;
  end;
end;

procedure cxExportVGToHTML(const AFileName: string;
  AVerticalGrid: TcxCustomVerticalGrid; AExpand: Boolean = True;
  ARecordPerBand: Integer = 8; const AFileExt: string = 'html');
begin
  cxExportVGToFile(AFileName, AVerticalGrid, cxExportToHtml, AExpand, False, [],
  ARecordPerBand, AFileExt);
end;

procedure cxExportVGToXML(const AFileName: string;
  AVerticalGrid: TcxCustomVerticalGrid; AExpand: Boolean = True;
  ARecordPerBand: Integer = 8; const AFileExt: string = 'xml');
begin
  cxExportVGToFile(AFileName, AVerticalGrid, cxExportToXML, AExpand, False, [],
  ARecordPerBand, AFileExt);
end;

procedure cxExportVGToExcel(const AFileName: string;
  AVerticalGrid: TcxCustomVerticalGrid; AExpand: Boolean = True;
  AUseNativeFormat: Boolean = True;
  ARecordPerBand: Integer = 8; const AFileExt: string = 'xls');
begin
  cxExportVGToFile(AFileName, AVerticalGrid, cxExportToExcel, AExpand,
    AUseNativeFormat, [], ARecordPerBand, AFileExt);
end;

procedure cxExportVGToText(const AFileName: string;
  AVerticalGrid: TcxCustomVerticalGrid; AExpand: Boolean = True;
  const ASeparator: string = ''; const ABeginString: string = '';
  const AEndString: string = ''; ARecordPerBand: Integer = 8;
  const AFileExt: string = 'txt');
begin
  cxExportVGToFile(AFileName, AVerticalGrid, cxExportToText, AExpand, False,
    [ASeparator, ABeginString, AEndString], ARecordPerBand, AFileExt);
end;

initialization
  Move(cxIndentFontName[1], cxIndentStyle.FontName[0], Length(cxIndentFontName));
  cxExportInit(cxGetResourceString, @ColorToRGB, {$IFDEF VCL} True {$ELSE} False {$ENDIF});
end.


⌨️ 快捷键说明

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