OpenXML SpreadsheetDocument SaveAs() 给出正在使用的文件错误
OpenXML SpreadsheetDocument SaveAs() giving file-in-use error
我正在尝试使用 OpenXMLPowerTools v4.5.3.2、DocumentFormat.OpenXML v2.9.1 动态生成 Excel 电子表格,从 ASP.Net 核心网络应用调用。
我已经确认能够生成电子表格。
问题是我需要生成电子表格...并且return它作为内存流(所以ASP.Net核心网络控制器
HCExcelReport.cs:
class HCExcelReport
{
protected SpreadsheetDocument doc;
protected MemorySpreadsheet ms;
protected string tmpFile = System.IO.Path.GetTempFileName();
public MemoryStream Generate(List<CandidateRecords> candidateRecords)
{
MemoryStream memoryStream = null;
using (OpenXmlMemoryStreamDocument streamDoc = OpenXmlMemoryStreamDocument.CreateSpreadsheetDocument())
{
doc = streamDoc.GetSpreadsheetDocument();
ms = new MemorySpreadsheet();
...
// Save to disk=
doc.SaveAs(tmpFile); // <-- Successfully writes .. but file remains open
doc.Dispose(); // <-- file *STILL* remains open
}
// Copy to memory stream
using (FileStream fileStream = new FileStream(tmpFile, FileMode.Open, FileAccess.Read)) // <-- Exception: the file is in use by another process!!!!
{
memoryStream = new MemoryStream();
fileStream.CopyTo(memoryStream);
memoryStream.Position = 0;
}
return memoryStream;
我尝试过无数种方法...
...但是如果我使用 PowerTools,我看不到 doc.SaveAs()
...
的任何替代方案
...如果我使用 doc.SaveAs()
,该文件似乎会保留 "in use",直到我的网络应用程序退出。
问:有什么建议吗?
请看这个postHow to copy a file while it is being used by another process
// Copy to memory stream
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
memoryStream = new MemoryStream();
fs.CopyTo(memoryStream );
fs.Close();
}
return memoryStream;
问候马塞尔
我正在尝试使用 OpenXMLPowerTools v4.5.3.2、DocumentFormat.OpenXML v2.9.1 动态生成 Excel 电子表格,从 ASP.Net 核心网络应用调用。
我已经确认能够生成电子表格。
问题是我需要生成电子表格...并且return它作为内存流(所以ASP.Net核心网络控制器
HCExcelReport.cs:
class HCExcelReport
{
protected SpreadsheetDocument doc;
protected MemorySpreadsheet ms;
protected string tmpFile = System.IO.Path.GetTempFileName();
public MemoryStream Generate(List<CandidateRecords> candidateRecords)
{
MemoryStream memoryStream = null;
using (OpenXmlMemoryStreamDocument streamDoc = OpenXmlMemoryStreamDocument.CreateSpreadsheetDocument())
{
doc = streamDoc.GetSpreadsheetDocument();
ms = new MemorySpreadsheet();
...
// Save to disk=
doc.SaveAs(tmpFile); // <-- Successfully writes .. but file remains open
doc.Dispose(); // <-- file *STILL* remains open
}
// Copy to memory stream
using (FileStream fileStream = new FileStream(tmpFile, FileMode.Open, FileAccess.Read)) // <-- Exception: the file is in use by another process!!!!
{
memoryStream = new MemoryStream();
fileStream.CopyTo(memoryStream);
memoryStream.Position = 0;
}
return memoryStream;
我尝试过无数种方法...
...但是如果我使用 PowerTools,我看不到 doc.SaveAs()
...
...如果我使用 doc.SaveAs()
,该文件似乎会保留 "in use",直到我的网络应用程序退出。
问:有什么建议吗?
请看这个postHow to copy a file while it is being used by another process
// Copy to memory stream
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
memoryStream = new MemoryStream();
fs.CopyTo(memoryStream );
fs.Close();
}
return memoryStream;
问候马塞尔