c# - 从单元格值中获取列号,openxml

标签 c# excel openxml openxml-sdk

我有一个 Excel,其中我想获取下图的列号: Sample Image

在上图中,我知道记录将出现在第一行,但我不确定列号。在上面的示例中,列值:“数量”出现在“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;
}

然后可以这样调用:

string cellReference = GetCellReference(@"c:\temp\test.xlsx", "Sheet1", 1, "Quantity");
Console.WriteLine(cellReference); //prints D1 for your example

如果你只想要 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(GetColumnName(cellReference)); //prints D for your example

关于c# - 从单元格值中获取列号,openxml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27738041/

相关文章:

c# - 我需要将线程访问同步到 int 吗?

excel - 如何替换 Excel 中特定的未知字符?

Python .xlsx (Office OpenXML) 阅读器像 csv 模块一样简单?

c# - OpenXML、SAX 和简单地读取 Xlsx 文件

java - 使用 Apache POI 在 Microsoft Excel XSSF 中存储电话号码

c#更改变量内存地址

c# - 通过 Rfc2898DeriveBytes 的密码哈希 - 传递给 getBytes 的内容

c# - HttpRequestException 在 Windows Phone 中的激活事件中使用 HttpClient

excel - Excel VBA中单元格的引用部分

vba - 在 VBA 中查找指定 Excel 工作表上的单元格而不激活工作表