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

C# 将Excel工作簿另存为HTML-无法访问';System.IO.MemoryStream';,c#,excel,.net,C#,Excel,.net,我有一些代码,我想将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

我有一些代码,我想将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); 
我收到此错误
无法访问“System.IO.MemoryStream”。

例外是

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: 
有什么建议吗?

根据您发布的stacktrace,第一个参数是文件名(字符串)。它接受MemoryStream(或您想传递给它的任何其他内容)的原因是因为参数类型是对象。但在运行时,它需要一个字符串

文件名可选对象。一个字符串,指示要保存的文件的名称。您可以包含完整路径;如果没有,Microsoft Excel会将文件保存在当前文件夹中

我在
SaveAs
方法中没有看到任何接受流的参数。看起来,如果要保存工作簿,必须将其保存到磁盘。

因为,无法使用可用的API将工作簿保存到
内存流

最好的解决方案是将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);

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

完整的异常消息和堆栈跟踪是什么?您的哪行代码导致了错误?您介意粘贴完整的异常吗?好的,我已经添加了异常详细信息好的,那么有没有其他方法可以获取电子表格的内容并将其用作电子邮件正文?@Ja77aman,正如Winney提到的,您无法以这种方式执行。如果您希望将其保存到memorystream,那么最好查看一些第三方库,例如GemBox.Spreadsheet