Search code examples
sql-serverexcelexport-to-excelexport-to-csv

Pasting Large Amounts of Data to Excel


I have a query that I run in SQL Server using Management Studio. The results are output to the grid that I then copy and paste to Excel sheet that gets saved in .csv format. I've never had any problem doing this before but this one particular query includes a column that contains a large chunk of HTML (several thousand char.). If I try to paste the entire query output into excel it can't handle that column and the sheet is unusable.

I have to paste everything else and then copy each of the rows individually that contain the HTML and double click the destination cell in Excel to enter in "edit mode". Is there any way around this?

I have tried: Output to text but no matter what I set the maximum column value to in SSMS it truncates the HTML column. I have tried output to file but the only file choice it gives me is Crystal Reports. Is there a way to either put Excel in edit mode globally so that I can paste the entire thing at once, or to tell SSMS (Express Version) to output to a .csv file?

ETA: I've tried right clicking on the grid results and saving as .csv but it still truncates the HTML column. Even though I have "Results to Text" option set at 8K chars. and non XML data set to over 64K in the results to grid option.


Solution

  • As @Remy pointed out, have you tried using OPENROWSET:

    insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
    'Excel 8.0;Database=C:\template.xls;', 
    'SELECT * FROM [SheetName$]') 
    select * from myTable
    

    -- EDIT

    Make sure you have Ad Hoc Distributed Queries enabled:

    sp_configure 'show advanced options', 1;
    GO
    RECONFIGURE;
    GO
    sp_configure 'Ad Hoc Distributed Queries', 1;
    GO
    RECONFIGURE;
    GO
    

    Good luck.