开发者

How to download data from an ASP.Net webpage to an Excel file?

开发者 https://www.devze.com 2023-04-06 21:54 出处:网络
I need to download the data in a table and also a picture to an excel spreadsheet. I am currently able to create the excel spreadsheet on the website, but how do I get the data into the spreadsheet?

I need to download the data in a table and also a picture to an excel spreadsheet. I am currently able to create the excel spreadsheet on the website, but how do I get the data into the spreadsheet?

protected void btn_ExcelDownload_Click(object sender, EventArgs e)
{
    string path = Server.MapPath("");
    path = path + @"\Resources\MessageStatus.xlsx";
    string name = Path.GetFileName(path);
  开发者_StackOverflow社区  Response.AppendHeader("content-disposition", "attachment; filename=" + name);
    Response.ContentType = "Application/msword";
    Response.WriteFile(path);
    Response.End();
}


I had done this using this class

void WriteToXls(string fromfilePath, string targetFileName)
    {
        if (!String.IsNullOrEmpty(fromfilePath)) 
        {
            HttpResponse response = HttpContext.Current.Response;
            response.Clear();
            response.Charset = "utf-8";
            response.ContentType = "text/xls";
            response.AddHeader("content-disposition", string.Format("attachment; filename={0}", targetFileName));
            response.BinaryWrite(File.ReadAllBytes(fromfilePath));
            response.End();
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号