gridview导出之后身份证号是科学计数法,显示之后数据最后几位全是0了。找了半天,改不对啊!!
private void Export(string FileName)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString() + ".xls");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void printout_Click(object sender, EventArgs e)
{
Export("学生报名信息");
}
}
private void Export(string FileName)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString() + ".xls");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void printout_Click(object sender, EventArgs e)
{
Export("学生报名信息");
}
}
解决方案 »
- 各位大侠、牛人们!请教个关于gridview的问题
- 关于treeview和frame的简单问题
- 最后50分,请问如何按时间排列????????
- ASP.NET + VB.NET 中全局变量问题
- 如何响应服务器按钮事件后退一个页面?
- 请问哪位知道asp.net 2.0的menu控件可不可以设置为TreeView 的右键菜单?
- 在.NET 中 又是一个编码问题?????????
- 初次用WebService 请大家指教;WebService已经作好,也已经Web引用了,现在是如何调用WebService.asmx的方法;谢谢了!最好是有例子或步
- 怎样应用继承类:
- Cookie是什么(定义)?怎么用?例子?具体应用方法?与session的区别?
- 设了一个检查按钮,点击后网页就变形了,怎么回事儿
- 文件下载,中文文件名过长被IE截取,如何解决啊
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
e.Row.Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
e.Row.Cells[4].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
e.Row.Cells[5].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
e.Row.Cells[6].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
e.Row.Cells[7].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
e.Row.Cells[8].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#e0ffff'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
}
}
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//找到身份证是第几列,比如第一列.只需将其格式化为字符即可。
e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
}
}
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="btnToExcel" runat="server" OnClick="btnToExcel_Click" Text="导出到Excel" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="xm" HeaderText="姓名" />
<asp:BoundField DataField="xb" HeaderText="性别" />
<asp:BoundField DataField="sfzh" HeaderText="身份证号" />
<asp:BoundField DataField="zxmc" HeaderText="高中地址" />
<asp:BoundField DataField="jtzz" HeaderText="家庭住址"/>
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</form>
</body>
</html>后台
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Text;public partial class testdc : System.Web.UI.Page
{
DB con = new DB();
protected void Page_Load(object sender, EventArgs e)
{
bind_GridView();
}
protected void bind_GridView()
{
string sql = "select * from baoming";
con.DBopen();
DataSet myds = con.creatdataset(sql, "xm");
GridView1.DataSource = myds;
GridView1.DataBind();
con.DBclose();
}
private void Export(string FileName)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString() + ".xls");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void btnToExcel_Click(object sender, EventArgs e)
{
Export("学生报名信息");
}
}
或
string style = @"<style> .text { mso-number-format:\@; } </script> ";
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Write(style); Response.Write(sw.ToString());
Response.End();
public override void VerifyRenderingInServerForm(Control control){}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//你的身份证是第3列,只需将其格式化为字符即可。
e.Row.Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//你的身份证是第3列,只需将其格式化为字符即可。
e.Row.Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
}
}
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].Cells[2].Text.Length >= 15)
GridView1.Rows[i].Cells[2].Text = "[" + GridView1.Rows[i].Cells[2].Text.Trim() + "]";
//GridView1.Rows[i].Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
}
......
}
问题解决了,谢谢大家!我对身份证号列进行了处理。
'<%#" " + Eval("id_card").ToString() %>'
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//你的身份证是第3列,只需将其格式化为字符即可。
e.Row.Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
}
}