gpt4 book ai didi

c# - 将 Excel 工作簿另存为 HTML - 无法访问 'System.IO.MemoryStream'

转载 作者:行者123 更新时间:2023-11-30 20:50:54 26 4
gpt4 key购买 nike

我有一些代码,我想将 excel 电子表格转换为 html,这样我就可以将它用作电子邮件的正文。

        Excel.Application excel = new Excel.Application();
Excel.Workbook workbook = excel.Workbooks.Open(Properties.Settings.Default.FileToSend);

//Save the workbook to Memory Stream in HTML format
MemoryStream ms = new MemoryStream();

// This is the line i have an error on
workbook.SaveAs(ms, Excel.XlFileFormat.xlHtml);

//Seek MemoryStream position to 0
ms.Position = 0;

//Define a StreamReader object with the above MemoryStream
StreamReader sr = new StreamReader(ms);

//Load the saved HTML from StreamReader now into a string variable
string strHtmlBody = sr.ReadToEnd();

这是我得到错误的行

        // This is the line i have an error on
workbook.SaveAs(ms, Excel.XlFileFormat.xlHtml);

我收到此错误 Cannot access 'System.IO.MemoryStream'。

异常(exception)情况是

System.Runtime.InteropServices.COMException was unhandled
HelpLink=xlmain11.chm
HResult=-2146827284
Message=Cannot access 'System.IO.MemoryStream'.
Source=Microsoft Excel
ErrorCode=-2146827284
StackTrace:
at Microsoft.Office.Interop.Excel._Workbook.SaveAs(Object Filename, Object FileFormat, Object Password, Object WriteResPassword, Object ReadOnlyRecommended, Object \ CreateBackup, XlSaveAsAccessMode AccessMode, Object ConflictResolution, Object AddToMru, Object TextCodepage, Object TextVisualLayout, Object Local)
at Auto_KPI_Email.Program.sendExcel() in E:\My Documents\Visual Studio 2010\Projects\Auto KPI Email\Auto KPI Email\Program.cs:line 71
at Auto_KPI_Email.Program.Main(String[] args) in E:\My Documents\Visual Studio 2010\Projects\Auto KPI Email\Auto KPI Email\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

有什么建议吗?

最佳答案

作为@GrantWinney pointed out in his answer ,您无法使用可用的 API 将工作簿保存到 MemoryStream

最好的解决方案是将 HTML 输出到一个临时位置,然后再读回结果。

Excel.Application excel = new Excel.Application();
Excel.Workbook workbook = excel.Workbooks.Open(Properties.Settings.Default.FileToSend);

var temporaryFilepath = Path.ChangeExtension(Path.GetTempFileName(), ".html");
workbook.SaveAs(temporaryFilepath, Excel.XlFileFormat.xlHtml);

var result = File.ReadAllText(temporaryFilepath);

虽然不如将其保存在内存中那么优雅,但在与其他程序集成时,它有时是唯一的解决方案。

关于c# - 将 Excel 工作簿另存为 HTML - 无法访问 'System.IO.MemoryStream',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22162475/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com