相关疑难解决方法(0)

打开xml excel读取单元格值

我正在使用Open XML SDK打开Excel xlsx文件,并尝试读取每个工作表中位置A1的单元格值.我使用以下代码:

using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(openFileDialog1.FileName, false))
{
    var sheets = spreadsheetDocument.WorkbookPart.Workbook.Descendants<Sheet>();

    foreach (Sheet sheet in sheets)
    {
        WorksheetPart worksheetPart = (WorksheetPart)spreadsheetDocument.WorkbookPart.GetPartById(sheet.Id);
        Worksheet worksheet = worksheetPart.Worksheet;

        Cell cell = GetCell(worksheet, "A", 1);

        Console.Writeline(cell.CellValue.Text);
     }
}

private static Cell GetCell(Worksheet worksheet, string columnName, uint rowIndex)
{
     Row row = GetRow(worksheet, rowIndex);

     if (row == null)
         return null;

     return row.Elements<Cell>().Where(c => string.Compare
               (c.CellReference.Value, columnName +
               rowIndex, true) == 0).First();
}

// Given a worksheet and a row index, return …
Run Code Online (Sandbox Code Playgroud)

c# openxml openxml-sdk

47
推荐指数
3
解决办法
7万
查看次数

标签 统计

c# ×1

openxml ×1

openxml-sdk ×1