python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
Format cells column text in excel using c#.net

Format cells column text in excel using c#.net

Hello everyone, I am going to share the code sample for formatting to an excel cells column using c#

Table of content
  1. Connection String for connect DB to bond and display excels.
  2. Example code for format cells column text in excel.

Connection String

string constr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();

Example code for format cells text in excel

protected void BtnFormatCellsInExcel_Click1(object sender, EventArgs e)
{
        DataTable dt = new DataTable();
        SqlDataReader dataReader;

        SqlConnection conn = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand("select * from dbo.TableName", conn);

        conn.Open();

        dataReader = cmd.ExecuteReader();
        dt.Load(dataReader);

        conn.Close();

        GrvFormatCellsInExcel.DataSource = dt;
        GrvFormatCellsInExcel.DataBind();

        Response.Clear();
        Response.Buffer = true;

        Response.AddHeader("content-disposition","attachment;filename=GridViewExport.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";

        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

        GrvFormatCellsInExcel.AllowPaging = false;
        GrvFormatCellsInExcel.DataBind();
       
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GrvFormatCellsInExcel.Rows[i];
            row.BackColor = System.Drawing.Color.Red;
            row.Attributes.Add("Anyclass", "Anytextmode");
        }
        GrvFormatCellsInExcel.RenderControl(htmlTextWriter);
        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        Response.Write(style);
        Response.Output.Write(stringWriter.ToString());
        Response.Flush();
        Response.End();
    }



ANIL SINGH

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions