【发布时间】: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:
有什么建议吗?
【问题讨论】:
-
什么是完整的异常消息和堆栈跟踪?您的代码的哪一行导致了错误?
-
你介意粘贴完整的例外吗?
-
好的,我已经添加了异常详情