Click here to Skip to main content
16,021,125 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Linkbutton.It call the Read Class..that class writen in console application..

When User click a link button then "File Download" dialog box will appear, if user click ok then xlsx(Template.xlsx) file open and it have column headers are list items.

My Code:


XML
Read.cs(Console Application)
{
List<string> list = new List<string>();

            list.Add("Name");
            list.Add("Designation");
            list.Add("Dob");
            list.Add("Address");
            Response.AddHeader("content-disposition", "attachment;filename=Template.xlsx");
            Response.Charset = "";
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}




But this content dype does not open the xlsx file It raise the error....and I want to set the column header as Name,Designation,Dob,Address..Pls help


Thanks in advance
Posted
Updated 30-Oct-12 22:46pm
v2

1 solution

I have solved this myself...

My code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OfficeOpenXml;


namespace Downloadxlsx
{
    public partial class ReadExcel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void lkl_Click(object sender, EventArgs e)
        {
            using (ExcelPackage pck = new ExcelPackage())
            {

                
                List<string> list = new List<string>();

                list.Add("Name");
                list.Add("Designation");
                list.Add("Dob");
                list.Add("Address");

                ExcelWorksheet ws = pck.Workbook.Worksheets.Add("SearchReport");
                int col = 1;
                for (int i = 0; i < list.Count; i++) // Loop through List with for  
                {

                    ws.Cells[1, col].Value = list[i].ToString();
                    col++;

                }

                Byte[] fileBytes = pck.GetAsByteArray();
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.Cookies.Clear();
                              Response.Cache.SetCacheability(HttpCacheability.Private);
                Response.CacheControl = "private";
                Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
                Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
                Response.AppendHeader("Content-Length", fileBytes.Length.ToString());
                Response.AppendHeader("Pragma", "cache");
                Response.AppendHeader("Expires", "60");
                Response.AppendHeader("Content-Disposition",
                "attachment; " +
                "filename=\"ExcelReport.xlsx\"; " +
                "size=" + fileBytes.Length.ToString() + "; " +
                "creation-date=" + DateTime.Now.ToString("R") + "; " +
                "modification-date=" + DateTime.Now.ToString("R") + "; " +
                "read-date=" + DateTime.Now.ToString("R"));
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                       Response.BinaryWrite(fileBytes);
                Response.End();
            }
        }
    }
}</string></string>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900