如何在ClosedXML中计算Excel文件的列数?

时间:2017-06-30 07:08:58

标签: c# c#-4.0 closedxml

我尝试使用代码执行此操作:

unit Unit60;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;

type
  TForm60 = class(TForm)
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
  public
    ImageDst: TImage;
    ImageMask: TImage;
    ImageSprite: TImage;
    Button1: TButton;
  end;

var
  Form60: TForm60;

implementation

{$R *.dfm}

procedure TForm60.FormCreate(Sender: TObject);
begin
  ImageDst := TImage.Create(Form60);
  ImageMask := TImage.Create(Form60);
  ImageSprite := TImage.Create(Form60);
  Button1 := TButton.Create(Form60);

  ImageDst.Name := 'ImageDst';
  ImageDst.Parent := Self;
  ImageDst.Left := 24;
  ImageDst.Top := 24;
  ImageDst.Width := 249;
  ImageDst.Height := 281;

  ImageMask.Name := 'ImageMask';
  ImageMask.Parent := Self;
  ImageMask.Left := 279;
  ImageMask.Top := 24;
  ImageMask.Width := 128;
  ImageMask.Height := 128;

  ImageSprite.Name := 'ImageSprite';
  ImageSprite.Parent := Self;
  ImageSprite.Left := 279;
  ImageSprite.Top := 158;
  ImageSprite.Width := 128;
  ImageSprite.Height := 128;

  Button1.Name := 'Button1';
  Button1.Parent := Self;
  Button1.Left := 288;
  Button1.Top := 303;
  Button1.Width := 75;
  Button1.Height := 25;
  Button1.Caption := 'Button1';
  Button1.OnClick := Button1Click;
end;

procedure TForm60.Button1Click(Sender: TObject);
var
  Mask,Sprite,Dst: TBitmap;
begin

  { create bitmap within TImage }
  Dst := ImageDst.Picture.Bitmap;
  Dst.SetSize(ImageDst.Width, ImageDst.Height);

  { prepare some mask & show it at bottom }
  Mask := ImageMask.Picture.Bitmap;
  Mask.SetSize(128, 128);
  Mask.Canvas.Brush.Color := clRed;
  Mask.Canvas.FillRect(TRect.Create(32, 32, 96, 96));

  { prepare some sprite & show it at bottom  }
  Sprite := ImageSprite.Picture.Bitmap;
  Sprite.SetSize(128, 128);
  Sprite.Canvas.Ellipse(TRect.Create(16,16,112,112));

  { draw to image }
  BitBlt(Dst.Canvas.Handle, 0, 0, Mask.Width, Mask.Height, Mask.Canvas.Handle, 0, 0, SrcAnd);
  BitBlt(Dst.Canvas.Handle, 0, 0, Sprite.Width, Sprite.Height, Sprite.Canvas.Handle, 0, 0, SrcInvert);
end;

end.

worksheet.Columns.count();

没有方法count()

所有代码均为:

Columns

2 个答案:

答案 0 :(得分:3)

查看来自https://github.com/ClosedXML/ClosedXML的代码 Count应该使用LINQ,因为IXLColumns实现IEnumerable<IXLColumn>     IXLColumns:IEnumerable

如果您在Count上找不到方法,则表示您缺少

using System.Linq;

答案 1 :(得分:1)

我试图做同样的事情。以下对我有用:

worksheet.Columns().Count()