-------------------------数字转换成EXCEL列名---------------------------------

alter function col_rev(
 @i_version int, -- Excel 版本号 
 @i_col int          -- 字段号(数值表示)
 )
returns varchar(20)
as
begin
  declare @v_devide int;
  declare @v_mod int;
  declare @v_col int;
  declare @str nvarchar(20);
  set @v_col=@i_col;
  set @str = '';
  
  if @i_version=2003 and @v_col<=256  -- Excle 2003 最多256列
  begin
    if @v_col>26
    begin
      set @str= (CASE when (@v_col%26)=0 then char(ascii('A')+(@v_col/26)-2)+'Z'
                 ELSE char(ascii('A')+(@v_col/26)-1)+char(ascii('A')+(@v_col%26)-1) end); 
    end
    else if @v_col=26
    begin
      set @str = 'Z';
    end
    else
    begin
      select @str=char(ascii('A')+(@v_col%26)-1);
    end
  end
  return @str;
end

select '76' AS C1, dbo.col_rev(2003,76) as col UNION ALL
select '77' AS C1, dbo.col_rev(2003,77) as col UNION ALL
select '78' AS C1, dbo.col_rev(2003,78) as col UNION ALL
select '79' AS C1, dbo.col_rev(2003,79);


 

比较古老的方法了,在网上看见了,COPY过来备注一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值