Exporting to Excel with bcp
I was recently looking for a quick way to export SQL Server table data (over 1,00,000 records) to a Excel file. I found that the bcp command line tool can export SQL Server table data to a .xls file but when I export it to a Excel 2007 (.xlsx) file, I couldn't open the file. It throws a warning - "Excel cannot open the file because the file format or extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file". The fix to this issue reported in ASP.NET Forums which points to a MS KB article , is complicated. I wondered, if bcp can export to a .xls file why can't it smoothly export to a .xlsx file? Curiously, I was able to able to export over 7 lakh records to a .xls file (which is supposed to have a row limit of ~65K rows in 22seconds) with bcp and the .xls file opens fine in Excel 2007 displaying all the 7 lakh records. Vinod helped me understand how the process actually works - Fundamentally, you n...