2023年12月7日发(作者:)
C#DataTable导出EXCEL后身份证等信息显示乱码解决
在DataTable导出EXCEL后发现有些格式显示有问题,比如身份证号码等大于11位的数字显示为科学计数法、13681-1等 带中划线的两
段数字显示为日期格式等。
处理方法如下:
public static void DataTable2Excel(ble dtData)
{
id dgExport = null;
// 当前对话
ntext curContext = t;
// IO用于导出并返回excel文件
Writer strWriter = null;
xtWriter htmlWriter = null;
if (dtData != null)
{
// 设置编码和附件格式
tType = "application/-excel";
tEncoding = oding("gb2312");
t = "gb2312";
// 导出excel文件
strWriter = new Writer();
htmlWriter = new xtWriter(strWriter);
// 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid
dgExport = new id();
urce = tView;
aging = false;
taBound += new idItemEventHandler(dgExport_ItemDataBound);
nd();
// 返回客户端
Control(htmlWriter);
();
("" + ng());
();
}
}
protected static void dgExport_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (pe == || pe == atingItem)
{
foreach (TableCell cell in )
{
if (h((), @"^d{12,}$") || h((), @"^d+[-]d+$"))
{
("style", "format:@");
}
}
}
}
在DataGrid的行绑定事件给单元格加上样式就可以了。DataTable2Excel方法是网上找的,来源记不清了,行绑定事件的里的内容是自已写的。
发布评论