从内存流创建XLWorkbook

时间:2016-03-13 22:46:49

标签: c# xml ssrs-2012 closedxml

我想从内存流中创建XLWorkbook对象。 xlsx文件的字节被写入内存流。但是当我尝试创建XLWorkbook对象时,我得到了异常:NullReference异常被捕获。任何人都可以帮我这个吗?

//...
ssrsHelper.ExecuteReport( model, "EXCELOPENXML" );

MemoryStream memStream = new MemoryStream();
memStream.Write( model.ReportBits, 0, model.ReportBits.Length );

//in this line I get the exception
XLWorkbook wb = new XLWorkbook( memStream, XLEventTracking.Disabled );

1 个答案:

答案 0 :(得分:0)

尝试这一点(假设model.ReportBits是一个有效的xlsx文件格式的有效字节数组):

using(MemoryStream memStream = new MemoryStream()) {
    memStream.Write(model.ReportBits, 0, model.ReportBits.Length);
    XLWorkbook wb = new XLWorkbook(memStream, XLEventTracking.Disabled);
    /*  ... */
    wb.SaveAs(pathToSavedFile);
}