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

C# 如何在C中使用OpenXML读取excel的空白单元格列值#,c#,excel,openxml-sdk,C#,Excel,Openxml Sdk,在我的Execl工作表中,有一些空白值列单元格,所以当我使用此代码时,在get错误“对象引用未设置为对象的实例”中 foreach(一行一行) { DataRow DataRow=dataTable.NewRow(); 对于(int i=0;i搜索CellValue 大小写单元格值。字符串: 返回cell.CellValue!=null?cell.CellValue.Text:string.Empty; //inlineString=>inlineString的搜索 大小写CellValues.

在我的Execl工作表中,有一些空白值列单元格,所以当我使用此代码时,在get错误“对象引用未设置为对象的实例”中

foreach(一行一行)
{
DataRow DataRow=dataTable.NewRow();
对于(int i=0;i字符串值=cell.CellValue.InnerXml;
if(cell.DataType!=null&&cell.DataType.Value==CellValues.SharedString)
{
返回stringTablePart.SharedStringTable.ChildElements[Int32.Parse(value)].InnerText;
}
其他的
{
返回值;
}
}
不一定存在“CellValue”。在您的情况下,它是“null”,因此您有您的错误。 要读取空白单元格,请执行以下操作:

如果不想根据单元格包含的内容格式化结果,请尝试

private static string GetCellValue(Cell cell)
{
    return cell.InnerText;
}
如果要在返回单元格值之前格式化单元格

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;
    }
}
私有静态字符串GetCellValue(电子表格文档文档,单元格)
{
//如果没有数据类型,则返回单元格的innerText的值
if(cell.DataType==null)返回cell.InnerText;
//取决于单元的类型
开关(cell.DataType.Value)
{
//string=>搜索CellValue
大小写单元格值。字符串:
返回cell.CellValue!=null?cell.CellValue.Text:string.Empty;
//inlineString=>inlineString的搜索
大小写CellValues.InlineString:
return cell.InlineString!=null?cell.InlineString.Text.Text:string.Empty;
//sharedString=>搜索sharedString
案例CellValues.SharedString:
//共享部分存在吗?
如果(doc.WorkbookPart.SharedStringTablePart==null)doc.WorkbookPart.SharedStringTablePart=new SharedStringTablePart();
//文本是否存在?
foreach(doc.WorkbookPart.SharedStringTablePart.SharedStringTable.Elements()中的SharedStringItem项)
{
//如果文本存在,请从SharedStringTable返回
if(item.InnerText==cell.InnerText)返回cell.InnerText;
}
//sharedStringTable中没有文本,请创建并返回它
doc.WorkbookPart.SharedStringTablePart.SharedStringTable.Append(新的SharedStringItem(新的DocumentFormat.OpenXml.Spreadsheet.Text(cell.InnerText));
doc.WorkbookPart.SharedStringTablePart.SharedStringTable.Save();
返回cell.InnerText;
//默认情况:bool/编号/日期
//以纯文本形式返回单元格的值
//您可以根据需要分析类型
违约:
返回cell.InnerText;
}
}
两个有用的文档:

  • 关于单元:
  • 关于共享字符串:

在哪一行出现错误?字符串值=cell.CellValue.InnerXml;
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;
    }
}