Click here to Skip to main content
15,896,278 members
Articles / Programming Languages / C# 4.0

Microsoft Interop API to convert the .doc, .docx, .dot, .dotx and .xls,.xlsx, .rtf to HTML

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
13 Dec 2012CPOL3 min read 88K   6.4K   34  
Convert Word documents, Excel sheets to HTML files using Microsoft Office Interop API and render the result back to a client browser.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eShopping.Common.Converter;

namespace eShopping.WebForms.FileHandler
{
    public partial class Document : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                ConvertAndLoadDocumentInEditor();
            }
        }

        private void ConvertAndLoadDocumentInEditor()
        {
            //To save every file with different name
            string randamName = DateTime.Now.ToFileTime().ToString();

            string relativePath = Server.MapPath("~") + "/_Temp/";

            //Complete path of the file.
            string FilePath = relativePath + randamName + flDocument.FileName;

            string GeneratedName = randamName + flDocument.FileName.Split('.')[flDocument.FileName.Split('.').Count() - 2] + ".html";

            flDocument.SaveAs(FilePath);

            //Converter functionality needs the file name to save as.
            string FileToSave = HttpContext.Current.Server.MapPath("~") + "_Temp\\" + GeneratedName;

            //Get the instance of IConverter interface
            IConverter doc = ConverterLocator.Converter(FilePath, FileToSave);

            //Call the Converter class and set th test of editor to converted excel.
            editor.Text = doc.Convert().ToString().Replace("�", "");
        }
    }
}

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
Software Developer (Senior) Nagarro Softwares
India India
I am vijay tanwar and i am a software engineer with passion of programming. I love to programming in c#, I love to warp up more and more things in few lines of code. my favirote languages are c# and javascript and both are fully object oriended. I always like to become the .net Architect.

Comments and Discussions