开发者

Exception while import from excel using openXML

开发者 https://www.devze.com 2023-04-12 18:04 出处:网络
I would like to deserialize all the data from the excel file to list. I am using this code class ExcelImport

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 St开发者_C百科ikerList;
        }
    }

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?


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.


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消