DataTable 字段包含 HTML 标记,导出 Excel 的方法;将数字按字符串格式导出

最近需要做DataTable 导出Excel,由于DataTable 的结构是任意的,也就是说可以包含各种字段,于是DataTable 中可能含有Html 标记(<tr><td><br> 等),开始按常规方法导出,以\t, \r\n 做分隔符,就会出现问题,导出的Excel文件完全乱了。那些Html标记会在Excel中表现出来,比如<hr>就变成了横线。

None.gif                 System.Web.HttpContext.Current.Response.Write(cell1.ToString());
None.gif                System.Web.HttpContext.Current.Response.Write(
@" \t " ));
None.gif                System.Web.HttpContext.Current.Response.Write(cell2.ToString());
None.gif                System.Web.HttpContext.Current.Response.Write(
@" \r\n " ));

现在按HTML格式导出,就可以了。
另外DataTable 中可能含有字符串字段,但是该字段由数字组成,比如"身份证号码",也需要做处理,转换为字符串格式。

None.gif    string  strFileName  =   " Export.xls " ;
None.gif                System.Web.HttpContext.Current.Response.AddHeader(
" Content-Disposition " " attachment; filename= "   +  Server.UrlEncode(strFileName));  // 防止文件名出现乱码
None.gif
                System.Web.HttpContext.Current.Response.ContentType  =   " application/vnd.ms-excel " // 设置输出文件类型为excel文件。 
None.gif
None.gif                
//  因为 DataTable 中可能含有 html (<hr> <br> 等)标记,所以要用特殊的导出方式,即按Html形式导出
None.gif
                StringBuilder line  =   new  StringBuilder();
None.gif                
string  NumberAsTextExp  =   " vnd.ms-excel.numberformat:@ " ;  
None.gif                
string  s;
None.gif                line.Append(
" <table border = 1> " );
None.gif                
for  ( int  i  =   0 ; i  <  dtTable.Rows.Count; i ++ )   // dtTable 是要导出的DataTable
ExpandedBlockStart.gifContractedBlock.gif
                 dot.gif {
InBlock.gif                    line.Append(
"<tr>");
InBlock.gif                    
for (int j = 0; j < dtTable.Columns.Count; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        line.Append(
"<td style='"+ NumberAsTextExp + "'>"); //将数字按字符串格式导出,比如身份证号码
InBlock.gif
                        s = dtTable.Rows[i][j].ToString();
InBlock.gif                        
if (dtTable.Columns[j].DataType == typeof(string))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            s 
= Server.HtmlEncode(s);
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        line.Append(s 
+ "</td>");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    line.Append(
"</tr>");
ExpandedBlockEnd.gif                }

None.gif                line.Append(
" </table> " );
None.gif                System.Web.HttpContext.Current.Response.Write(
@" <html><head> " );
None.gif                System.Web.HttpContext.Current.Response.Write(
@" <meta http-equiv=""content-type"" content=""text/html; charset=gb2312""> " );
None.gif                System.Web.HttpContext.Current.Response.Write(
" </head> " );
None.gif                System.Web.HttpContext.Current.Response.Write(
" <body> " );
None.gif                System.Web.HttpContext.Current.Response.Write(line.ToString());
None.gif                System.Web.HttpContext.Current.Response.Write(
" </body> " );
None.gif                System.Web.HttpContext.Current.Response.Write(
@" </html> " );
None.gif
None.gif                System.Web.HttpContext.Current.Response.End();

转载于:https://www.cnblogs.com/Yes/archive/2007/06/01/767398.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值