简体   繁体   English

如何将数据从ASP.Net网页下载到Excel文件?

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

I need to download the data in a table and also a picture to an excel spreadsheet. 我需要将数据下载到表格中,还需要将图片下载到Excel电子表格中。 I am currently able to create the excel spreadsheet on the website, but how do I get the data into the spreadsheet? 我目前可以在网站上创建excel电子表格,但是如何将数据导入电子表格?

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM