.Net中导出Excel中身份证等数字串的解决方式
2013-05-29 15:37
148 查看
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:@");
}
}
}
{
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中身份证等数字串的解决方式
- 用POI导出excel时,较长的数字不想被自动变为科学计数法的解决方式
- 用POI导出excel时,较长的数字不想被自动变为科学计数法的解决方式(转)
- 用POI导出excel时,较长的数字不想被自动变为科学计数法的解决方式
- 用POI导出excel时,较长的数字不想被自动变为科学计数法的解决方式
- [.Net] Excel导入导出各种方式分析
- jsp 页面导出excel时字符串数字变成科学计数法的解决方法
- php导出数据到excel,身份证等数字常用格式
- php导出数据到excel,防止身份证等数字字符格式变成科学计数的方-------缺点,必须使用table tr td
- 使用poi做excel导出时解决以文本格式存储的数字问题
- c# 导出excel 数字太长而显示为科学计数法的解决方法
- php excel导出数字字符串变成科学技术法导致精度变化的解决方法
- 使用poi做excel导出时解决以文本格式存储的数字问题
- POI 导出excel带小数点的数字格式显示不对解决方法
- 导出Excel时,解决数字前的0问题
- .net导出excel无表格线解决办法(转载)
- .NET导出Excel遇到的80070005错误的解决
- 解决:导出excel身份证号码显示为科学计数法
- 解决asp.net导出Excel报表时字符转成了数字科学计数法格式问题
- C#导出EXCEL超大数据源码,解决内存溢出问题,使用OLEDB方式(NPOI做不到)