NetSuite SuiteScript 2.0 export data to Excel file(xls)
In NetSuite SuiteScript, We usually do/implement export data to CSV, that's straight forward:
- Collect 'encoded' string to Array for column, join them with comma ',' to be a string.
- Collect each line's data same as column to push to the Array.
- Join all 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 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:
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
* @NModuleScope SameAccount
* @author Carl, Zeng
* @description This's a sample SuiteLet script(SuiteScript 2.0) to export data
* to Excel file and directly download it in browser
*/
define(
[ 'N/file', 'N/encode' ],
/**
* @param {file}
* file
* @param {format}
* format
* @param {record}
* record
* @param {redirect}
* redirect
* @param {runtime}
* runtime
* @param {search}
* search
* @param {serverWidget}
* serverWidget
*/
function(file, encode) { /**
* Definition of the Suitelet script trigger point.
*
* @param {Object}
* context
* @param {ServerRequest}
* context.request - Encapsulation of the incoming
* request
* @param {ServerResponse}
* context.response - Encapsulation of the Suitelet
* response
* @Since 2015.2
*/
function onRequest(context) { if (context.request.method == 'GET') { var xmlStr = '<?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" ';
xmlStr += 'xmlns:x="urn:schemas-microsoft-com:office:excel" ';
xmlStr += 'xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ';
xmlStr += 'xmlns:html="http://www.w3.org/TR/REC-html40">'; xmlStr += '<Styles>'
+ '<Style ss:ID="s63">'
+ '<Font x:CharSet="204" ss:Size="12" ss:Color="#000000" ss:Bold="1" ss:Underline="Single"/>'
+ '</Style>' + '</Styles>'; xmlStr += '<Worksheet ss:Name="Sheet1">';
xmlStr += '<Table>'
+ '<Row>'
+ '<Cell ss:StyleID="s63"><Data ss:Type="String"> ID </Data></Cell>'
+ '<Cell><Data ss:Type="String"> Products Feature </Data></Cell>'
+ '</Row>'; xmlStr += '<Row>'
+ '<Cell><Data ss:Type="String">1</Data></Cell>'
+ '<Cell><Data ss:Type="String">NetSuite Export CSV</Data></Cell>'
+ '</Row>'; xmlStr += '<Row>'
+ '<Cell><Data ss:Type="String">2</Data></Cell>'
+ '<Cell><Data ss:Type="String">NetSuite Export Excel</Data></Cell>'
+ '</Row>'; xmlStr += '</Table></Worksheet></Workbook>'; var strXmlEncoded = encode.convert({
string : xmlStr,
inputEncoding : encode.Encoding.UTF_8,
outputEncoding : encode.Encoding.BASE_64
}); var objXlsFile = file.create({
name : 'sampleExport.xls',
fileType : file.Type.EXCEL,
contents : strXmlEncoded
});
// Optional: you can choose to save it to file cabinet
// objXlsFile.folder = -14;
// var intFileId = objXlsFile.save(); context.response.writeFile({
file : objXlsFile
});
} } return {
onRequest : onRequest
}; });
NetSuite SuiteScript 2.0 export data to Excel file(xls)的更多相关文章
- C# Note38: Export data into Excel
Microsoft.Office.Interop.Excel You have to have Excel installed. Add a reference to your project to ...
- csharp:using OpenXml SDK 2.0 and ClosedXML read excel file
https://openxmlexporttoexcel.codeplex.com/ http://referencesource.microsoft.com/ 引用: using System; u ...
- How to create a zip file in NetSuite SuiteScript 2.0 如何在现有SuiteScript中创建和下载ZIP压缩文档
Background We all knows that: NetSuite filecabinet provided a feature to download a folder to a zip ...
- Export SQLite data to Excel in iOS programmatically(OC)
//For the app I have that did this, the SQLite data was fairly large. Therefore, I used a background ...
- Export Data from mysql Workbench 6.0
原文地址:export-data-from-mysql-workbench-6-0 问题描述 I'm trying to export my database, using MySQL Workben ...
- csharp: Export DataSet into Excel and import all the Excel sheets to DataSet
/// <summary> /// Export DataSet into Excel /// </summary> /// <param name="send ...
- Insert data from excel to database
USE ESPA Truncate table dbo.Interface_Customer --Delete the table data but retain the structure exec ...
- csharp: Export DataTable to Excel using OpenXml 2.5 in asp.net
//https://www.microsoft.com/en-us/download/details.aspx?id=5124 Open XML SDK 2.0 for Microsoft Offic ...
- csharp: Export or Import excel using MyXls,Spire.Xls
excel 2003 (效果不太理想) using System; using System.Collections.Generic; using System.ComponentModel; usi ...
随机推荐
- 静态工厂方法VS构造器
我之前已经介绍过关于构建者模式(Builder Pattern)的一些内容,它是一种很有用的模式用于实例化包含几个属性(可选的)的类,带来的好处是更容易读.写及维护客户端代码.今天,我将继续介绍对象创 ...
- WordPress基础:固定链接的设置
安装wordpressp后的工作之一最好修改固定链接,原因就是wp默认的/?p=123形式不利于搜索引擎优化而且也不美观. URL结构参数说明:%year%:日志发表的年份,如2009%monthnu ...
- mysql关于排序值的问题
- idea使用心得(3)-重构初探
上一篇idea心得中,介绍了快捷键的用法.其中提及了重构神器Alt+Ctrl+Shift+T , 当时只是稍稍提及,本文重点在idea提供的重构选项.后续会有<重构,改善既有代码的设计>的 ...
- css引入方式
1.<style> body{} </style> 2.写在一个单独的文件里面保存即新建一个文件:xx.css; 注明该文件的位置<link re ...
- mybatis比较数字或者单字母
http://blog.csdn.net/alibert/article/details/50177017 <if test="req.queryType == '3'.toStrin ...
- java集合-HashSet
HashSet 概述 对于 HashSet 而言,它是基于 HashMap 实现的,底层采用 HashMap 来保存元素,所以如果对 HashMap 比较熟悉了,那么学习 HashSet 也是很轻松的 ...
- SpringMVC参数自动绑定
SpringMVC的各种参数绑定方式 1. 基本数据类型(以int为例,其他类似):Controller代码: @RequestMapping("saysth.do") publi ...
- 使用Maven构建Java Web项目时,关于jsp中引入js、css文件路径问题。
今天有点闲,自己动手搭建一个Java Web项目,遇到jsp中引入js.css文件时路径不正确的问题,于是在网上查阅了很多资料,最终都无法解决问题,于是,上stackoverflow找到了解决方法,这 ...
- Java中Comparable和Comparator接口区别分析
Java中Comparable和Comparator接口区别分析 来源:码农网 | 时间:2015-03-16 10:25:20 | 阅读数:8902 [导读] 本文要来详细分析一下Java中Comp ...