如何从sql server复制数据时自动调整excel单元格

我正在使用存储过程将sql数据导出到excel中.

虽然打开excel几个单元格值部分隐藏,我想调整它的大小,包括标题..

并希望使标题更大胆..

PLZ帮我解决这个问题..

sql存储过程:

alter procedure usp_example         
 (                
 @db_name varchar(100),                
 @schm_name varchar(100),                
 @table_name varchar(100),                 
 @file_name varchar(100)                
  )                
 as          


--Generate column names as a recordset                
  declare @columns varchar(8000), @sql varchar(8000) ,  
   @sql1 varchar(8000), @data_file varchar(100)           

   select                       
    @columns=coalesce(@columns+',','')+column_name+' as '+column_name                       
    from                       
    information_schema.columns                      
    where                       
    table_name=@table_name       

    select @columns=''''+replace(replace(@columns,' as ',''' as '),',',',''')                           


    --Create a dummy file to have actual data                
     select @data_file=substring(@file_name,1,len(@file_name)-              
     charindex('\',reverse(@file_name)))+'\data_file.csv'                


      --Generate column names in the passed EXCEL file                
       set @sql='bcp " select ''DATE'', *,''SHIFT'' ,''SETUPTIME'' from (select '+@columns+') as t" queryout   c:\Daily_Reports\daily_machinedescription_report_summary.csv  -c -t,  -T -S ' + @@servername                
        exec master..xp_cmdshell @sql               
        print @sql             



     --Generate data in the dummy file        

    set @sql='bcp "SELECT convert(varchar, ENTRY_DATE, 105) as DATE ,machinename as Machine,RunTime,ECTime,ReworkTime as RWTime,IdleTime,DownTime,SHIFT_TYPE as Shift,SetupTime  FROM VentureBI_Prod_Test..view_admin_report ORDER BY segment " queryout   c:\Daily_Reports\data_file.csv  -c -t,  -T -S' + @@servername                

     exec master..xp_cmdshell @sql          



      --Copy dummy file to passed EXCEL file              
     set @sql= 'exec master..xp_cmdshell ''type '+@data_file+' >> "'+@file_name+'"'''              
     exec(@sql)               


         ----Delete dummy file               
       set @sql= 'exec master..xp_cmdshell ''del '+@data_file+''''              
       exec(@sql)    

问候,

T.Navin

保存宏代码时出错:

最佳答案 我猜你是把数据作为csv(来自你的代码)传输. csv文件无法格式化.

您可以在之后的编程语言中将csv文件转换为xlsx或xls.

点赞