使用 openXML 从 Excel 导入时出现异常

发布于 2024-12-09 05:19:18 字数 1273 浏览 0 评论 0原文

我想将 Excel 文件中的所有数据反序列化为列表。

我正在使用这段代码

class ExcelImport
{
    Workbook workBook;
    SharedStringTable sharedStrings;
    IEnumerable<Sheet> workSheets;
    WorksheetPart custSheet;
    WorksheetPart orderSheet;
    string FilePath;
    ExcelStorage provider;
    Stiker[] ans;
    List<Stiker> StikerList;

    public ExcelImport(string fp)
    {
        FilePath = fp;


    }




    public List<Stiker> dothejob()
    {
        using (SpreadsheetDocument document =
    SpreadsheetDocument.Open(FilePath, true))
        {
            StikerList= new List<Stiker>();
            workBook = document.WorkbookPart.Workbook;
            workSheets = workBook.Descendants<Sheet>();
            sharedStrings = document.WorkbookPart.SharedStringTablePart.SharedStringTable;
            StikerList = Stiker.LoadStiker(custSheet.Worksheet, sharedStrings);
            return StikerList;
        }
    }

,但由于某些原因,我在以下行中得到了异常:sharedStrings = document.WorkbookPart.SharedStringTablePart.SharedStringTable;

“对象引用未设置为对象的实例。”。

经过上面的saggestion发现

if (sharedStringTablePart == null)
{
// report a problem
}

rerurn null

有什么想法吗?

I would like to deserialize all the data from the excel file to list.

I am using this code

class ExcelImport
{
    Workbook workBook;
    SharedStringTable sharedStrings;
    IEnumerable<Sheet> workSheets;
    WorksheetPart custSheet;
    WorksheetPart orderSheet;
    string FilePath;
    ExcelStorage provider;
    Stiker[] ans;
    List<Stiker> StikerList;

    public ExcelImport(string fp)
    {
        FilePath = fp;


    }




    public List<Stiker> dothejob()
    {
        using (SpreadsheetDocument document =
    SpreadsheetDocument.Open(FilePath, true))
        {
            StikerList= new List<Stiker>();
            workBook = document.WorkbookPart.Workbook;
            workSheets = workBook.Descendants<Sheet>();
            sharedStrings = document.WorkbookPart.SharedStringTablePart.SharedStringTable;
            StikerList = Stiker.LoadStiker(custSheet.Worksheet, sharedStrings);
            return StikerList;
        }
    }

But from some reson I get exception in the line:sharedStrings =
document.WorkbookPart.SharedStringTablePart.SharedStringTable;

that "Object reference not set to an instance of an object.".

After the above saggestion found that the

if (sharedStringTablePart == null)
{
// report a problem
}

rerurn null

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃气十足 2024-12-16 05:19:18

源行中的属性之一将为“null”并且没有值。

您需要使用调试器来解决这个问题(在该行上设置断点并将鼠标悬停在每个属性上),或者将该行分解为单独的语句。类似这样:

var workBookPart = document.WorkbookPart;

if (workBookPart == null)
{
    // do something to report a problem
}

var sharedStringTablePart = workBookPart.SharedStringTablePart;
if (sharedStringTablePart == null)
{
    // report a problem
}

sharedStrings = sharedStringTablePart.SharedStringTable;

这样您的代码可以在运行时确定是否存在问题:在处理由您自己的系统以外的其他系统创建的数据时,这种“防御”想法通常是一个好主意。

One of the properties in the source line will be "null" and have no value.

You'll want to either use a debugger to figure this out (set a breakpoint on the line and hover the mouse over each property), or break down the line into separate statements. Something like:

var workBookPart = document.WorkbookPart;

if (workBookPart == null)
{
    // do something to report a problem
}

var sharedStringTablePart = workBookPart.SharedStringTablePart;
if (sharedStringTablePart == null)
{
    // report a problem
}

sharedStrings = sharedStringTablePart.SharedStringTable;

This way your code can determine at run-time if there's an issue: this kind of "defensive" idea is usually a good idea when working with data created by some system other than your own.

不爱素颜 2024-12-16 05:19:18

经过半天的解析 2007、2010、2013 并转换一些 2003->2007 并解析它们后,我得到了一种在某些情况下解析 excel 发出 SharedStringTable 的方法

 var link = document.WorkbookPart.SharedStringTablePart;
           Func<Cell, string> selector = (cell) => cell.InnerText;

          if (link != null)
           {
               SharedStringTable sharedStringTable = document.WorkbookPart.SharedStringTablePart.SharedStringTable;
               selector = (cell) => cell.DataType == null ? cell.InnerText : cell.DataType == CellValues.SharedString ? sharedStringTable.ElementAt(Int32.Parse(cell.InnerText)).InnerText : cell.InnerText;

          }

         var values = wsPart.Worksheet.Descendants<Cell>().Select(cell =>selector(cell) ).ToArray();

After half a day parsing 2007, 2010, 2013 and convert some 2003->2007 and parsing them I got one method of parsing excel emitting SharedStringTable in some cases

 var link = document.WorkbookPart.SharedStringTablePart;
           Func<Cell, string> selector = (cell) => cell.InnerText;

          if (link != null)
           {
               SharedStringTable sharedStringTable = document.WorkbookPart.SharedStringTablePart.SharedStringTable;
               selector = (cell) => cell.DataType == null ? cell.InnerText : cell.DataType == CellValues.SharedString ? sharedStringTable.ElementAt(Int32.Parse(cell.InnerText)).InnerText : cell.InnerText;

          }

         var values = wsPart.Worksheet.Descendants<Cell>().Select(cell =>selector(cell) ).ToArray();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文