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

📄 qexport4xlsx.pas

📁 Advanced.Export.Component.v4.01.rar,delphi 第三方控件
💻 PAS
📖 第 1 页 / 共 4 页
字号:
                  CreateFullNode('charset', '', ['val'], [IntToStr(FooterStyle.Font.Charset)]);
                end;
            end;
            CreateFullNode('family', '', ['val'], ['2']);
            CreateFullNode('scheme', '', ['val'], ['minor']);
            EndNode('font');
          end;
          //strip styles
          if (StripStylesList.Count > 0) and (StripStyleType <> ssNone) then
            for i := 0 to StripStylesList.Count - 1 do
            begin
              BeginNode('font', '', [], []);
              Write(GetStyle(StripStylesList[i].Options.Font.Style));
              CreateFullNode('sz', '', ['val'], [IntToStr(StripStylesList[i].Options.Font.Size)]);
              CreateFullNode('color', '', ['rgb'], [ColorToHex(StripStylesList[i].Options.Font.Color)]);
              CreateFullNode('name', '', ['val'], [StripStylesList[i].Options.Font.Name]);
              CreateFullNode('family', '', ['val'], ['2']);
              CreateFullNode('charset', '', ['val'], [IntToStr(StripStylesList[i].Options.Font.Charset)]);
              CreateFullNode('scheme', '', ['val'], ['minor']);
              EndNode('font');
            end;
          EndNode('fonts');


          if (StripStylesList.Count > 0) and (StripStyleType <> ssNone) then
            BeginNode('fills', '', ['count'], [IntToStr(6 + StripStylesList.Count)])
          else
            BeginNode('fills', '', ['count'], ['6']); // 2 "default" + 4 "Header, CaptionRow, Data, Footer"
            
          BeginNode('fill', '', [], []);
          CreateFullNode('patternFill', '', ['patternType'], ['none']);
          EndNode('fill');
          BeginNode('fill', '', [], []);
          CreateFullNode('patternFill', '', ['patternType'], ['gray125']);
          EndNode('fill');
          for i := 1 to 4 do
          begin
            BeginNode('fill', '', [], []);
            BeginNode('patternFill', '', ['patternType'], ['solid']);
            case i of
              1:
                CreateFullNode('fgColor', '', ['rgb'], [ColorToHex(HeaderStyle.BackgroundColor)]);
              2:
                CreateFullNode('fgColor', '', ['rgb'], [ColorToHex(CaptionRowStyle.BackgroundColor)]);
              3:
                CreateFullNode('fgColor', '', ['rgb'], [ColorToHex(DataStyle.BackgroundColor)]);
              4:
                CreateFullNode('fgColor', '', ['rgb'], [ColorToHex(FooterStyle.BackgroundColor)]);
            end;
            EndNode('patternFill');
            EndNode('fill');
          end;
          if (StripStylesList.Count > 0) and (StripStyleType <> ssNone) then
            for i := 0 to StripStylesList.Count - 1 do
            begin
              BeginNode('fill', '', [], []);
              BeginNode('patternFill', '', ['patternType'], ['solid']);
              CreateFullNode('fgColor', '', ['rgb'], [ColorToHex(StripStylesList[i].Options.BackgroundColor)]);
              EndNode('patternFill');
              EndNode('fill');
            end;
          EndNode('fills');

          if (StripStylesList.Count > 0) and (StripStyleType <> ssNone) then
            BeginNode('borders', '', ['count'], [IntToStr(5 + StripStylesList.Count)])
          else
            BeginNode('borders', '', ['count'], ['5']);//1 default and 4 "Header, CaptionRow, Data, Footer"
            
          CreateBorder(True);  //default
          
          if HeaderStyle.UseBorder then
            CreateBorder(False, HeaderStyle.Border.Style, HeaderStyle.Border.Color)
          else
            CreateBorder(True);

          if CaptionRowStyle.UseBorder then
            CreateBorder(False, CaptionRowStyle.Border.Style, CaptionRowStyle.Border.Color)
          else
            CreateBorder(True);

          if DataStyle.UseBorder then
            CreateBorder(False, DataStyle.Border.Style, DataStyle.Border.Color)
          else
            CreateBorder(True);

          if FooterStyle.UseBorder then
            CreateBorder(False, FooterStyle.Border.Style, FooterStyle.Border.Color)
          else
            CreateBorder(True);

          if (StripStylesList.Count > 0) and (StripStyleType <> ssNone) then
            for i := 0 to StripStylesList.Count - 1 do
              if StripStylesList[i].Options.UseBorder then
                CreateBorder(False, StripStylesList[i].Options.Border.Style, StripStylesList[i].Options.Border.Color)
              else
                CreateBorder(True);
          EndNode('borders');

          BeginNode('cellStyleXfs', '', ['count'], ['1']);
          CreateFullNode('xf', '', ['numFmtId', 'fontId', 'fillId', 'borderId'], ['0', '0', '0', '0']);
          EndNode('cellStyleXfs');

          if (StripStylesList.Count > 0) and (StripStyleType <> ssNone) then
            BeginNode('cellXfs', '', ['count'], [IntToStr(5 + StripStylesList.Count)])
          else
            BeginNode('cellXfs', '', ['count'], ['5']);

          CreateFullNode('xf', '', ['numFmtId', 'fontId', 'fillId', 'borderId', 'xfId'], ['0', '0', '0', '0', '0']);
          
          if (HeaderStyle.Alignment <> caLeft) or (HeaderStyle.VerticalAligment <> cvaBottom) or HeaderStyle.WrapText then
          begin
            BeginNode('xf', '', ['numFmtId', 'fontId', 'fillId', 'borderId', 'xfId', 'applyBorder', 'applyAlignment'],
              ['0', '1', '2', '1', '0', '1', '1']);
            Write(GetAlignmentNode(HeaderStyle.Alignment, HeaderStyle.VerticalAligment, HeaderStyle.WrapText));
            EndNode('xf');
          end else
            CreateFullNode('xf', '', ['numFmtId', 'fontId', 'fillId', 'borderId', 'xfId', 'applyBorder'],
              ['0', '1', '2', '1', '0', '1']);

          if (CaptionRowStyle.Alignment <> caLeft) or (CaptionRowStyle.VerticalAligment <> cvaBottom) or CaptionRowStyle.WrapText then
          begin
            BeginNode('xf', '', ['numFmtId', 'fontId', 'fillId', 'borderId', 'xfId', 'applyBorder', 'applyAlignment'],
              ['0', '2', '3', '2', '0', '1', '1']);
            Write(GetAlignmentNode(CaptionRowStyle.Alignment, CaptionRowStyle.VerticalAligment, CaptionRowStyle.WrapText));
            EndNode('xf');
          end else
            CreateFullNode('xf', '', ['numFmtId', 'fontId', 'fillId', 'borderId', 'xfId', 'applyBorder'],
              ['0', '2', '3', '2', '0', '1']);

          if (DataStyle.Alignment <> caLeft) or (DataStyle.VerticalAligment <> cvaBottom) or DataStyle.WrapText then
          begin
            BeginNode('xf', '', ['numFmtId', 'fontId', 'fillId', 'borderId', 'xfId', 'applyBorder', 'applyAlignment'],
              ['0', '3', '4', '3', '0', '1', '1']);
            Write(GetAlignmentNode(DataStyle.Alignment, DataStyle.VerticalAligment, DataStyle.WrapText));
            EndNode('xf');
          end else
            CreateFullNode('xf', '', ['numFmtId', 'fontId', 'fillId', 'borderId', 'xfId', 'applyBorder'],
              ['0', '3', '4', '3', '0', '1']);

          if (FooterStyle.Alignment <> caLeft) or (FooterStyle.VerticalAligment <> cvaBottom) or FooterStyle.WrapText then
          begin
            BeginNode('xf', '', ['numFmtId', 'fontId', 'fillId', 'borderId', 'xfId', 'applyBorder', 'applyAlignment'],
              ['0', '4', '5', '4', '0', '1', '1']);
            Write(GetAlignmentNode(FooterStyle.Alignment, FooterStyle.VerticalAligment, FooterStyle.WrapText));
            EndNode('xf');
          end else
            CreateFullNode('xf', '', ['numFmtId', 'fontId', 'fillId', 'borderId', 'xfId', 'applyBorder'],
              ['0', '4', '5', '4', '0', '1']);

          if (StripStylesList.Count > 0) and (StripStyleType <> ssNone) then
          begin
            n := 4;
            for i := 0 to StripStylesList.Count - 1 do
            begin
              Inc(n);
              if (StripStylesList[i].Options.Alignment <> caLeft) or
                (StripStylesList[i].Options.VerticalAligment <> cvaBottom) or StripStylesList[i].Options.WrapText then
                begin
                  BeginNode('xf', '', ['numFmtId', 'fontId', 'fillId', 'borderId', 'xfId', 'applyBorder', 'applyAlignment'],
                    ['0', IntToStr(n), IntToStr(n + 1), IntToStr(n), '0', '1', '1']);
                  Write(GetAlignmentNode(StripStylesList[i].Options.Alignment,
                    StripStylesList[i].Options.VerticalAligment, StripStylesList[i].Options.WrapText));
                  EndNode('xf');
                end else
                  CreateFullNode('xf', '', ['numFmtId', 'fontId', 'fillId', 'borderId', 'xfId', 'applyBorder'],
                    ['0', IntToStr(n), IntToStr(n + 1), IntToStr(n), '0', '1']);
            end;
          end;
            
          EndNode('cellXfs');
          
          BeginNode('cellStyles', '', ['count'], ['1']);
          CreateFullNode('cellStyle', '', ['name', 'xfId', 'builtinId'], ['Normal', '0', '0']);
          EndNode('cellStyles');
        
          CreateFullNode('dxfs', '', ['count'], ['0']);
          CreateFullNode('tableStyles', '', ['count', 'defaultTableStyle', 'defaultPivotStyle'], ['0', 'TableStyleMedium9', 'PivotStyleLight16']);
          CreateFullNode('colors', '', [], []);

          EndNode('styleSheet');
        end;
      finally
        Free;
      end;
  finally
    Stream.Free;
  end;
