根据this article的说法,当OpenXML的"MemoryStreams达到高水位线“时,它就不是线程安全的,必须切换到IsolatedStorage。
这种情况发生在甚至小于1mb的工作簿上,因为未压缩的数据很可能是该大小的10倍。
我确实需要并发创建xlsx文件,特别是具有并发性的异常大的文件。MS的解决方案是实现如下内容(从C#转换而来),但我不确定如何处理它。
Public Class PackagePartStream
Private _stream As Stream
Private Shared _m As New Mutex(False)
Public Sub New(ByVal Stream As Stream)
_stream = Stream
End Sub
Public Function Read(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer) As Integer
Return _stream.Read(buffer, offset, count)
End Function
Public Sub Write(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer)
_m.WaitOne(Timeout.Infinite, False)
_stream.Write(buffer, offset, count)
_m.ReleaseMutex()
End Sub
Public Sub Flush()
_m.WaitOne(Timeout.Infinite, False)
_stream.Flush()
_m.ReleaseMutex()
End Sub
End Class到目前为止,我最好的猜测是这样的,但我有一种感觉,我过于简化了这一点,互斥锁需要更接近于处理OpenXML的WriteElement的函数
Dim stream As New PackagePartStream()
Using document As SpreadsheetDocument = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook, True)
WriteExcelFile(ds, document)
End Using我在.NET中没有做太多的线程工作,但希望有人能给我指明正确的方向。
发布于 2014-04-01 10:42:39
我讨厌回答我自己的问题,但是如果你在but服务器上构建动态报表,请不要使用OpenXML开发工具包。请改用EPplus。它使用了另一个不使用IsolagedStorage的打包库。令人遗憾的是,微软没有解决这个问题。
https://stackoverflow.com/questions/22718820
复制相似问题