为什么选择Cell.CellReference?如何获取单元格位置? [英] Why is Cell.CellReference optional? How do I get cell location otherwise?

查看:1434
本文介绍了为什么选择Cell.CellReference?如何获取单元格位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据OpenXml规范,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<Row>())
{
    foreach (Cell c in r.Elements<Cell>())
    {
        Console.WriteLine(c.CellReference);
    }
} 

但是没有CellReference,位置?

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

我已经做了一些研究,所有的解决方案似乎都依赖于将单元格的CellReference与给定的字符串进行比较。

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

推荐答案

CellReference是可选的,因为如果缺少,Excel使用先到先得原则(这不是官方术语)。

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).

当所有Cell类都缺少CellReference属性时,Excel会根据遇到Row和Cell类的顺序进行解析。

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.

第一行类(你会注意到RowIndex也是一个可选属性),在SheetData类中将被假设为具有行索引1.并且该行中的第一个Cell类被假定为具有CellReferenceA1 (假设Cell没有分配CellReference)。并且该行中的第二个Cell类被假定为具有CellReferenceB1。等等。

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.

下一个Row类(如果RowIndex属性也未分配)将被假定为第二行。

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

基本上,如果所有的RowIndex和CellReference属性都丢失,您的所有单元格类都在工作表的左上角聚集在一起。

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.

假设,如果你有3个未分配的RowIndex-ed Row类,而下一个Row类的RowIndex为8,那么这是一个有效的工作表。您只需要在工作表中的前3行中的前3行数据(如预期),第4行类(RowIndex为8)作为工作表中的第8行。

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.

这篇关于为什么选择Cell.CellReference?如何获取单元格位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