NetSuite Suitescript 2.0 Export data to Excel file (xls)

Source: Internet
Author: User

In NetSuite suitescript, We usually do/implement export data to CSV, That's straight forward:

    1. Collect ' encoded ' string to Array for column, joins them with comma ', ' to is a string.
    2. Collect each Line's data same as column to push to the ARRAY.
    3. Join all of the Array data (include column row and all data rows) with ' \n\t ' to a big CSV string.
    4. Save the CSV string as file content then store it to file-cabinet, or write them directly in Suitelet as a Output.

Today I am going to talk about export custom NetSuite data to EXCEL file(file suffix is. xls)

Share screenshoot:

High Level View:

    1. Prepared XML Header String. Put in styles as desire, and workbook, worksheet, table
    2. Concat to put in dynamic cell Data. So we got whole the well formed XML string.
    3. Nlapicreatefile (suitescript 1.0) or file.create (suitescript 2.0) put in encoded XML string to create a Excel file .
    4. Store the file to filecabinet or set it as output of a suitelet (so directly download It)
Sample in Suitescript 2.0:

1 /**2 * @NApiVersion 2.x3 * @NScriptType Suitelet4 * @NModuleScope Sameaccount5 * @author Carl, Zeng.6 * @description This's A sample Suitelet script (suitescript 2.0) to export data7 * to Excel file and directly download it in browser8  */9 Define (Ten[' n/file ', ' N/encode ' ], one         /** a * @param {file} - * File - * @param {format} the * Format - * @param {record} - * Record - * @param {redirect} + * REDIRECT - * @param {runtime} + * Runtime a * @param {search} at * Search - * @param {serverwidget} - * Serverwidget -          */ -         function(file, Encode) { -  in             /** - * Definition of the Suitelet script trigger Point. to              *  + * @param {Object} - * Context the * @param {serverrequest} * * Context.request-encapsulation of the incoming $ * RequestPanax Notoginseng * @param {serverresponse} - * Context.response-encapsulation of the Suitelet the * Response + * @Since 2015.2 a              */ the             functiononrequest (context) { +  -                 if(context.request.method = = ' GET ') { $  $                     varXmlstr = ' <?xml version= ' 1.0 '? ><?mso-application progid= "excel.sheet"?> '; -Xmlstr + = ' <workbook xmlns= "urn:schemas-microsoft-com:office:spreadsheet" '; -Xmlstr + = ' xmlns:o= "urn:schemas-microsoft-com:office:office"; theXmlstr + = ' xmlns:x= "urn:schemas-microsoft-com:office:excel"; -Xmlstr + = ' xmlns:ss= "urn:schemas-microsoft-com:office:spreadsheet";WuyiXmlstr + = ' xmlns:html= ' http://www.w3.org/TR/REC-html40 > '; the  -Xmlstr + = ' <Styles> ' wu+ ' <style ss:id= ' S63 ' > ' -+ ' <font x:charset= "204" ss:size= "ss:color=" #000000 "ss:bold=" 1 "ss:underline=" single "/> ' about+ ' </Style> ' + ' </Styles> '; $  -Xmlstr + = ' <worksheet ss:name= ' Sheet1 ' > '; -Xmlstr + = ' <Table> ' -+ ' <Row> ' a+ ' <cell ss:styleid= "s63" ><data ss:type= "String" > ID </Data></Cell> ' ++ ' <cell><data ss:type= "String" > Products Feature </Data></Cell> ' the+ ' </Row> '; -  $Xmlstr + = ' <Row> ' the+ ' <cell><data ss:type= ' String >1</Data></Cell> ' the+ ' <cell><data ss:type= "String" >netsuite Export csv</data></cell> ' the+ ' </Row> '; the  -Xmlstr + = ' <Row> ' in+ ' <cell><data ss:type= ' String >2</Data></Cell> ' the+ ' <cell><data ss:type= "String" >netsuite Export excel</data></cell> ' the+ ' </Row> '; about  theXmlstr + = ' </Table></Worksheet></Workbook> '; the  the                     varstrxmlencoded =Encode.convert ({ + string:xmlstr, - Inputencoding:encode. encoding.utf_8, the Outputencoding:encode. Encoding.base_64Bayi                     }); the  the                     varObjxlsfile =file.create ({ -Name: ' Sampleexport.xls ', - Filetype:file. type.excel, the contents:strxmlencoded the                     }); the                     //Optional:you can choose to save it to file cabinet the                     //Objxlsfile.folder = -14; -                     //var Intfileid = Objxlsfile.save (); the  the context.response.writeFile ({ the File:objxlsfile94                     }); the                 } the  the             }98  about             return { - onrequest:onrequest101             };102 103});

NetSuite Suitescript 2.0 Export data to Excel file (xls)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.