-3
string strFileName = "LoginData.xls";    

ds.Tables[0].TableName = "LoginUserData";

var stringXml = ExcelHelper.GetExcelXml(ds, strFileName);

Response.Clear();

Response.AppendHeader("content-Type", "application/vnt.ms-excel");

Response.AppendHeader("content-disposition", "attachment; filename=" + strFileName);
string headerTable = @"<Table><tr><td>Report Header</td></tr><tr><td></Table>";
Response.Write(headerTable);
Response.Write(stringXml);

Response.Flush();
Response.End();
6
  • string headerTable = @"<Table><tr><td>Report Header</td></tr><tr><td></Table>"; Response.Write(headerTable); after adding these the dataset value is coming one line not in table format Commented Mar 31, 2017 at 7:39
  • 1
    How can you expect writing some HTML table tags to result in a valid Excel document? Commented Mar 31, 2017 at 7:42
  • Internet explorer allows you to export to excel when right-clicking on a table. Commented Mar 31, 2017 at 7:52
  • The Report i have is dynamic and and it come from database , which i have Stored in dataset .. from dataset But I not getting option add custom header in SQL .. Commented Mar 31, 2017 at 8:15
  • Possible duplicate of stackoverflow.com/a/9653194/1602237 Commented Mar 31, 2017 at 8:17

1 Answer 1

2

A better approach would probably be to use epplus and create the Excel document that way.

See this for the example below.

private void DumpExcel(DataTable tbl)
{
    using (ExcelPackage pck = new ExcelPackage())
    {
        //Create the worksheet
        ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");

        //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
        ws.Cells["A1"].LoadFromDataTable(tbl, true);

        //Format the header for column 1-3
        using (ExcelRange rng = ws.Cells["A1:C1"])
        {
            rng.Style.Font.Bold = true;
            rng.Style.Fill.PatternType = ExcelFillStyle.Solid;                      //Set Pattern for the background to Solid
            rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(79, 129, 189));  //Set color to dark blue
            rng.Style.Font.Color.SetColor(Color.White);
        }

        //Example how to Format Column 1 as numeric 
        using (ExcelRange col = ws.Cells[2, 1, 2 + tbl.Rows.Count, 1])
        {
            col.Style.Numberformat.Format = "#,##0.00";
            col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
        }

        //Write it back to the client
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;  filename=ExcelDemo.xlsx");
        Response.BinaryWrite(pck.GetAsByteArray());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.