sheet.Cell 包装类

我想创build一个包装的C#类为:

sheet.Cells[int row, int colmn].Value = "some value"; 

任何build议或应该找出一些其他的方式吗?

先谢谢你。

评论:
为了澄清,工作表是一个Excel工作表。 我们有第三方组件来编写excel报告。 现在我们需要更新报表,因为第三方组件得到更新。 我需要做的是创build一个可以模仿的类

 sheet.Cell[int row, int colmn].Value = “Some Value”; 

我正在寻找这篇文章中描述的东西:
http://pietschsoft.com/post/2007/03/17/C-Give-your-object-a-Default-Indexer-Property.aspx

但是,索引器有两个参数时会变得复杂。

这是我提出的解决scheme。 这可能不是最好的解决scheme。 但是,我找不到其他的方法。

 public class SheetClass { public SheetWrapper Cells; public SheetClass(Worksheet _sheet) { Cells = new SheetWrapper(_sheet); } } public class SheetWrapper { private readonly Worksheet sheet; public PFCell this[int x, int y] { get { return new PFCell(this.sheet, x-1, y-1); } } public SheetWrapper(Worksheet sheet) { this.sheet = sheet; } } public class PFCell { Worksheet ws; int x; int y; public PFCell(Worksheet ws, int x, int y) { this.ws = ws; this.x = x; this.y = y; } public object Value { get { return ws.Cells[x,y].Value; } set { ws.Cells[x,y].Value = value; } } } 

然后在页面上,我可以做到以下几点:

 SheetClass sc = new SheetClass(sheet); sc.Cells[1, 1].Value = "test"; 

如果有更好的方法来完成这个,请让我知道。

 class SheetWrapper { private readonly Sheet sheet; public SheetWrapper(Sheet sheet) { this.sheet = sheet; } public object this[int row, int column] { get { return sheet.Cells[row, column].Value; } // add null checks, etc set { sheet.Cells[row, column].Value = value; } } } 

看起来好像您正在尝试保持与标准Excel互操作代码完全相同的语法,但使单元索引基于2而不是基于1。 我不太了解Excel对象模型,但似乎这应该工作:

 public class SheetClass { RangeWrapper cells; public RangeWrapper Cells { get; } public SheetClass(Worksheet _sheet) { cells = new RangeWrapper(_sheet.Cells); } } public class RangeWrapper { private readonly Range cells; public Cell this[int x, int y] { get { return Cells[x-1, y-1]; } } public RangeWrapper(Range cells) { this.cells = cells; } }