如何从一个ASP.Net网页数据下载到Excel文件? [英] How to download data from an ASP.Net webpage to an Excel file?
本文介绍了如何从一个ASP.Net网页数据下载到Excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个图片下载表中的数据,也为Excel US preadsheet。我目前能在网站上创建Excel US preadsheet,但我如何获取数据成S preadsheet?
保护无效btn_ExcelDownload_Click(对象发件人,EventArgs的发送)
{
字符串路径=使用Server.Mappath();
路径=路径+ @\\资源\\ MessageStatus.xlsx
字符串名称= Path.GetFileName(路径);
Response.AppendHeader(内容处置,附件;文件名=+姓名);
Response.ContentType =应用程序/ msword
Response.WriteFile(路径);
到Response.End();
}
解决方案
我做了这个使用这个类
无效WriteToXls(字符串fromfilePath,串targetFileName)
{
如果(!String.IsNullOrEmpty(fromfilePath))
{
HTT presponse响应= HttpContext.Current.Response;
response.Clear();
Response.Charset的=UTF-8;
response.ContentType =文/ XLS
response.AddHeader(内容处置的String.Format(附件;文件名= {0},targetFileName));
response.BinaryWrite(File.ReadAllBytes(fromfilePath));
到Response.End();
}
}
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();
}
解决方案
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();
}
}
这篇关于如何从一个ASP.Net网页数据下载到Excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文