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();
}
}
精彩评论