Click here to Skip to main content
15,894,646 members
Articles / Desktop Programming / Windows Forms

Document Preview Application

Rate me:
Please Sign up or sign in to vote.
4.74/5 (25 votes)
2 Jul 2009CPOL2 min read 133.2K   12.6K   89  
An application to preview your documents and files such as PDF, Doc, JPG, PPT, XSL.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DocExp.Interfaces;
using Microsoft.Office.Interop.Excel;

namespace DocExp.PreviewControls
{
    public class ExcelPreview : WebBrowser, IPreview
    {
        #region IPreview Members

        public void Preview(string path)
        {
            Guid g = Guid.NewGuid();
            ConvertDocument(g, path);
            this.Url = new Uri(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + g.ToString() + ".html");
        }

        #endregion

        void ConvertDocument(Guid g, string fileName)
        {
            object m = System.Reflection.Missing.Value;
            object oldFileName = (object)fileName;
            object readOnly = (object)false;
            ApplicationClass ac = null;
            System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
            try
            {
                
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
                // First, create a new Microsoft.Office.Interop.Word.ApplicationClass.    
                ac = new ApplicationClass();

                // Now we open the document.          
                Workbook doc = ac.Workbooks.Open(fileName, m,  readOnly,
                     m,  m,  m,  m,  m,  m,  m,
                     m,  m,  m,  m,  m);
                // Create a temp file to save the HTML file to.           
                string tempFileName = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + g.ToString() + ".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)Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
                // Save the file.                 
                doc.SaveAs( newFileName,  fileType,
                     m,  m,  m,  m,XlSaveAsAccessMode.xlExclusive , m,  m,  m,
                     m,  m);
            }
            finally
            {
                // Make sure we close the application class.      
                if (ac != null)
                    ac.Quit();
                System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
Turkey Turkey
Tamer Oz is a Microsoft MVP and works as Assistant Unit Manager.

Comments and Discussions