我正在将MS Word和MS Excel文件嵌入到Windows窗体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
方法中的代码。有什么帮助吗?
答案 0 :(得分:1)
找到此代码,它完美无缺。替换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);