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

↘锁芯ラ 提交于 2019-12-11 08:58:47

问题


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);
    Response.AppendHeader("content-disposition", "attachment; filename=" + name);
    Response.ContentType = "Application/msword";
    Response.WriteFile(path);
    Response.End();
}

回答1:


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();
        }
    }


来源:https://stackoverflow.com/questions/7515018/how-to-download-data-from-an-asp-net-webpage-to-an-excel-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!