Saving data from Snowflake to S3 bucket as a XLSX file

Viewed 146

I wan't to save query result from Snowflake. I can save the file as a CSV file but is it possible to save the data as an XLSX file

The query I used:

file_name = 'w{}_processedsample_{}_rawsample.xlsx'.format(wave_number, time_stamp)
    
query = f"select * from {database}.{schema}.{view} where wavenumber={wave_number}"
    
get_view_query = f"copy into  @{database}.{schema}.{save_to_s3_stage}/{file_name} from ({query})  HEADER = TRUE OVERWRITE = TRUE SINGLE=TRUE FILE_FORMAT = (TYPE=CSV FIELD_DELIMITER = ',' NULL_IF ='' ESCAPE_UNENCLOSED_FIELD = '\\\\' EMPTY_FIELD_AS_NULL = FALSE ENCODING = UTF8)"

Currently the xlsx file is corrupt. Also I need the xlsx file to not be zipped.

1 Answers

When you unload data from Snowflake you can only save it to one of the supported file formats:

enter image description here

You can't change the file extension to anything you like. In your example, you are unloading a CSV file that is simply named with a .xlsx extension so your computer doesn't open it properly with excel (probably because it's compressed actually).

To make sure the file is not zipped, change your file format to (the last line is the change):

FILE_FORMAT = (
   TYPE=CSV FIELD_DELIMITER = ',' 
   NULL_IF ='' 
   ESCAPE_UNENCLOSED_FIELD = '\\\\' 
   EMPTY_FIELD_AS_NULL = FALSE 
   ENCODING = UTF8
   COMPRESSION= NONE
)"
Related