I am working with embedding MS Word and MS Excel file to Windows Form WebBrowser. I have successfully done it with MS Word but I do not have any idea how to do it in MS Excel.
Code in Embedding MS Word in Winform WebBrowser:
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));
}
My problem is that, I do not have any idea how to convert excel to HTML, replacing the codes inside the ConvertDocument method. Any help?
Aucun commentaire:
Enregistrer un commentaire