smartsheet-apismartsheet-api-1.1smartsheet-java-sdk-v1

Smartsheet - Download Sheet as Excel File using API


I am trying to use the Smartsheet API to download a sheet as an excel file. I am really struggling to put together the correct code to do this.

Can you provide a basic code example that will download an excel file using the Smartsheet API?


Solution

  • The documentation for creating an excel file from a sheet is here. It gives an example that uses curl. After curl is installed the following command can be used with the bold fields replaced with the appropriate values:

    curl https://api.smartsheet.com/1.1/sheet/SHEET_ID -H "Authorization: Bearer ACCESS_TOKEN" -H "Accept: application/vnd.ms-excel" -o OUTPUT.XLS

    SHEET_ID: this is the id of the sheet. It can be retrieved inside the smartsheet interface (screenshot below) by right clicking on a sheet tab and selecting properties. It can also be retrieved via the API by hitting the sheets endpoint (https://api.smartsheet.com/1.1/sheets). More info on the endpoint is here.

    enter image description here

    ACCESS_TOKEN: Is a token that can be retrieved via the smartsheet interface by clicking on "Account" selecting "Personal Settings" and then clicking "API Access". Then click the "Generate new access token" button to create a new token.

    enter image description here

    OUTPUT.XLS: is the name of the excel file that will be created in the current directory.


    I also want to point out that the same steps can be accomplished using Smartsheet's Java SDK. After installing the SDK the following code can be used to download a sheet as an Excel file.

    public static void main(String[] args) throws SmartsheetException, IOException {
        // Setup the File object
        File file = new File("OUTPUT.XLS");
    
        // Create the file if it does not exist
        if(!file.exists()){
            file.createNewFile();
        }
    
        // Create the output stream from the File object
        FileOutputStream outputStream = new FileOutputStream(file, false);
    
        // Setup a Smartsheet object with the necessary access token
        Smartsheet smartsheet = new SmartsheetBuilder().setAccessToken("ACCESS_TOKEN").build();
    
        // Request the sheet as an excel file from smartsheet
        long sheetId = 8263950702798724L;// SHEET_ID
        smartsheet.sheets().getSheetAsExcel(sheetId, outputStream);
    
        // Flush and Close the output stream
        outputStream.flush();
        outputStream.close();
    }
    

    Again, replace SHEET_ID, ACCESS_TOKEN and OUTPUT.XLS with the appropriate values.