end;

{ TQExport4Xlsx }

procedure TQExport4Xlsx.SetXlsxOptions(const Value: TQExport4XlsxOptions);
begin
  FXlsxOptions.Assign(Value);
end;

procedure TQExport4Xlsx.SetSheetName(const Value: WideString);
begin
  if Value <> '' then
    FSheetName := Value;
end;

procedure TQExport4Xlsx.WriteHeader;
var
  i: Integer;
begin
  for i := 0 to Header.Count - 1 do
  begin
    Inc(FCounter);
    FXlsxFile.AddCell(0, FCounter, 0, Header[i], ectString, 1);
  end;
end;

procedure TQExport4Xlsx.WriteFooter;
var
  i: Integer;
begin
  for i := 0 to Footer.Count - 1 do
  begin
    Inc(FCounter);
    FXlsxFile.AddCell(0, FCounter, 0, Footer[i], ectString, 4);
  end;
end;

procedure TQExport4Xlsx.BeginExport;
begin
  inherited;
  FCounter := 0;
  FDataRowCounter := 0;
  FXlsxFile := TXlsxFileMaker.Create(FileName, XlsxOptions);
  FXlsxFile.SheetName := SheetName;
  FXlsxFile.CreateDirsStructure;
  if FXlsxFile.IsExistDirsStructure then
  begin
    FXlsxFile.FillCommonData;
    FXlsxFile.PrepareExport;
    WriteHeader;
  end;
