asp.net导出EXCEL问题
<meta http-equiv="content-type" content="application/ms-excel; charset=GB2312"/>
<table border="0">
<tr style="background-color:#CEDFEF;">
<td style="width:80px;height:25px; text-align:center; font-weight:bold; border:solid 0.5pt black">序号</td><td style="width:80px;height:25px; text-align:center; font-weight:bold; border:solid 0.5pt black">日期</td><td style="width:80px;height:25px; text-align:center; font-weight:bold; border:solid 0.5pt black">机构名称</td><td style="width:80px;height:25px; text-align:center; font-weight:bold; border:solid 0.5pt black">经纪人</td><td style="width:150px;height:25px; text-align:center; font-weight:bold; border:solid 0.5pt black">物业地址</td><td style="width:80px;height:25px; text-align:center; font-weight:bold; border:solid 0.5pt black">建成年代</td><td style="width:80px;height:25px; text-align:center; font-weight:bold; border:solid 0.5pt black">面积</td><td style="width:80px;height:25px; text-align:center; font-weight:bold; border:solid 0.5pt black">楼层</td><td style="width:80px;height:25px; text-align:center; font-weight:bold; border:solid 0.5pt black">朝向</td><td style="width:100px;height:25px; text-align:center; font-weight:bold; border:solid 0.5pt black">初评价格</td><td style="width:100px;height:25px; text-align:center; font-weight:bold; border:solid 0.5pt black">登记用户</td>
</tr><tr>
<td style="height:20px;text-align:center;border:solid 0.5pt black; ">380978</td><td style="height:20px;text-align:center;border:solid 0.5pt black; ">2013-3-19 11:27:17</td><td style="height:20px;text-align:center;border:solid 0.5pt black; ">链家</td><td style="height:20px;text-align:center;border:solid 0.5pt black; "></td><td style="height:20px;text-align:center;border:solid 0.5pt black; ">金尚嘉园</td><td style="height:20px;text-align:center;border:solid 0.5pt black; "></td><td style="height:20px;text-align:center;border:solid 0.5pt black; ">76.5</td><td style="height:20px;text-align:center;border:solid 0.5pt black; ">9/</td><td style="height:20px;text-align:center;border:solid 0.5pt black; ">东</td><td style="height:20px;text-align:center;border:solid 0.5pt black; ">48560贷260/7</td><td style="height:20px;text-align:center;border:solid 0.5pt black; ">罗永</td>
</tr>
</table>
文件流输出的是以上字符串,但是没有下载提示!
代码:
HtmlTextWriter hw = new HtmlTextWriter(tw);
this.EnableViewState = false;
hw.WriteLine("<meta http-equiv="content-type" content="application/ms-excel; charset=GB2312"/>");
table.RenderControl(hw);
Response.AppendHeader("Content-Disposition", "Attachment;Filename=" + HttpUtility.UrlEncode("Attendance.xls", System.Text.Encoding.Default));
Response.ContentEncoding = System.Text.Encoding.Default;
Response.ContentType = "application/ms-excel";
Response.Charset = "GB2312";
Response.Write(tw.ToString());
Response.End(); excel asp.net
[解决办法]
Response.Write(tw.ToString());
?
Response.BinaryWrite(ms.ToArray());
[解决办法]
我也遇到这样的问题,正需要解决
[解决办法]
public void ExportExcel(string FileName, string tableContent)
{
byte[] byteArray = Encoding.GetEncoding("gb2312").GetBytes(tableContent);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearContent();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
string userAgent = System.Web.HttpContext.Current.Request.ServerVariables["http_user_agent"].ToLower();
if (userAgent.Contains("firefox"))
{
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
}
else
{
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpContext.Current.Server.UrlEncode(FileName));
}
System.Web.HttpContext.Current.Response.AddHeader("Content-Length", byteArray.Length.ToString());
System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";
System.Web.HttpContext.Current.Response.BinaryWrite(byteArray);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.Close();
System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
}
if (OutExcel)
{
//定义文档类型、字符编码
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
//下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
//filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc .xls .txt .htm
Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString() + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//Response.ContentType指定文件类型 可以为 application/ms-excel、application/ms-word、application/ms-txt、application /ms-html 或其他浏览器可直接支持文档
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
// 定义一个输入流
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.printContent.RenderControl(oHtmlTextWriter);
//this 表示输出本页,你也可以绑定datagrid,或其他支持 obj.RenderControl()属性的控件
Response.Write(oStringWriter.ToString());
Response.End();
// btn.Visible = true;
}