简体   繁体   中英

Saving HTML from MemoryStream into a Excel file

I have a XSLT transformed HTML data in MemoryStream (in C#). I am trying to convert this to an Excel format before emailing, preferably conversion happens all in memory again without saving to local disk. I can worry about the email attachment part later. Can anyone point me to a sample on how I could do the conversion from HTML to Excel format either through OpenXML or with Office.Interop.Excel.

The HTML data is well formed and I could manually do the conversion by opening the html in Excel application and do a Save As to save it in xlsx format (Office 2010), no problem. I also tried to simply change the .html extension to .xlsx, but then excel complains about opening it.

What's the best way to automate the manual SaveAs action so that I could use the same html data in Excel format? I understand that I could create a separate .xslt for directly converting my XML into Excel format. But, that'll be too many .xslt to maintain. I'm trying to find the hack to let Excel do the work for me.

Thank you for any and all pointers in advance!

EDIT:

I figured I have no choice but to store html to disk and read it back and use Excel Interop to do SaveAs method. When I did try though, getting the exception with HRESULT: 0x800A03EC on the SaveAs method. Here's how to reproduce it.

steps to reproduce the behavior

  1. Save this text

<html><head></head><body><center><h1>Test Header</h1></center></body></html>

as C:\\Test.html

  1. after making reference to Excel interop like this,

using Excel = Microsoft.Office.Interop.Excel;

Try this code

` var app = new Excel.Application();

Excel.Workbook wb = null;

            try
            {
                wb = app.Workbooks.Open(@"c:\test.html");
                wb.SaveAs(@"c:\test.xlsx", Excel.XlFileFormat.xlOpenDocumentSpreadsheet);
                //wb.SaveCopyAs(@"c:\test.xlsx");
                wb.Close();
            }
            catch (Exception ex)
            {
                //_logger.Error(ex);
            }
            finally
            {
                app.Quit();
            }

`

I always get the mentioned exception on SaveAs no matter which fileformat I choose or even not mentioning the fileformat there..

Any ideas?

This code works. It turns out the exception I was getting is only related to the file format I was trying to save. When I changed it to Open XML workbook, it saved fine.

using Excel = Microsoft.Office.Interop.Excel;
.
.
.

var app = new Excel.Application();

    Excel.Workbook wb = null;

                        try
                        {
                            wb = app.Workbooks.Open(@"c:\test.html");
                            wb.SaveAs(@"c:\test.xlsx", Excel.XlFileFormat.xlOpenXMLWorkbook);
                            //wb.SaveCopyAs(@"c:\test.xlsx");
                            wb.Close();
                        }
                        catch (Exception ex)
                        {
                            //_logger.Error(ex);
                        }
                        finally
                        {
                            app.Quit();
                        }

Here's the updated code that takes bytes[] html as input and returns xlsx in bytes[]

public static byte[] DoConvertXlDataToOpenXml(byte[] data, FileInfo fileInfo)
        {
            ExcelInterop.Application excelApp = null;
            ExcelInterop.Workbooks workBooks = null;
            ExcelInterop.Workbook workBook = null;
            FileInfo tempFile = null;
            FileInfo convertedTempFile = null;

            try
            {
                //Stream the file to temporary location, overwrite if exists
                tempFile = new FileInfo(Path.ChangeExtension(Path.Combine(Path.GetTempFileName()), fileInfo.Extension));

                using (var destStream = new FileStream(tempFile.FullName, FileMode.Create, FileAccess.Write))
                {
                    destStream.Write(data, 0, data.Length);
                }

                //open original
                excelApp = new ExcelInterop.Application();
                excelApp.Visible = false;
                excelApp.DisplayAlerts = false;

                workBooks = excelApp.Workbooks;

                workBook = workBooks.Open(tempFile.FullName);

                convertedTempFile = new FileInfo(Path.ChangeExtension(Path.GetTempFileName(), "XLSX"));

                //Save as XLSX
                excelApp.Application.ActiveWorkbook.SaveAs(
                     convertedTempFile.FullName
                     , Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook
                     , ConflictResolution: ExcelInterop.XlSaveConflictResolution.xlLocalSessionChanges);

                excelApp.Application.ActiveWorkbook.Close();

                return File.ReadAllBytes(convertedTempFile.FullName);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (workBooks != null)
                    Marshal.ReleaseComObject(workBooks);

                if (workBook != null)
                    Marshal.ReleaseComObject(workBook);

                if (excelApp != null)
                    Marshal.ReleaseComObject(excelApp);

                if (tempFile != null && tempFile.Exists)
                    tempFile.Delete();

                if (convertedTempFile != null && convertedTempFile.Exists)
                {
                    convertedTempFile.Delete();
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM