DataTable转Excel并对0开头的数值保留0显示
2016-04-15 17:22
309 查看
在一般的asp.net导出Excel中都会把数字前的0默认去掉,此方法有效解决这问题。
下面是具体方法:
#region DataTable导出到Excel
/// <summary>
/// DataTable导出到Excel
/// </summary>
/// <param name="pData">DataTable</param>
/// <param name="pFileName">导出文件名</param>
public static void DataTableExcel(System.Data.DataTable pData, string pFileName)
{
// 当前对话
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
if (pData != null)
{
string UserAgent = curContext.Request.ServerVariables["http_user_agent"].ToLower();
if (UserAgent.IndexOf("firefox") == -1)//火狐浏览器
pFileName = System.Web.HttpUtility.UrlEncode(pFileName, System.Text.Encoding.UTF8);
curContext.Response.AddHeader("Content-Disposition", "attachment; filename=" + pFileName + ".xls");
curContext.Response.ContentType = "application/vnd.ms-excel";
StringBuilder TableSB = new StringBuilder();
#region 标题头
string strHeader = "<table border=\"1\" style=\"background-color:Gray;font-weight:bold;\"><tr>";
for (int i = 0; i < pData.Columns.Count; i++)
{
strHeader += "<td>" + pData.Columns[i].ColumnName + "</td>";
}
strHeader += "</tr></table>";
#endregion
TableSB.Append("<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">");
foreach (DataRow orow in pData.Rows) {
TableSB.Append("<tr>");
for (int i = 0; i < pData.Columns.Count; i++)
{
if (orow[i].ToString().StartsWith("0") && !orow[i].ToString().StartsWith("0."))
TableSB.Append(string.Format("<td style='mso-number-format:general;mso-number-format:\"{1}\"'>{0}</td>", orow[i].ToString(), @"\@"));
else
TableSB.Append("<td >" + orow[i].ToString() + "</td>");
}
TableSB.Append("</tr>");
}
TableSB.Append("</table>");
string strMeta = "<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=UTF-8\"/>";
curContext.Response.Write(strMeta + strHeader + TableSB.ToString());
curContext.Response.End();
}
}
#endregion
下面是具体方法:
#region DataTable导出到Excel
/// <summary>
/// DataTable导出到Excel
/// </summary>
/// <param name="pData">DataTable</param>
/// <param name="pFileName">导出文件名</param>
public static void DataTableExcel(System.Data.DataTable pData, string pFileName)
{
// 当前对话
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
if (pData != null)
{
string UserAgent = curContext.Request.ServerVariables["http_user_agent"].ToLower();
if (UserAgent.IndexOf("firefox") == -1)//火狐浏览器
pFileName = System.Web.HttpUtility.UrlEncode(pFileName, System.Text.Encoding.UTF8);
curContext.Response.AddHeader("Content-Disposition", "attachment; filename=" + pFileName + ".xls");
curContext.Response.ContentType = "application/vnd.ms-excel";
StringBuilder TableSB = new StringBuilder();
#region 标题头
string strHeader = "<table border=\"1\" style=\"background-color:Gray;font-weight:bold;\"><tr>";
for (int i = 0; i < pData.Columns.Count; i++)
{
strHeader += "<td>" + pData.Columns[i].ColumnName + "</td>";
}
strHeader += "</tr></table>";
#endregion
TableSB.Append("<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">");
foreach (DataRow orow in pData.Rows) {
TableSB.Append("<tr>");
for (int i = 0; i < pData.Columns.Count; i++)
{
if (orow[i].ToString().StartsWith("0") && !orow[i].ToString().StartsWith("0."))
TableSB.Append(string.Format("<td style='mso-number-format:general;mso-number-format:\"{1}\"'>{0}</td>", orow[i].ToString(), @"\@"));
else
TableSB.Append("<td >" + orow[i].ToString() + "</td>");
}
TableSB.Append("</tr>");
}
TableSB.Append("</table>");
string strMeta = "<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=UTF-8\"/>";
curContext.Response.Write(strMeta + strHeader + TableSB.ToString());
curContext.Response.End();
}
}
#endregion
相关文章推荐
- 多线程应用-类(thread)
- 160415、sql语句sort排序,sort为空的在后面
- QueryRunner使用
- c3p0操作MySQL数据库
- saiku执行速度慢
- XMLHttpRequest实现终止请求和超时
- javascript 二分查找排序
- 手游性能优化之深入理解Texture Compression
- json解析,jquery解析遍历json
- PostgreSQL代码分析,查询优化部分,canonicalize_qual
- saiku执行速度慢
- ENVI软件操作【数据显示操作——Overlay菜单操作】
- Java中参数的引用传递和值传递
- 返回一个一维整数数组中最大子数组的和
- Incorrect integer value: '' for column 'RegNum' at row 1
- Android中通过EventBus传递消息数据
- 查看android sqlite数据库常用操作
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (
- Iptables防火墙使用
- OSG OSGearth vs2010编译