Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Wednesday, May 15, 2013

How to export DataTable to Excel using C#

In this post we will be exporting the DataTable to an Excel using C#
Below is the method which will do the trick. Just pass the parameters datatable and the filename.

private void ExportDataTableToExcel(DataTable thetable, string fileName)
        {
            string attachment = "attachment; filename=" + fileName + ".xls";

            Response.ClearContent();

            Response.AddHeader("content-disposition", attachment);

            Response.ContentType = "application/vnd.ms-excel";

            //write columns from DataTable to Excel      
            string tab = "";
            foreach (DataColumn dc in thetable.Columns)
            {
                string title = string.Empty;
                if (dc.ColumnName.Contains("Meta"))
                    title = "Metadata Properties";
                else
                    title = dc.ColumnName;
                Response.Write(tab + title);
                tab = "\t";
            }
            Response.Write("\n");

            //write rows from DataTable to Excel  
            int i;
            foreach (DataRow dr in thetable.Rows)
            {
                tab = "";
                for (i = 0; i < thetable.Columns.Count; i++)
                {
                    Response.Write(tab + dr[i].ToString());
                    tab = "\t";
                }
                Response.Write("\n");
            }
            //To throw the excel file generated
            Response.End();

        }


Hope this helps...

Tuesday, September 27, 2011

How to programmatically send an email in SharePoint using C#


In this post I will discuss about Sending an e-mail in SharePoint 2010 using C#

public static bool SendMail(string Subject, string Body, bool IsBodyHtml, string From, string To, string Cc, string Bcc)
        {
            bool mailSent = false;
            try
            {
                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Host = SPContext.Current.Site.WebApplication.
                OutboundMailServiceInstance.Server.Address;
                MailMessage mailMessage = new MailMessage(From, To, Subject, Body);
                if (!String.IsNullOrEmpty(Cc))
                {
                    MailAddress CCAddress = new MailAddress(Cc);
                    mailMessage.CC.Add(CCAddress);
                }
                if (!String.IsNullOrEmpty(Bcc))
                {
                    MailAddress BCCAddress = new MailAddress(Bcc);
                    mailMessage.Bcc.Add(BCCAddress);
                }
                mailMessage.IsBodyHtml = IsBodyHtml;
                smtpClient.Send(mailMessage);
                mailSent = true;
            }
            catch (Exception) { return mailSent; }
            return mailSent;
        }

Hope this helps..
Next Post- Programatically set the welcome page in Sharepoint

Popular Posts