C# 导出 excel 模板

<asp:Button ID="Button1" runat="server" Text="导出PWHT模板" OnClick="btnExport_PWHT_Click" >
</asp:Button>

protected void btnExport_PWHT_Click(object sender, EventArgs e)
{

string path = Server.MapPath("../z_imes_weldlist/导入热处理报告号模板.xlsx");
FileInfo fi = new FileInfo(path);//excelFile为文件在服务器上的地址
if (File.Exists(path))
{
Response.Clear();
Response.Buffer = true; //设置<span style="color: rgb(51, 51, 51); line-height: 24px; white-space: pre-wrap;">输出页面是否被缓冲</span>
Response.Charset = "GB2312"; //设置了类型为中文防止乱码的出现
Response.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", HttpUtility.UrlEncode("导入热处理报告号模板.xls"))); //定义输出文件和文件名
Response.AppendHeader("Content-Length", fi.Length.ToString());
Response.ContentEncoding = Encoding.Default;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
Response.WriteFile(fi.FullName);
Response.Flush();
Response.End();
}
else
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "<script>alert('此模板不存在!');</script>", false);
}

源地址:https://blog.csdn.net/qq_34496400/article/details/51353267

原文地址:https://www.cnblogs.com/codejimmygao/p/14857878.html