ANS-0908 · SUITESCRIPT DEVELOPMENT
How to Generate an Excel File from XML Content in NetSuite SuiteScript 2.x
This guide demonstrates how to programmatically create and save an Excel (.xls) file in NetSuite by constructing its XML content using SuiteScript 2.x file module functions.
Short answer
To create an Excel file in NetSuite using SuiteScript 2.x, generate the Excel XML content as a string. Use `file.create` to instantiate a file object with the XMLDOC type, name, and content. Set the desired folder and encoding properties, then use `file.save` to store the file in the NetSuite File Cabinet.
Scenario
A NetSuite user needs to programmatically generate an Excel spreadsheet (.xls) containing specific data and save it to the File Cabinet. The requirement is to construct the Excel file directly from XML content rather than using a CSV or other simpler format, allowing for more complex formatting or multi-sheet structures.
Solution
To generate an Excel file from XML content in NetSuite using SuiteScript 2.x, follow these steps:
Prepare the SuiteScript 2.x Environment:
Ensure your script is defined with
@NApiVersion 2.xand requires theN/filemodule. The core logic will reside within a function, which can then be called from an appropriate SuiteScript 2.x entry point (e.g.,executefor a Scheduled Script).Implement the File Generation Logic:
The following SuiteScript 2.x code snippet demonstrates how to create an Excel file named 'helloGeorge.xls' with two sheets containing sample data. Note the use of
file.createandfile.savefrom theN/filemodule, and direct property assignments forfolderandencoding.
javascript
/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript // Example script type; adjust as needed
*/
define(['N/file'], function(file) {
function doit(){
var xlsFile = file.create({
name: 'helloGeorge.xls',
fileType: file.Type.XMLDOC,
contents: '<?xml version"1.0"?> <?mso-application progid"Excel.Sheet"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> </DocumentProperties><ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> <ProtectStructure>False</ProtectStructure> <ProtectWindows>False</ProtectWindows></ExcelWorkbook> <Styles> <Style ss:ID="Default" ss:Name="Normal"> <Alignment ss:Vertical="Bottom" /> <Borders /> <Font /> <Interior /> <NumberFormat /> <Protection /> </Style></Styles> <Worksheet ss:Name="Sample Sheet 1"> <Table ss:ExpandedColumnCount="2" x:FullColumns="1" x:FullRows="1" ID="Table1"><Column ss:Width="150" /><Column ss:Width="200" /><Row> <Cell><Data ss:Type="Number">1</Data></Cell> <Cell><Data ss:Type="Number">2</Data></Cell></Row><Row> <Cell><Data ss:Type="Number">3</Data></Cell> <Cell><Data ss:Type="Number">4</Data></Cell></Row><Row> <Cell><Data ss:Type="Number">5</Data></Cell> <Cell><Data ss:Type="Number">6</Data></Cell></Row><Row> <Cell><Data ss:Type="Number">7</Data></Cell> <Cell><Data ss:Type="Number">8</Data></Cell></Row></Table></Worksheet><Worksheet ss:Name="Sample Sheet 2"><Table ss:ExpandedColumnCount="2" x:FullColumns="1" x:FullRows="1" ID="Table2"><Column ss:Width="150" /><Column ss:Width="200" /><Row> <Cell><Data ss:Type="String">A</Data></Cell> <Cell><Data ss:Type="String">B</Data></Cell></Row><Row> <Cell><Data ss:Type="String">C</Data></Cell> <Cell><Data ss:Type="String">D</Data></Cell></Row><Row> <Cell><Data ss:Type="String">E</Data></Cell> <Cell><Data ss:Type="String">F</Data></Cell></Row><Row> <Cell><Data ss:Type="String">G</Data></Cell> <Cell><Data ss:Type="String">H</Data></Cell></Row></Table></Worksheet></Workbook>'
});
xlsFile.folder = -15; // -15 typically refers to the "Attachments Received" folder
xlsFile.encoding = 'windows-1252';
file.save({
file: xlsFile
});
}
// Call the function. In a typical SuiteScript 2.x implementation,
// this would be part of an exported entry point (e.g., return { execute: doit };)
// or called directly if the script type allows for immediate execution.
doit();
});Expert NetSuite Support
Need help with this NetSuite issue?
SuiteScript Development consulting and configuration support
