Print dynamic GridView content
<p>Ok. I've made this awe-crap-code Exporting table to <strong>XLS</strong>
(but still no idea <strong>how to make it editable (not read only )</strong>)</p>

<p>The task for now is <strong>printing</strong> data from GridView. I can send it to HTML format (like on XLS) <strong>but</strong> with <strong>no lines</strong> and download file for print is weird. </p>

<p><strong>How can I make some "Print-View" for my GridView (with lines... ) and Print it then ?</strong></p>

<p>Is it possible on web site ? without download.</p>

<p>by the way here is how I do export ... (maybe I'll get some advances ^_ )</p>

<pre><code>    using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;

public class GridViewExportUtil
{
    static HSSFWorkbook hssfworkbook;

    static MemoryStream WriteToStream()
    {
        //Write the stream data of workbook to the root directory
        MemoryStream file = new MemoryStream();
        hssfworkbook.Write(file);
        return file;
    }

    static void InitializeWorkbook()
    {
        hssfworkbook = new HSSFWorkbook();

        ////create a entry of DocumentSummaryInformation
        DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
        dsi.Company = "";
        hssfworkbook.DocumentSummaryInformation = dsi;

        ////create a entry of SummaryInformation
        SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
        si.Subject = "";
        hssfworkbook.SummaryInformation = si;
    }
    /// &lt;param name="fileName"&gt;&lt;/param&gt;
    /// &lt;param name="gv"&gt;&lt;/param&gt;
    public static void Export(string fileName, GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.Charset = System.Text.Encoding.Unicode.EncodingName;
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
        HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
        //HttpContext.Current.Response.ContentType = "application/ms-excel";
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; // NPOI
        HttpContext.Current.Response.AddHeader(
             "content-disposition", string.Format(
                "attachment; filename=Report.xls"));//, fileName)); // Need .XLS file
        HttpContext.Current.Response.Clear();

        InitializeWorkbook();

        HSSFSheet sheet1 = hssfworkbook.CreateSheet("Таблица");
        //sheet1.CreateRow(0).CreateCell(0).SetCellValue("Таблица");

        using (StringWriter sw = new StringWriter())
        {
                //  Create a form to contain the grid
                Table table = new Table();
                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);
                }
                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    GridViewExportUtil.PrepareControlForExport(row);
                    table.Rows.Add(row);
                }
                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }

                sheet1.DisplayGridlines = true;

                HSSFCellStyle style1 = hssfworkbook.CreateCellStyle();
                style1.Alignment = HSSFCellStyle.ALIGN_CENTER;

                sheet1.SetColumnWidth(0, 10000);
                sheet1.SetColumnWidth(1, 5000);
                sheet1.VerticallyCenter = true;

                for (int j = 2; j &lt; table.Rows[0].Cells.Count; j++)
                {
                    sheet1.SetColumnWidth(j, 4000);
                    sheet1.SetDefaultColumnStyle(short.Parse(j.ToString()), style1);
                }

                double Temp=0;
                for(int i=0; i&lt;(table.Rows.Count-1); i++)
                {

                    for (int j = 0; j &lt; table.Rows[i].Cells.Count;j++)
                    {
                        if (i != 0 &amp;&amp; j != 0)
                        {
                            if (double.TryParse(table.Rows[i].Cells[j].Text, out Temp))
                            {
                                sheet1.CreateRow(i).CreateCell(j).SetCellValue(Temp.ToString());
                            }
                        }
                        else
                        {
                            sheet1.CreateRow(i).CreateCell(j).SetCellValue(table.Rows[i].Cells[j].Text);
                        }
                    }
                }
                HttpContext.Current.Response.BinaryWrite(WriteToStream().GetBuffer());
                HttpContext.Current.Response.End();
        }
    }

    /// &lt;summary&gt;
    /// Replace any of the contained controls with literals
    /// &lt;/summary&gt;
    /// &lt;param name="control"&gt;&lt;/param&gt;
    private static void PrepareControlForExport(Control control)
    {
        for (int i = 0; i &lt; control.Controls.Count; i++)
        {
            Control current = control.Controls[i];
            if (current is LinkButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
            }
            else if (current is ImageButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
            }
            else if (current is HyperLink)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
            }
            else if (current is DropDownList)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
            }
            else if (current is CheckBox)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
            }
            if (current.HasControls())
            {
                GridViewExportUtil.PrepareControlForExport(current);
            }
        }
    }
}
</code></pre>
