SIGN UP MEMBER LOGIN:    
ARTICLE

Creating Excel/Word/HTML Document Using C#

Posted by Sanjay Articles | XML in C# February 24, 2009
This article describes a simple approach to create Excel/Word/HTML document using C#, ADO.Net dataset without using interop/excel assemblies reference.
Reader Level:
Download Files:
 

This article describes a simple approach to create Excel/Word/HTML document using C#, ADO.Net dataset. Codes internally generate a XML document and XSL document and then transform it in well formatted HTML document. Since both Excel and Word supports HTML thus we can create these documents from HTML source. We can also change appearance of data using CSS.



The CreateDocument function has four parameters i.e. path of the file, name of the file, dataset object, and the type of file to be created:

XmlXslToExcel.CreateDocument(filePath, "test", ds, XmlXslToExcel.DocumentType.Excel);

CreateDocument function internally call CreateXMLDocument() and CreateXSLDocument() and then transform the XML document using XSL document and creates specified file.

public static bool CreateDocument(string filePath, string fileName, DataSet ds, DocumentType docType)
{
            XmlDocument xmlDoc = null;
            XslCompiledTransform xslTran = null ;
            FileStream excelFileStream = null;
            bool isFileCreated;
            try
            {
                if (ds == null || ds.Tables.Count == 0) return false;
                xmlDoc = CreateXmlDocument(ds);
                xslTran = CreateXslDocument(ds);
                if (xmlDoc == null || xslTran == null) return false;
                // Append xls extention in file name to create an excel                                            

    file (we can also use doc extention to create word or html document file)
                switch (docType)
                {
                    case DocumentType.Excel:
                        filePath = filePath + "\\Excel\\" + fileName + ".xls";
                        break;
                    case DocumentType.Word:
                        filePath = filePath + "\\Word\\" + fileName + ".doc";
                        break;
                    case DocumentType.HTML:
                        filePath = filePath + "\\HTML\\" + fileName + ".htm";
                        break;
                    default:
                        filePath = filePath + "\\HTML\\" + fileName + ".htm";
                        break;
                }

                // Create FileStream object with file mode as Create
                excelFileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create);

                // Create XmlTextWriter object for the FileStream            
                System.Xml.XmlTextWriter xtw = new System.Xml.XmlTextWriter(excelFileStream, System.Text.Encoding.Unicode);

                // Now transform xml document into specified stream
                // Or we can also specify the output file which is to be created by transformation
                xslTran.Transform(xmlDoc, null, xtw);
                //xslTran.Transform(xmlDoc, fileName);
                xtw.Flush();
                excelFileStream.Flush();
                xtw.Close();
                excelFileStream.Close();
                xtw = null;
                excelFileStream = null;
                // check for file existance
                isFileCreated = File.Exists(filePath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (excelFileStream != null)
                    excelFileStream.Close();
                excelFileStream = null;
                xmlDoc = null;
                xslTran = null;
            }
            return isFileCreated;
}


Excel limitations:

Following are few limitations of this approach:

  1. This approach will not work where you want to create multiple sheets from the available data.
  2. There is also a limitation of number of records per sheet ( you can populate only 65536 rows per sheet)

Login to add your contents and source code to this article
share this article :
post comment
 

Like the methods/classes for CreateXmlDocument and CreateXslDocument. Without them, the article is of little (no) value. With them, the article would be very useful.

Posted by raggedy andy Aug 21, 2011

sir its a good effort,please tell me how can i create spreadsheet in asp.net

Posted by saifullah khan Jan 20, 2011

i want to create a ribbon in c#.net,i dont know how to start also,so pls tel me procedure to create a ribbon in c#,including namespaces also.....pls help me..... thank u

Posted by kishore kumar Jan 19, 2011

100/100

Posted by saifullah khan Nov 03, 2010

Thanks a lot for sharing.

Posted by Roahn Luo Jun 11, 2009
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor