locked
heading to excel in code behind RRS feed

  • Question

  • User219039814 posted

    Response.ClearContent();
    Response.Charset = "";
    Response.ContentEncoding = System.Text.Encoding.Unicode;
    Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

    //Adds HTTP header to the output stream
    Response.AddHeader("content-disposition", string.Format("attachment; filename=BatchWisePaymentDetails.xls"));

    // Gets or sets the HTTP MIME type of the output stream
    Response.ContentType = "application/vnd.ms-excel";
    string space = "";
    Response.Write("\n");
    Response.Write("\n");
    space = "\t";
    space = "\t";


    foreach (DataColumn dcolumn in downloadreporttable.Columns)
    {


    Response.Write(space + dcolumn.ColumnName);
    space = "\t";
    }
    Response.Write("\n"); space = "\t";
    space = "\t";
    int countcolumn;
    foreach (DataRow dr in downloadreporttable.Rows)
    {
    space = "\t";
    for (countcolumn = 0; countcolumn < downloadreporttable.Columns.Count; countcolumn++)
    {

    Response.Write(space + dr[countcolumn].ToString());
    space = "\t";

    }

    Response.Write("\n");
    }
    Response.End();

    How can i add a heading to this excel import

    Saturday, May 26, 2018 11:04 AM

Answers

  • User219039814 posted
    Thank you.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 26, 2018 4:12 PM

All replies

  • User753101303 posted

    Hi,

    Not sure to get which kind of "heading" you are talking about. You want a title on the first row before column names?

    You won't get much flexibility here. Also AFAIK it shouldn't work with recent Excel version (they are checking if the file extension matches the actual content ie it might show a message as you pretend a TSV file to be an actual XLS file). Finally if I remember it is confusing for user (they'll have a message when changing the file with something that can't be saved back to a TSV).

    Another approach is to use for example https://msdn.microsoft.com/en-us/library/office/ff478153.aspx that would allow to create true XLSX files...

    Saturday, May 26, 2018 2:34 PM
  • User219039814 posted
    Yes i meant a title..
    Saturday, May 26, 2018 3:16 PM
  • User753101303 posted

    Seems I don't get it? You mean Response.Write("Some Title Here\n"); which would show this on the first row of the Excel file and then write column names and data is not enough? The problem is that you need some formatting or you mean a page header or a worksheet name ? For those 3 cases, a TSV won't fit and you'll have to create a true Excel file.

    Saturday, May 26, 2018 3:51 PM
  • User219039814 posted
    Thank you.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 26, 2018 4:12 PM