C# 从单元格值openxml获取列号
我有一个Excel,其中我想获得下图的列号:C# 从单元格值openxml获取列号,c#,excel,openxml,openxml-sdk,C#,Excel,Openxml,Openxml Sdk,我有一个Excel,其中我想获得下图的列号: 在上图中,我知道记录将出现在第一行,但我不确定列号。在上述示例中,“D1”上显示列值:“数量”。我知道行号如何使用OPEN XML查找列号(“在上述情况下为D”),因为列名称数量可能出现在excel中的任何位置,我只需要查找数量的对应值。不幸的是,没有一个方法可以调用来查找正确的单元格。相反,您需要遍历单元格以找到匹配的文本。让事情稍微复杂一点,单元格中的值并不总是实际文本。相反,字符串可以存储在SharedStringTablePart中,单元格
在上图中,我知道记录将出现在第一行,但我不确定列号。在上述示例中,“D1”上显示列值:“数量”。我知道行号如何使用OPEN XML查找列号(“在上述情况下为D”),因为列名称数量可能出现在excel中的任何位置,我只需要查找数量的对应值。不幸的是,没有一个方法可以调用来查找正确的单元格。相反,您需要遍历单元格以找到匹配的文本。让事情稍微复杂一点,单元格中的值并不总是实际文本。相反,字符串可以存储在
SharedStringTablePart
中,单元格的值是该表内容的索引
类似于以下内容的内容应满足您的要求:
private static string GetCellReference(string filename, string sheetName, int rowIndex, string textToFind)
{
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filename, false))
{
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
//get the correct sheet
Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sheetName).First();
if (sheet != null)
{
WorksheetPart worksheetPart = workbookPart.GetPartById(sheet.Id) as WorksheetPart;
SharedStringTablePart stringTable = workbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
Row row = sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).First();
if (row != null)
{
foreach (Cell c in row.Elements<Cell>())
{
string cellText;
if (c.DataType == CellValues.SharedString)
{
//the value will be a number which is an index into the shared strings table
int index = int.Parse(c.CellValue.InnerText);
cellText = stringTable.SharedStringTable.ElementAt(index).InnerText;
}
else
{
//just take the value from the cell (note this won't work for dates and other types)
cellText = c.CellValue.InnerText;
}
if (cellText == textToFind)
{
return c.CellReference;
}
}
}
}
}
return null;
}
如果您只想要D
而不是D1
,可以使用简单的regex
删除数字:
private static string GetColumnName(string cellReference)
{
if (cellReference == null)
return null;
return Regex.Replace(cellReference, "[0-9]", "");
}
然后像这样使用它:
string cellReference = GetCellReference(@"c:\temp\test.xlsx", "Sheet1", 1, "Quantity");
Console.WriteLine(cellReference); //prints D1 for your example
string cellReference = GetCellReference(@"c:\temp\test.xlsx", "Sheet1", 1, "Quantity");
Console.WriteLine(GetColumnName(cellReference)); //prints D for your example
不幸的是,没有一个方法可以调用来找到正确的单元格。相反,您需要遍历单元格以找到匹配的文本。让事情稍微复杂一点,单元格中的值并不总是实际文本。相反,字符串可以存储在
SharedStringTablePart
中,单元格的值是该表内容的索引
类似于以下内容的内容应满足您的要求:
private static string GetCellReference(string filename, string sheetName, int rowIndex, string textToFind)
{
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filename, false))
{
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
//get the correct sheet
Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sheetName).First();
if (sheet != null)
{
WorksheetPart worksheetPart = workbookPart.GetPartById(sheet.Id) as WorksheetPart;
SharedStringTablePart stringTable = workbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
Row row = sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).First();
if (row != null)
{
foreach (Cell c in row.Elements<Cell>())
{
string cellText;
if (c.DataType == CellValues.SharedString)
{
//the value will be a number which is an index into the shared strings table
int index = int.Parse(c.CellValue.InnerText);
cellText = stringTable.SharedStringTable.ElementAt(index).InnerText;
}
else
{
//just take the value from the cell (note this won't work for dates and other types)
cellText = c.CellValue.InnerText;
}
if (cellText == textToFind)
{
return c.CellReference;
}
}
}
}
}
return null;
}
如果您只想要D
而不是D1
,可以使用简单的regex
删除数字:
private static string GetColumnName(string cellReference)
{
if (cellReference == null)
return null;
return Regex.Replace(cellReference, "[0-9]", "");
}
然后像这样使用它:
string cellReference = GetCellReference(@"c:\temp\test.xlsx", "Sheet1", 1, "Quantity");
Console.WriteLine(cellReference); //prints D1 for your example
string cellReference = GetCellReference(@"c:\temp\test.xlsx", "Sheet1", 1, "Quantity");
Console.WriteLine(GetColumnName(cellReference)); //prints D for your example
请看这个问题寻求帮助:上面的问题没有回答我的问题。它说明如何在知道行和列位置的情况下获取单元格值。我的问题是我知道单元格值和行数,我想要列号。有一个简单的方法可以做到:你的列位置-56=会给你列号,因为excel总是从“a”开始编号。请查看此问题以获取帮助:上面的问题没有回答我的问题。它说明如何在知道行和列位置的情况下获取单元格值。我的问题是我知道单元格的值和行,我想要列号。有一个简单的方法可以做到:你的_column_position-56=会给你列号,因为excel总是从“a”开始编号。