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

3

我有一些代码,想要将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: 

有什么建议吗?

1
完整的异常消息和堆栈跟踪是什么?你的代码哪一行引起了错误? - Dai
你介意也把完整的异常信息粘贴过来吗? - Panda Zhang
1
好的,我已经添加了异常详细信息。 - Ja77aman
2个回答

4
@GrantWinney在他的回答中指出,使用现有的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);

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


2
根据MSDN和您发布的堆栈跟踪信息,第一个参数是文件名(字符串)。它接受您的MemoryStream (或任何其他您想传递给它的内容)是因为参数类型是对象。但在运行时,它期望一个字符串。

Filename 可选对象。指示要保存的文件名的字符串。您可以包括完整路径;如果不这样做,Microsoft Excel将文件保存在当前文件夹中。

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

好的,那么有没有其他方法可以获取电子表格的内容并将其用作电子邮件的正文? - Ja77aman
@Ja77aman,正如Winney所提到的,您无法以这种方式完成。如果您想将其保存到内存流中,最好查看一些第三方库,例如GemBox.Spreadsheet。 - Panda Zhang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接