C#如何通过openXML中传递行和列来获取单元格值?

问题描述 投票:0回答:1

Net core,我正在尝试通过打开xml来读取excel。 我试图通过传递某些行和列详细信息来获取单元格值。

以下是我的实现。

private DataTable ConvertExcelToDataTable(IFormFile uploadRegistration)
{
    //Create a new DataTable.
    var table = new DataTable();

    using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(uploadRegistration.OpenReadStream(), false))
    {
        WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart;
        IEnumerable<Sheet> sheets = spreadSheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>();
        string relationshipId = sheets.First().Id.Value;
        WorksheetPart worksheetPart = (WorksheetPart)spreadSheetDocument.WorkbookPart.GetPartById(relationshipId);
        Worksheet workSheet = worksheetPart.Worksheet;
        SheetData sheetData = workSheet.GetFirstChild<SheetData>();
        IEnumerable<Row> rows = sheetData.Descendants<Row>();
        foreach (Cell cell in rows.ElementAt(0))
        {
            table.Columns.Add(GetCellValue(spreadSheetDocument, cell));
        }
        foreach (Row row in rows)
        {
            DataRow tempRow = table.NewRow();
            for (int i = 0; i < row.Descendants<Cell>().Count(); i++)
            {
                // tempRow[i] = GetCellValue(spreadSheetDocument, row.Descendants<Cell>().ElementAt(i));
                Console.WriteLine(GetCellValue(spreadSheetDocument, row.Descendants<Cell>().ElementAt(i))); // Here i am able to display cell value but not row and column details
            }
            table.Rows.Add(tempRow);
        }
    }
    table.Rows.RemoveAt(0);
    return table;
}

我有尝试获取行和列详细信息的方法,如下所示

public static (int row, int col) ParseCellAddress(string s)
{
     StringBuilder stringBuilderCol = new StringBuilder();
     StringBuilder stringBuilderRow = new StringBuilder();

     foreach (char c in s)
     {
         if (char.IsLetter(c))
         {
             if (stringBuilderRow.Length > 0)
             {
                 throw new ArgumentException($"Invalid Cell adress {s}.");
             }

             stringBuilderCol.Append(c);
         }
         else if (char.IsDigit(c))
         {
             if (stringBuilderCol.Length < 1)
             {
                 throw new ArgumentException($"Invalid Cell adress {s}.");
             }

             stringBuilderRow.Append(c);
         }
         else
         {
             throw new ArgumentException($"Invalid Cell adress {s}.");
         }
     }

     if (stringBuilderRow.Length == 0 || stringBuilderCol.Length == 0)
     {
         throw new ArgumentException($"Invalid Cell adress {s}.");
     }

     int rowIdx = int.Parse(stringBuilderRow.ToString());

     // calc column index from column name

     string columnName = stringBuilderCol.ToString();

     int colIdx = 0;
     int pow = 1;
     for (int i = columnName.Length - 1; i >= 0; i--)
     {
         colIdx += (columnName[i] - 'A' + 1) * pow;
         pow *= 26;
     }

     return (rowIdx, colIdx);
 }

在上述方法中,我可以获得行和列的详细信息。

现在我正在寻找的是,如果我传递特定的行和详细信息,例如第 2 行第 4 列,那么我想获取单元格值。我正在寻找某种方法,它将行和列作为输入并获取单元格值

private string GetCellValue(int row, int column){
//return data for this particular row and column
}

有人可以帮我解决这个问题吗?任何帮助,将不胜感激。谢谢

.net excel asp.net-core openxml iformfile
1个回答
0
投票

您可以尝试使用下面的方法获取特定行的特定单元格

 private static Cell GetCell(Worksheet worksheet, string columnName, uint rowIndex)
{
    Row row = worksheet.GetFirstChild<SheetData>().Elements<Row>().FirstOrDefault(r => r.RowIndex == rowIndex);
    if (row != null)
    {
        return row.Elements<Cell>().FirstOrDefault(c => string.Compare(c.CellReference.Value, columnName + rowIndex, true) == 0);
    }
    return null;
}
© www.soinside.com 2019 - 2024. All rights reserved.