end;

procedure TQExport4Xlsx.BeforeExport;
begin
  inherited;

end;

procedure TQExport4Xlsx.AfterExport;
begin

  inherited;
end;

procedure TQExport4Xlsx.EndExport;
begin
  if FXlsxFile.IsExistDirsStructure then
  begin
    WriteFooter;
    FXlsxFile.FinishExport;
    FXlsxFile.CompressFile;
  end;
  FXlsxFile.Free;
  inherited;
end;

procedure TQExport4Xlsx.WriteCaptionRow;
var
  i: Integer;
begin
  if FXlsxFile.IsExistDirsStructure then
  begin
    Inc(FCounter);
    for i := 0 to Columns.Count - 1 do
      FXlsxFile.AddCell(i, FCounter, Columns.Count - 1, GetColCaption(i), ectString, 2);
  end;
end;

procedure TQExport4Xlsx.WriteDataRow;
var
  i, n: Integer;
begin
  if FXlsxFile.IsExistDirsStructure then
  begin
    Inc(FCounter);
    for i := 0 to ExportRow.Count - 1 do
    begin
      Formats.ApplyParams;
      if (XlsxOptions.StripStylesList.Count > 0) and (XlsxOptions.StripStyleType <> ssNone) then
      begin
        if XlsxOptions.StripStyleType = ssColumn then
          n := i mod XlsxOptions.StripStylesList.Count
        else
          n := FDataRowCounter mod XlsxOptions.StripStylesList.Count;

        FXlsxFile.AddCell(i, FCounter, ExportRow.Count - 1,
          GetExportedValue(ExportRow[i]), ExportRow[i].Row.Columns[i].ColType, n + 5);
      end
      else
        FXlsxFile.AddCell(i, FCounter, ExportRow.Count - 1,
          GetExportedValue(ExportRow[i]), ExportRow[i].Row.Columns[i].ColType, 3);
      Formats.RestoreSeparators;
    end;
    Inc(FDataRowCounter);
  end;
end;

constructor TQExport4Xlsx.Create(AOwner: TComponent);
begin
  inherited;
  FSheetName := 'sheet1';
  FXlsxOptions := TQExport4XlsxOptions.Create(Self);
end;

destructor TQExport4Xlsx.Destroy;
begin
  FXlsxOptions.Free;
  inherited;
end;

procedure TQExport4Xlsx.Execute;
begin
  DoExport;
  ShowResult;
end;

end.

⌨️ 快捷键说明

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