[.CS]
#region 匯出Excel
public override void VerifyRenderingInServerForm(Control control)
{
//處理'GridView' 的控制項 'GridView' 必須置於有 runat=server 的表單標記之中
}
protected void btnExcel_Click(object sender, EventArgs e)
{
gvw_Execl.Visible = true;
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode("Lettrt.xls"));
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
Response.Write("<style type=text/css>");
Response.Write("td{mso-number-format:\"\\@\";}"); //將所有欄位格式改為"文字"
Response.Write("</style>");
//關閉ViewState
EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter(sw);
//關閉換頁跟排序
gvw_Execl.AllowSorting = false;
gvw_Execl.AllowPaging = false;
//取的Control的HTML
gvw_Execl.RenderControl(htw);
//把HTML寫回Browser
Response.Write(sw.ToString());
Response.End();
gvw_Execl.Visible = false;
}
#endregion
如果依以上寫法中文一樣會出亂碼,請加上
//避免中文會出亂碼
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");
--串HTML匯出Excel
string filename = "Letter";
string html = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=big5\">"
+ "<title>Letter</title></head>";
html += "<table border=\"1\">";
//表頭
html += "<tr>";
for (int i = 0; i < gvw_Execl.Columns.Count; i++)
{
if (!gvw_Execl.Columns[i].Visible)
{
continue;
}
html += "<td bgcolor=\"#E4E4E4\">" + gvw_Execl.HeaderRow.Cells[i].Text;
}
//先把分頁關掉
gvw_Execl.AllowPaging = false;
//內容
for (int i = 0; i < gvw_Execl.Rows.Count; i++)
{
html += "<tr>";
for (int c = 0; c < gvw_Execl.Rows[i].Cells.Count; c++)
{
if (gvw_Execl.Rows[i].Cells[c].Controls.Count > 0)
{
html += "<td>" + ((Literal)gvw_Execl.Rows[i].Cells[c].Controls[1]).Text + "</td>";
//html += "<td>" + gvw_Execl.Rows[i].Cells[c].Controls[1].ToString() + "</td>";
}
else
{
html += "<td>" + gvw_Execl.Rows[i].Cells[c].Text + "</td>";
}
}
html += "</tr>";
}
html += "</table>";
string sContentDisposition = "";
sContentDisposition += "attachment; "; // 強制存檔,未設定則依瀏覽器預設開啟或存檔
sContentDisposition += "filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls"; //設定檔名可為中文_#1
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-disposition", sContentDisposition);
HttpContext.Current.Response.ContentType = "application/vnd.ms-excle";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
//utt8 to ascii
Byte[] bytes = System.Text.Encoding.Unicode.GetBytes(html);
Byte[] outByte = System.Text.Encoding.Convert(System.Text.Encoding.Unicode, System.Text.Encoding.Default, bytes);
HttpContext.Current.Response.BinaryWrite(outByte);
HttpContext.Current.Response.End();
參考連結
簡易匯出Excel
欄位格式設定
Excel 格式
中文亂碼解決方法
#region 匯出Excel
public override void VerifyRenderingInServerForm(Control control)
{
//處理'GridView' 的控制項 'GridView' 必須置於有 runat=server 的表單標記之中
}
protected void btnExcel_Click(object sender, EventArgs e)
{
gvw_Execl.Visible = true;
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode("Lettrt.xls"));
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
Response.Write("<style type=text/css>");
Response.Write("td{mso-number-format:\"\\@\";}"); //將所有欄位格式改為"文字"
Response.Write("</style>");
//關閉ViewState
EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter(sw);
//關閉換頁跟排序
gvw_Execl.AllowSorting = false;
gvw_Execl.AllowPaging = false;
//取的Control的HTML
gvw_Execl.RenderControl(htw);
//把HTML寫回Browser
Response.Write(sw.ToString());
Response.End();
gvw_Execl.Visible = false;
}
#endregion
如果依以上寫法中文一樣會出亂碼,請加上
//避免中文會出亂碼
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");
--串HTML匯出Excel
string filename = "Letter";
string html = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=big5\">"
+ "<title>Letter</title></head>";
html += "<table border=\"1\">";
//表頭
html += "<tr>";
for (int i = 0; i < gvw_Execl.Columns.Count; i++)
{
if (!gvw_Execl.Columns[i].Visible)
{
continue;
}
html += "<td bgcolor=\"#E4E4E4\">" + gvw_Execl.HeaderRow.Cells[i].Text;
}
//先把分頁關掉
gvw_Execl.AllowPaging = false;
//內容
for (int i = 0; i < gvw_Execl.Rows.Count; i++)
{
html += "<tr>";
for (int c = 0; c < gvw_Execl.Rows[i].Cells.Count; c++)
{
if (gvw_Execl.Rows[i].Cells[c].Controls.Count > 0)
{
html += "<td>" + ((Literal)gvw_Execl.Rows[i].Cells[c].Controls[1]).Text + "</td>";
//html += "<td>" + gvw_Execl.Rows[i].Cells[c].Controls[1].ToString() + "</td>";
}
else
{
html += "<td>" + gvw_Execl.Rows[i].Cells[c].Text + "</td>";
}
}
html += "</tr>";
}
html += "</table>";
string sContentDisposition = "";
sContentDisposition += "attachment; "; // 強制存檔,未設定則依瀏覽器預設開啟或存檔
sContentDisposition += "filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls"; //設定檔名可為中文_#1
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-disposition", sContentDisposition);
HttpContext.Current.Response.ContentType = "application/vnd.ms-excle";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
//utt8 to ascii
Byte[] bytes = System.Text.Encoding.Unicode.GetBytes(html);
Byte[] outByte = System.Text.Encoding.Convert(System.Text.Encoding.Unicode, System.Text.Encoding.Default, bytes);
HttpContext.Current.Response.BinaryWrite(outByte);
HttpContext.Current.Response.End();
參考連結
簡易匯出Excel
欄位格式設定
Excel 格式
中文亂碼解決方法
留言
張貼留言