使用 openXML 从 Excel 导入时出现异常
我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
源行中的属性之一将为“null”并且没有值。
您需要使用调试器来解决这个问题(在该行上设置断点并将鼠标悬停在每个属性上),或者将该行分解为单独的语句。类似这样:
这样您的代码可以在运行时确定是否存在问题:在处理由您自己的系统以外的其他系统创建的数据时,这种“防御”想法通常是一个好主意。
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:
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.
经过半天的解析 2007、2010、2013 并转换一些 2003->2007 并解析它们后,我得到了一种在某些情况下解析 excel 发出 SharedStringTable 的方法
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