TD HTML标记的右对齐

时间:2014-10-29 19:23:53

标签: html sql-server for-xml-path

由于某些原因,我正在为特定时间段的客户生成HTML代码,并且在该代码中我需要将Dollar字段('付费'和'费用'列)文本右对齐,这怎么可能使用XML raw('tr' )。

create table #Customer
(
id varchar(500),
CustomerName varchar(500),
customertype varchar(500),
LastOrderNo varchar(500),
charges varchar(500),
Payed varchar(500)
)

insert into #Customer
select '201','Cus1','XX','on-09799','60.09$','30.00$'
union all 
select '202','Cus2','XX','on-09777','90.09$','50.00$' 
union all 
select '203','Cus3','YY','on-09766','100.09$','100.00$' 
union all 
select '204','Cus4','ZZ','on-09788','2000.09$','2000.00$' 


Declare @Body varchar(max)
set @Body=''

select @Body =@Body+'<table cellpadding=0 cellspacing=0 border=0 >' +
                  '<tr><td width="100px" > ID</td>' +
                      '<td width="140px" >CustomerName</td>' +
                      '<td width="100px" >Customertype</td>' +
                      '<td width="100px" > LastOrderNo</td>' +
                      '<td width="100px" align="right" bgcolor=#E6E6FA><b> charges</b></td>' +
                      '<td width="110px" align="right" bgcolor=#E6E6FA><b> Payed</b></td></tr>'

select @Body = @Body+(SELECT  
                        td= id,'',
                        td= CustomerName,'',
                        td= customertype,'',
                        td= LastOrderNo,'',
                        td= charges,'',     
                        td= Payed,''    
                        from #Customer                          
                        For XML raw('tr'), Elements
                )+'</table>'


select @Body

select * from #Customer


drop table #Customer

2 个答案:

答案 0 :(得分:4)

我建议你尝试将两行更改为:

             '<td width="100px" style="text-align:right" bgcolor=#E6E6FA><b> charges</b></td>' +
             '<td width="110px" style="text-align:right" bgcolor=#E6E6FA><b> Payed</b></td></tr>'

您可以使用以下方式进一步移动样式中的背景颜色:

 style="text-align:right;background-color:#E6E6FA"

然后,您可以在'text-align:right' as 'td/@style'之前添加td= charges,'',。如果在SELECT之后添加它,它将对齐所有cols。试试吧。

 select @Body = @Body+(SELECT   
      td= id,'',
      td= CustomerName,'',
      td= customertype,'',
      td= LastOrderNo,'',
      [td/@align]='right',
      td= charges,'',  
      [td/@align]='right',   
      td= Payed,''    
    from #Customer                          
    For XML path('tr') ---- instead of for xml raw(tr), element
 )+'</table>'

答案 1 :(得分:-1)

至少可以说你的要求真的很奇怪。但由于这个问题是关于样式化HTML,所以不要单独使用SQL。将CSS样式添加到HTML文件中:

<style type="text/css">
        tr td:nth-child(5), tr td:nth-child(6) {
            text-align:right;
        }
</style>

右对齐第5和第6列,即费用和付费。