首页 > 代码库 > [.Net] 导出Excel中身份证等数字串的解决方式
[.Net] 导出Excel中身份证等数字串的解决方式
public static void DataTableToExcel(System.Data.DataTable dtData, String FileName) { GridView dgExport = null; HttpContext curContext = HttpContext.Current; StringWriter strWriter = null; HtmlTextWriter htmlWriter = null; if (dtData != null) { HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8); curContext.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls"); curContext.Response.ContentType = "application/vnd.ms-excel"; curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7"); curContext.Response.Charset = "GB2312"; strWriter = new StringWriter(); htmlWriter = new HtmlTextWriter(strWriter); dgExport = new GridView(); dgExport.RowDataBound += (GridViewFormat); //在GridView绑定数据时,将数据格式化 dgExport.DataSource = dtData.DefaultView; dgExport.AllowPaging = false; dgExport.DataBind(); dgExport.RenderControl(htmlWriter); curContext.Response.Write(strWriter.ToString()); curContext.Response.End(); }} protected static void GridViewFormat(object sender, GridViewRowEventArgs e) { //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% for (int i = 0; i < e.Row.Cells.Count; i++) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@"); } } }
[.Net] 导出Excel中身份证等数字串的解决方式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。