In NetSuite suitescript, We usually do/implement export data to CSV, That's straight forward:
- Collect ' encoded ' string to Array for column, joins them with comma ', ' to is a string.
- Collect each Line's data same as column to push to the ARRAY.
- Join all of the Array data (include column row and all data rows) with ' \n\t ' to a big CSV string.
- 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:
- Prepared XML Header String. Put in styles as desire, and workbook, worksheet, table
- Concat to put in dynamic cell Data. So we got whole the well formed XML string.
- Nlapicreatefile (suitescript 1.0) or file.create (suitescript 2.0) put in encoded XML string to create a Excel file .
- 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)