[英]OpenXML how to get cell in range
Please help me to get cell in range (ex from A:1 to E:11 are all cells in rectangular). 请帮助我让细胞进入范围(从A:1到E:11是所有细胞都是矩形)。 For now, my ideal is
现在,我的理想是
Worksheet worksheet = GetWorksheet(document, sheetName);
SheetData sheetData = worksheet.GetFirstChild<SheetData>();
IEnumerable<Cell> cells = sheetData.Descendants<Cell>().Where(c =>
c.CellReference >= A:1 &&
c.CellReference <= E:11 &&
);
int t = cells.Count();
But this code does not work. 但是这段代码不起作用。 Thanks
谢谢
It won't be that easy to compare cell's CellReference with a string. 将单元格的CellReference与字符串进行比较并不容易。 And yes, what you are currently doing is wrong.
是的,你目前所做的是错的。 You simply cannot compare
strings
for Higher or Lower in such a way. 您无法以这种方式比较高或低的
strings
。
You have two options. 你有两个选择。
Option 1 : 选项1 :
You can take cell reference and break it down. 您可以获取单元格引用并将其分解。 That means separate characters and numbers and then give them values individually and compare
这意味着单独的字符和数字,然后单独给它们值和比较
A1 - > A and 1 -> Give A =1 so you have 1 and 1
A1
- > A and 1 -> Give A =1 so you have 1 and 1
E11 -> E and 11 -> Give E = 5 so you have 5 and 11
E11
-> E and 11 -> Give E = 5 so you have 5 and 11
So you will need to breakdown the CellReference
and check the validity for your requirement. 因此,您需要细分
CellReference
并检查您的要求的有效性。
Option 2 : 选项2:
If you notice above it's simply we take a 2D matrix index (ex : 1,1 and 5,11 which are COLUMN,ROW format)
. 如果您在上面注意到它只是我们采用2D矩阵索引
(ex : 1,1 and 5,11 which are COLUMN,ROW format)
。 You can simply use this feature in comparison. 您可以简单地使用此功能进行比较。 But catch is you cannot use
LINQ
for this, you need to iterate through rows and columns. 但是catch是你不能使用
LINQ
,你需要遍历行和列。 I tried to give following example code, try it 我试着给出以下示例代码,试一试
using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open("PATH", true))
{
//Get workbookpart
WorkbookPart workbookPart = myDoc.WorkbookPart;
// Extract the workbook part
var stringtable = workbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
//then access to the worksheet part
IEnumerable<WorksheetPart> worksheetPart = workbookPart.WorksheetParts;
foreach (WorksheetPart WSP in worksheetPart)
{
//find sheet data
IEnumerable<SheetData> sheetData = WSP.Worksheet.Elements<SheetData>();
int RowCount = 0;
int CellCount = 0;
// This is A1
int RowMin = 1;
int ColMin = 1;
//This is E11
int RowMax = 11;
int ColMax = 5;
foreach (SheetData SD in sheetData)
{
foreach (Row row in SD.Elements<Row>())
{
RowCount++; // We are in a new row
// For each cell we need to identify type
foreach (Cell cell in row.Elements<Cell>())
{
// We are in a new Cell
CellCount++;
if ((RowCount >= RowMin && CellCount >= ColMin) && (RowCount <= RowMax && CellCount <= ColMax))
{
if (cell.DataType == null && cell.CellValue != null)
{
// Check for pure numbers
Console.WriteLine(cell.CellValue.Text);
}
else if (cell.DataType.Value == CellValues.Boolean)
{
// Booleans
Console.WriteLine(cell.CellValue.Text);
}
else if (cell.CellValue != null)
{
// A shared string
if (stringtable != null)
{
// Cell value holds the shared string location
Console.WriteLine(stringtable.SharedStringTable.ElementAt(int.Parse(cell.CellValue.Text)).InnerText);
}
}
else
{
Console.WriteLine("A broken book");
}
}
}
// Reset Cell count
CellCount = 0;
}
}
}
}
This actually work. 这实际上有效。 I tested.
我测试过了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.