Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 672211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:27:33+00:00 2026-05-14T00:27:33+00:00

Ok. I’ve made this awe-crap-code Exporting table to XLS (but still no idea how

  • 0

Ok. I’ve made this awe-crap-code Exporting table to XLS
(but still no idea how to make it editable (not read only ))

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

How can I make some “Print-View” for my GridView (with lines… ) and Print it then ?

Is it possible on web site ? without download.

by the way here is how I do export … (maybe I’ll get some advances ^_ )

    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;
    }
    /// <param name="fileName"></param>
    /// <param name="gv"></param>
    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 < 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<(table.Rows.Count-1); i++)
                {

                    for (int j = 0; j < table.Rows[i].Cells.Count;j++)
                    {
                        if (i != 0 && 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();
        }
    }

    /// <summary>
    /// Replace any of the contained controls with literals
    /// </summary>
    /// <param name="control"></param>
    private static void PrepareControlForExport(Control control)
    {
        for (int i = 0; i < 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);
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:27:34+00:00Added an answer on May 14, 2026 at 12:27 am

    What you can do is call the browser’s “PRINT” function in javascript to print your page. Which means everything on your page not only the gridview.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.