【问题标题】:Saving Excel workbook as HTML - Cannot access 'System.IO.MemoryStream'将 Excel 工作簿另存为 HTML - 无法访问“System.IO.MemoryStream”
【发布时间】:2014-03-04 03:46:01
【问题描述】:

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

有什么建议吗?

【问题讨论】:

  • 什么是完整的异常消息和堆栈跟踪?您的代码的哪一行导致了错误?
  • 你介意粘贴完整的例外吗?
  • 好的,我已经添加了异常详情

标签: c# excel .net


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 好的,那么有没有其他方法可以获取电子表格的内容并将其用作电子邮件的正文?
    • @Ja77aman,正如 Winney 所说,您无法以这种方式进行操作。如果您热衷于将其保存到 memorystream,最好查看一些第三方库,例如 GemBox.Spreadsheet
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 2015-06-06
    • 2022-01-05
    • 1970-01-01
    • 2015-09-07
    相关资源
    最近更新 更多