在 Windows.Forms.WebBrowser 中查看 Excel 文件(正在将 Excel 转换为 HTML)

View Excel File in Windows.Forms.WebBrowser (Converting Excel to HTML)

我正在将 MS Word 和 MS Excel 文件嵌入到 Windows Form WebBrowser。我已经用 MS Word 成功完成了,但我不知道如何在 MS Excel.

在 Winform WebBrowser 中嵌入 MS Word 的代码:

private void Form1_Load(object sender, EventArgs e)
{
    loadDocument(fileName);
}

public void loadDocument(string fileName)
{
    // Call ConvertDocument asynchronously. 
    ConvertDocumentDelegate del;
    if (PreviewClass.extension.Equals(".xlsx"))
    {
        del = new ConvertDocumentDelegate(ConvertDocument);
    }
    else
    {
        del = new ConvertDocumentDelegate(ConvertExcel);
    }
    // Call DocumentConversionComplete when the method has completed. 
    del.BeginInvoke(fileName, DocumentConversionComplete, null);
}

// Have to replace this part but do not have any idea how
void ConvertDocument(string fileName)
{
    object m = System.Reflection.Missing.Value;
    object oldFileName = (object)fileName;
    object readOnly = (object)false;
    Word.Application ac = null;
    try
    {
        // First, create a new Microsoft.Office.Interop.Word.ApplicationClass.
        ac = new Word.Application();

        // Now we open the document.
        Word.Document doc = ac.Documents.Open(ref oldFileName, ref m, ref readOnly,
            ref m, ref m, ref m, ref m, ref m, ref m, ref m,
            ref m, ref m, ref m, ref m, ref m, ref m);

        // Create a temp file to save the HTML file to. 
        tempFileName = GetTempFile("html");

        // Cast these items to object.  The methods we're calling 
        // only take object types in their method parameters. 
        object newFileName = (object)tempFileName;

        // We will be saving this file as HTML format. 
        object fileType = (object)Word.WdSaveFormat.wdFormatHTML;

        // Save the file. 
        doc.SaveAs(ref newFileName, ref fileType,
            ref m, ref m, ref m, ref m, ref m, ref m, ref m,
            ref m, ref m, ref m, ref m, ref m, ref m, ref m);
       }

       finally
       {
            // Make sure we close the application class. 
            if (ac != null)
                ac.Quit(ref readOnly, ref m, ref m);
       }

void DocumentConversionComplete(IAsyncResult result)
{
    // navigate to our temp file. 
    wbPreview.Navigate(tempFileName);
}

void webBrowser1_DocumentCompleted(object sender,
    WebBrowserDocumentCompletedEventArgs e)
{
    try
    {
        if (tempFileName != string.Empty)
        {
            // delete the temp file we created. 
            File.Delete(tempFileName);

            // set the tempFileName to an empty string. 
            tempFileName = string.Empty;
        }
    }
    catch (Exception ex)
    {
    }
}

string GetTempFile(string extension)
{
    // Uses the Combine, GetTempPath, ChangeExtension, 
    // and GetRandomFile methods of Path to 
    // create a temp file of the extension we're looking for. 
    return Path.Combine(Path.GetTempPath(),
        Path.ChangeExtension(Path.GetRandomFileName(), extension));
}

我的问题是,我不知道如何将 excel 转换为 HTML,替换 ConvertDocument 方法中的代码。有帮助吗?

找到这段代码,它运行良好。替换 ConvertDocument 方法中的代码:

Excel.Application excel = new Excel.Application();
excel.Visible = false;
Excel.Workbook xlWorkbook = excel.Workbooks.Open(fileName);
Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[nameOfSheet];

tempFileName = GetTempFile("html");
object missing = System.Reflection.Missing.Value;
object newFileName = (object)tempFileName;
object fileType = (object)Excel.XlFileFormat.xlHtml;

xlWorkbook.SaveAs(tempFileName, fileType, missing, missing, missing, missing,
      Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
      missing, missing, missing, missing, missing);