Saturday, January 5, 2019

Create Excel file via SuiteScript

To create an xls file in SuiteScript, use the following code as guide:


//construct xml equivalent of the file

var xmlString = '<?xml version="1.0"?><?mso-application progid="Excel.Sheet"?>'; 
xmlString += '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" ';
xmlString += 'xmlns:o="urn:schemas-microsoft-com:office:office" ';
xmlString += 'xmlns:x="urn:schemas-microsoft-com:office:excel" ';
xmlString += 'xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ';
xmlString += 'xmlns:html="http://www.w3.org/TR/REC-html40">'; 

xmlString += '<Worksheet ss:Name="Sheet1">';
xmlString += '<Table>' + 
           '<Row>' +
            '<Cell><Data ss:Type="String"> First Header </Data></Cell>' +
            '<Cell><Data ss:Type="String"> Second Header </Data></Cell>' +
            '<Cell><Data ss:Type="String"> Third Header </Data></Cell>' +
            '<Cell><Data ss:Type="String"> Fourth Header </Data></Cell>' +
            '<Cell><Data ss:Type="String"> Fifth Header </Data></Cell>' +
       '</Row>';

xmlString += '<Row>' + 
                        '<Cell><Data ss:Type="String">Row 1 Column 1</Data></Cell>' + 
                        '<Cell><Data ss:Type="String">Row 1 Column 2</Data></Cell>' + 
                        '<Cell><Data ss:Type="String">Row 1 Column 3</Data></Cell>' + 
                        '<Cell><Data ss:Type="String">Row 1 Column 4</Data></Cell>' + 
                        '<Cell><Data ss:Type="String">Row 1 Column 5</Data></Cell>' + 
              '</Row>';

xmlString += '<Row>' + 
                        '<Cell><Data ss:Type="String">Row 2 Column 1</Data></Cell>' + 
                        '<Cell><Data ss:Type="String">Row 2 Column 2</Data></Cell>' + 
                        '<Cell><Data ss:Type="String">Row 2 Column 3</Data></Cell>' + 
                        '<Cell><Data ss:Type="String">Row 2 Column 4</Data></Cell>' + 
                        '<Cell><Data ss:Type="String">Row 2 Column 5</Data></Cell>' + 
              '</Row>';

xmlString += '</Table></Worksheet></Workbook>';

//create file
var xlsFile = nlapiCreateFile('TEST.xls', 'EXCEL', nlapiEncrypt(xmlString, 'base64'));

xlsFile.setFolder(445);

//save file 
var fileID = nlapiSubmitFile(xlsFile);

3 comments:

  1. Can you please send me the sample in script 2.0 version for reference.
    I already searched in multiple sites but didn't find such as any reference.

    ReplyDelete
  2. What a thrilling post, you have pointed out some excellent points, I as well believe this is a superb website. I have planned to visit it again and again. 2ms office 2016 product key

    ReplyDelete
  3. Comfortabl y, the post is actually the freshest on this precious topic. I concur with your conclusions and also definitely will eagerly look forward to your future updates. Saying thanks definitely will not simply just be acceptable, for the fantastic lucidity in your writing. I definitely will instantly grab your rss feed to stay abreast of any updates. Fabulous work and also much success in your business endeavors! Help with Excel

    ReplyDelete