asp.net导出excel及科学计数问题
转自:http://www.cnblogs.com/legend_sun/archive/2009/02/17/1392493.html
导出最简单方法:
const string style = @"<style> .text { mso-number-format:\@; } </style> ";
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + Server.UrlEncode(Name) + ".xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
//htw.WriteLine(Name);
System.Web.UI.LiteralControl lt = new LiteralControl();
lt.Text = "<div style=\"width:100%;font-bold:true;text-align:center;\">" +Name + "</div>";
if(Name!="")lt.RenderControl(htw);
gvdata.RenderControl(htw);
// Style is added dynamically
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + Server.UrlEncode(Name) + ".xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
//htw.WriteLine(Name);
System.Web.UI.LiteralControl lt = new LiteralControl();
lt.Text = "<div style=\"width:100%;font-bold:true;text-align:center;\">" +Name + "</div>";
if(Name!="")lt.RenderControl(htw);
gvdata.RenderControl(htw);
// Style is added dynamically
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
导出出现科学计数解决办法一:
GridView gvdata = new GridView();
gvdata.HeaderStyle.BackColor = System.Drawing.Color.Silver;
gvdata.HeaderStyle.Font.Bold = true;
gvdata.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
gvdata.RowDataBound += new GridViewRowEventHandler(gvdataRowDataBound);
gvdata.DataSource = dtData;
gvdata.DataBind();
const string style = @"<style> .text {mso-number-format:\@; } </style> ";
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + Server.UrlEncode(strFileName) + ".xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
//htw.WriteLine(Name);
System.Web.UI.LiteralControl lt = new LiteralControl();
lt.Text = "<div style=\"width:100%;font-bold:true;text-align:center;\">" + strFileName + "</div>";
if (strFileName != "") lt.RenderControl(htw);
gvdata.RenderControl(htw);
// Style is added dynamically
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
public void gvdataRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
e.Row.Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
}
}
gvdata.HeaderStyle.BackColor = System.Drawing.Color.Silver;
gvdata.HeaderStyle.Font.Bold = true;
gvdata.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
gvdata.RowDataBound += new GridViewRowEventHandler(gvdataRowDataBound);
gvdata.DataSource = dtData;
gvdata.DataBind();
const string style = @"<style> .text {mso-number-format:\@; } </style> ";
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + Server.UrlEncode(strFileName) + ".xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
//htw.WriteLine(Name);
System.Web.UI.LiteralControl lt = new LiteralControl();
lt.Text = "<div style=\"width:100%;font-bold:true;text-align:center;\">" + strFileName + "</div>";
if (strFileName != "") lt.RenderControl(htw);
gvdata.RenderControl(htw);
// Style is added dynamically
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
public void gvdataRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
e.Row.Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
}
}
另注: //1) 文本:vnd.ms-excel.numberformat:@
//2) 日期:vnd.ms-excel.numberformat:yyyy/mm/dd
//3) 数字:vnd.ms-excel.numberformat:#,##0.00
//4) 货币:vnd.ms-excel.numberformat:¥#,##0.00
//5) 百分比:vnd.ms-excel.numberformat: #0.00%
//2) 日期:vnd.ms-excel.numberformat:yyyy/mm/dd
//3) 数字:vnd.ms-excel.numberformat:#,##0.00
//4) 货币:vnd.ms-excel.numberformat:¥#,##0.00
//5) 百分比:vnd.ms-excel.numberformat: #0.00%