excel 匯出
1.using System.Web.UI;
2.
public override void VerifyRenderingInServerForm(Control control)
{
//'XX'型別 必須置於有 runat=server 的表單標記之中
}
3.
匯出一 :grid view
protected void btnExport_Click(object sender, EventArgs e)
{
string filename = "satisfyQry_" + DateTime.Now.ToString("yyyyMMdd-HHmm") + ".xls";
Response.Clear();
Response.Charset = "utf-8";
Response.ContentType = "application/vnd.ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.Write("");
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
gvList.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
4.匯出二
protected void btnExport_Click(object sender, EventArgs e)
{
string filename = "satisfyQry_" + DateTime.Now.ToString("yyyyMMdd-HHmm") + ".xls";
Response.Clear();
Response.Charset = "utf-8";
Response.AddHeader(
"Content-Disposition",
"attachment;filename=" + Server.UrlEncode(filename)
);
Response.ContentType = "application/vnd.ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.Write("");
Response.Write("");
Response.Write("");
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter tw = new System.Web.UI.HtmlTextWriter(sw);
#region
header
sw.WriteLine(@"
滿意 |
普通 |
不滿意 |
很不滿意 |
"); #endregion
#region
detail
for (int i = 0; i < gvList.Rows.Count; i++)
{
sw.WriteLine("
" + gvList.Rows[i].Cells[0].ToString() |
gvList.Rows[i].Cells.
+ "
" + gvList.Rows[i]["isi_icusname"].ToString() | + "
" + gvList.Rows[i]["isi_icuscode"].ToString() | + "
" + gvListdt.Rows[i]["isi_prerevenue"].ToString() | + "
" + gvListdt.Rows[i]["isi_equname"].ToString() | + "
" + gvListdt.Rows[i]["Status"].ToString() | + "
" + gvListdt.Rows[i]["pos_invno"].ToString() |
+ "
"); }
#endregion
sw.WriteLine("
"); Response.Write("
Response.Write(sw.ToString());
Response.Write("
"); Response.Write("
");
Response.End();
}