将Excel工作簿保存为HTML – 无法访问“System.IO.MemoryStream”
我有一些代码,我想将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:
有什么build议?
正如@GrantWinney在他的回答中指出的那样 ,您不能使用可用的API将工作簿保存到MemoryStream
。
您的最佳解决scheme是将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);
不如将它保存在内存中,然而当与其他程序集成时,它有时是唯一的解决scheme。
根据MSDN ,你发布的堆栈跟踪,第一个参数是一个文件名(string)。 它接受你的MemoryStream的原因(或者你想传给它的任何东西)是因为参数types是一个对象。 但在运行时,它需要一个string。
文件名可选对象。 一个string,表示要保存的文件的名称。 您可以包含完整path; 如果您不这样做,Microsoft Excel会将该文件保存在当前文件夹中。
在SaveAs
方法中,我看不到任何可以接受stream的参数。 看起来如果你想保存你的工作簿,你必须将其保存到磁盘。
- 使用Excel = Microsoft.Office.Interop.Excel编译错误
- System.Runtime.InteropServices.COMException(0x800706BA):RPC服务器不可用。 (从HRESULTexception:0x800706BA)
- 从xml文件导出到Excel或从数据集获取xml
- Excel API另存为对话框问题
- 如何从C#读取XLSB文件 – Microsoft.Office.Interop.Excel.Workbook
- Excel.Workbook单击取消引发exception
- 无需存储即可实现卓越
- F#不识别interop.office.Excel.dll
- 在Excel中列出文本