write out the data
--now we write out the data
SELECT @currentRow = 2 WHILE (@currentRow <= @Rows+1 AND @hr=0) BEGIN SELECT @currentColumn = 1 WHILE (@currentColumn <= @Columns AND @hr=0) BEGIN IF @hr=0 SELECT @strErrorMessage= 'getting the value from the query string' + LTRIM(STR(@currentRow)) +',' + LTRIM(STR(@currentRow))+')', @objErrorObject=@objQueryResults, @ResultSetRow=@CurrentRow-1 IF @hr=0 EXEC @hr=sp_OAMethod @objQueryResults, 'GetColumnString', @value OUT, @ResultSetRow, @currentColumn IF @hr=0 SELECT @strErrorMessage= 'assigning the value from the query string' + LTRIM(STR(@CurrentRow-1)) +', ' + LTRIM(STR(@currentcolumn))+')' , @objErrorObject=@objExcel, @command='Cells('+STR(@currentRow) +', ' + STR(@CurrentColumn)+').value' IF @hr=0 EXEC @hr=sp_OASetProperty @objExcel, @command, @value SELECT @currentColumn = @currentColumn + 1 END SELECT @currentRow = @currentRow + 1 END --define the name range --Cells(1, 1).Resize(10, 5).Name = "TheData" IF @hr=0 SELECT @strErrorMessage='assigning a name to a range ' + LTRIM(STR(@CurrentRow-1)) +', ' + LTRIM(STR(@currentcolumn-1))+')' , @objErrorObject=@objExcel, @command='Cells(1, 1).Resize('+STR(@currentRow-1) +', ' + STR(@CurrentColumn-1)+').Name' IF @hr=0 EXEC @hr=sp_OASetProperty @objExcel, @command, @RangeName
--Now autofilt the columns we've written to IF @hr=0 SELECT @strErrorMessage='Auto-fit the columns ', @objErrorObject=@objWorkSheet, @command='Columns("A:' +SUBSTRING(@alphabet,(@Columns / 26),1) +SUBSTRING(@alphabet,(@Columns % 26),1)+ '").autofit'
IF @hr=0 --insert into @bucket(bucket) EXEC @hr=sp_OAMethod @objWorkSheet, @command, @output out
IF @hr=0 SELECT @command ='del "' + @filename + '"' IF @hr=0 EXECUTE master..xp_cmdshell @Command, no_output IF @hr=0 SELECT @strErrorMessage='Saving the workbook as "'+@filename+'"', @objErrorObject=@objRange, @command = 'SaveAs("' + @filename + '")' IF @hr=0 EXEC @hr=sp_OAMethod @objWorkBook, @command IF @hr=0 SELECT @strErrorMessage='closing Excel ', @objErrorObject=@objExcel EXEC @hr=sp_OAMethod @objWorkBook, 'Close' EXEC sp_OAMethod @objExcel, 'Close'
IF @hr<>0 BEGIN DECLARE @Source VARCHAR(255), @Description VARCHAR(255), @Helpfile VARCHAR(255), @HelpID INT
EXECUTE sp_OAGetErrorInfo @objErrorObject, @source output,@Description output, @Helpfile output,@HelpID output SELECT @hr, @source, @Description,@Helpfile,@HelpID output SELECT @strErrorMessage='Error whilst ' +COALESCE(@strErrorMessage,'doing something') +', '+COALESCE(@Description,'') RAISERROR (@strErrorMessage,16,1) END EXEC sp_OADestroy @objServer EXEC sp_OADestroy @objQueryResults EXEC sp_OADestroy @objCurrentResultSet EXEC sp_OADestroy @objExcel EXEC sp_OADestroy @objWorkBookks EXEC sp_OADestroy @objWorkBook EXEC sp_OADestroy @objRange RETURN @hr GO |
手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )
GMT-8, 2026-4-11 19:45 , Processed in 0.022369 second(s), 22 queries .
Supported by Weloment Group X3.5
© 2008-2026 Best Deal Online