将Excel工作簿另存为HTML - 无法访问“System.IO.MemoryStream”

时间:2014-03-04 03:46:01

标签: 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 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'.

例外是

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: 

有什么建议吗?

2 个答案:

答案 0 :(得分:3)

作为@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);

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

答案 1 :(得分:1)

根据MSDN和您发布的堆栈跟踪,第一个参数是文件名(字符串)。它接受你的MemoryStream (或者你想传递给它的任何其他东西)的原因是因为参数类型是一个对象。但在运行时,它需要一个字符串。

  

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

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