简体   繁体   English

如何在C#中使用OpenXML读取Excel的空白单元格列值

[英]How to read blank cell column value of excel using OpenXML in C#

Here in my Execl Sheet there are some blank value column cell, so When I use This code then in get Error "Object reference not set to an instance of an object". 在我的Execl工作表中,有一些空白值列单元格,因此当我使用此代码时,出现错误“对象引用未设置为对象的实例”。

foreach (Row row in rows)
{
   DataRow dataRow = dataTable.NewRow();
   for (int i = 0; i < row.Descendants<Cell>().Count(); i++)
   {
        dataRow[i] = GetCellValue(spreadSheetDocument, row.Descendants<Cell>().ElementAt(i));
   }

   dataTable.Rows.Add(dataRow);
}

private static string GetCellValue(SpreadsheetDocument document, Cell cell)
{
    SharedStringTablePart stringTablePart = document.WorkbookPart.SharedStringTablePart;

    string value = cell.CellValue.InnerXml;

    if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString)
    {
        return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(value)].InnerText;
    }
    else
    {
        return value;
    }
}

The "CellValue" don't necessarily exist. “ CellValue”不一定存在。 In your case it is "null", so you have your error. 在您的情况下,它为“ null”,因此会出现错误。 To read your blank cell : 读取空白单元格:

If you don't want format the result depending what the cell contain, try 如果您不想根据单元格包含的内容格式化结果,请尝试

private static string GetCellValue(Cell cell)
{
    return cell.InnerText;
}

if you want to format your cell before returning the value of it 如果要在返回值之前格式化单元格

private static string GetCellValue(SpreadsheetDocument doc, Cell cell)
{
    // if no dataType, return the value of the innerText of the cell
    if (cell.DataType == null) return cell.InnerText;

    // depending type of the cell
    switch (cell.DataType.Value)
    {
        // string => search for CellValue
        case CellValues.String:
            return cell.CellValue != null ? cell.CellValue.Text : string.Empty;

        // inlineString => search of InlineString
        case CellValues.InlineString:
            return cell.InlineString != null ? cell.InlineString.Text.Text : string.Empty;

        // sharedString => search for the SharedString
        case CellValues.SharedString:
            // is sharedPart exist ?
            if (doc.WorkbookPart.SharedStringTablePart == null) doc.WorkbookPart.SharedStringTablePart = new SharedStringTablePart();
            // is the text exist ?
            foreach (SharedStringItem item in doc.WorkbookPart.SharedStringTablePart.SharedStringTable.Elements<SharedStringItem>())
            {
                // the text exist, return it from SharedStringTable
                if (item.InnerText == cell.InnerText) return cell.InnerText;
            }
            // no text in sharedStringTable, create it and return it
            doc.WorkbookPart.SharedStringTablePart.SharedStringTable.Append(new SharedStringItem(new DocumentFormat.OpenXml.Spreadsheet.Text(cell.InnerText)));
            doc.WorkbookPart.SharedStringTablePart.SharedStringTable.Save();
            return cell.InnerText;

        // default case : bool / number / date
        // return the value of the cell in plain text
        // you can parse types depending your needs
        default:
            return cell.InnerText;
    }
}

Two usefull documentations: 两个有用的文档:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 OpenXML C# 获取 Excel 中单元格的列索引 - Get the Column Index of a Cell in Excel using OpenXML C# 如何在 C# 中使用 OpenXML 将 RTF/HTML 放入 Excel 单元格 - How to put RTF/HTML into Excel cell using OpenXML in C# 如何使用带有c#的openxml(不是整个行/列)隐藏和保护excel文件中一个Cell的文本? - How can I hide and protect the text of one Cell in an excel file using openxml with c# (not the entire row/ column)? 在C#中使用OpenXml在Excel中创建日期单元格中的问题 - Issue in Creating Date cell in Excel using OpenXml in C# 如何使用OpenXML库和C#获取Excel文档中当前选定的单元格或范围 - How to get currently selected cell or range in Excel document using OpenXML library and C# C#如何使用OpenXmlWriter(OpenXML SDK 2.5)将单元格附加到Excel工作表中的每一行 - C# How do I append a Cell to every Row in a Excel Sheet using OpenXmlWriter (OpenXML SDK 2.5) 如何使用OpenXML和C#在Excel中添加带有单元格数据和样式的新行 - How to add new row with cell data and styles in Excel using OpenXML and C# 如何使用 DocumentFormat.OpenXML 或 ClosedXML C# 从 Excel 获取合并单元格 - How to get a merged cell from Excel using DocumentFormat.OpenXML or ClosedXML C# 如何使用OpenXML C#将特定于Excel的行和工作表读取到数据表中 - How to read file excel row and sheet specific using openXML C# into data table 如何在c#中使用openxml启用对excel中某些指定单元格和工作表的只读权限 - How to enable read only permision to some specified cells and sheets in excel using openxml in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM