worksheet获取cell,为什么选择Cell.CellReference?如何获取单元格位置?

According to the OpenXml spec CellReference is optional. But is there an alternative way to get a cell's location?

For example, I can get a row's cells by something like:

foreach (Row r in sheetData.Elements())

{

foreach (Cell c in r.Elements())

{

Console.WriteLine(c.CellReference);

}

}

But without CellReference, how do I know the cells' locations?

I have done some research and all solutions seem to rely on comparing a cell's CellReference against a given string.

解决方案

The CellReference is optional because if it's missing, Excel uses the "first come first served" principle (that's not the official term by the way).

What happens when all your Cell classes have missing CellReference properties is that Excel will parse based on the order in which it encounters Row and Cell classes.

The 1st Row class (you will note that the RowIndex is also an optional property) that's in the SheetData class will be assumed to have row index 1. And the 1st Cell class in that row is assumed to have the CellReference "A1" (assuming the Cell has no CellReference assigned). And the 2nd Cell class in that row is assumed to have the CellReference "B1". And so on and so forth.

The next Row class (if the RowIndex property is also not assigned) will be assumed to be the 2nd row.

Basically, if all the RowIndex and CellReference properties are missing, all your Cell classes are bunched together at the top-left corner of your worksheet.

Hypothetically speaking, if you have 3 unassigned RowIndex-ed Row classes and the next Row class has a RowIndex of 8, that's a valid worksheet. You'll just have the first 3 rows of data in the first 3 rows in the worksheet (as expected), and the "4th" Row class (with the RowIndex of 8) as the 8th row in the worksheet.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值