0

I have a grid-view and I exported to grid-view data to word as shown below. The grid-view data and style works as I expected. Now I want to and new header line to word document and than send my grid-view data under it. I have to add heading to word document before Response.Output.Write(sw.ToString()); line. Any help please.

Kind regards,

protected void ExportToExcel(object sender, EventArgs e)
    {
        string nowTarih = DateTime.Now.ToString("yyyy-MM-dd");
        string excelNameExport = "attachment;filename=" + nowTarih + "_LT_Raporu.doc";
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", excelNameExport);
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
        Response.ContentType = "application/vnd.ms-word";

        using (StringWriter sw = new StringWriter())
        {
            HtmlTextWriter hw = new HtmlTextWriter(sw);

            //To Export all pages
            mygrid.AllowPaging = false;
            this.gvBind();

            if (mygrid.Rows.Count > 0)
            {
                 mygrid.Height = new Unit(mygrid.RowStyle.Height.Value * mygrid.Rows.Count);
            }

            mygrid.DataBind();
            mygrid.RenderControl(hw);

            //style to format numbers to string
            string style = @"<style> .textmode { } </style>";
            Response.Write(style);
            **Response.Output.Write(sw.ToString());**
            Response.Flush();
            Response.End();
        }
    }
NTMS
  • 816
  • 7
  • 22

1 Answers1

0

I found it.

            HtmlTextWriter hw = new HtmlTextWriter(sw);

            hw.Write("<div> <h3 align=center><span style=");
            hw.Write(HtmlTextWriter.DoubleQuoteChar);
            hw.Write("font-weight:bold; font-family:'Segoe UI'; color: #81040a;");
            hw.Write(HtmlTextWriter.DoubleQuoteChar);
            hw.Write(">LT Control</span> </h3></div>");
            hw.WriteLine();
NTMS
  • 816
  • 7
  • 22