MemoryStream似乎在NPOI workbook.write后关闭?
MemoryStream似乎在NPOI workbook.write后关闭?
源代码这么写的
using (var ms = new MemoryStream()) { workbook.Write(ms); ms.Flush(); ms.Position = 0; sheet.Dispose(); workbook.Dispose(); return ms; }
解决此问题的另一个解决方法...它不使用多个MemoryStream
对象。
创建一个NpoiMemoryStream
继承的类MemoryStream
,并覆盖该Close
方法:
public class NpoiMemoryStream : MemoryStream { public NpoiMemoryStream() { AllowClose = true; } public bool AllowClose { get; set; } public override void Close() { if (AllowClose) base.Close(); } }
然后,像这样使用该流:
var ms = new NpoiMemoryStream(); ms.AllowClose = false; workbook.Write(ms); ms.Flush(); ms.Seek(0, SeekOrigin.Begin); ms.AllowClose = true;