JS导出txt文本文件,Netsuite方案

//传入一个table的id,将table的全部内容导出excel文件
function AutomateExcel(objTable)
{
// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var hang = objTable.rows.length;

var lie = objTable.rows(0).cells.length;

// Add table headers going cell by cell.
for (var i=0;i<hang;i++)
{
for (var j=0;j<lie;j++)
{
oSheet.Cells(i+1,j+1).value = objTable.rows(i).cells(j).innerText;
}

}
oXL.Visible = true;
oXL.UserControl = true;
}

//描述:将固定格式的xml文件导出excel文件
//strXml:传入的xml字符串,一般为dataset直接getxml得到的就可以;
//xmlField:要导入的字段和对应的中文名称,格式如下:
//var xmlField="& lt;FIELDLIST><TITLE>主题</TITLE><KEYWORD>关键词< /KEYWORD><TYPE>报题来源</TYPE><CREATE_DATE>创建日期< /CREATE_DATE><COLUMN_NAME>所属栏目</COLUMN_NAME>< /FIELDLIST>";
function ExpXmlToExcel(strXml,xmlField)
{
//导入xml字符串
var xmlDoc = new XmlDoc();
xmlDoc.loadXML(strXml);
var nodesList = xmlDoc.documentElement.childNodes;

//导入字段列表;
var xmlDocField = new XmlDoc();
xmlDocField.loadXML(xmlField);
var fieldList = xmlDocField.documentElement.childNodes;

// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var hang = nodesList.length;
var lie = fieldList.length;
//插入表头

for (var j=0;j<lie;j++)
{
oSheet.Cells(1,j+1).value =fieldList[j].text;
}

// Add table headers going cell by cell.
for (var i=0;i<hang;i++)
{
for (var j=0;j<lie;j++)
{
oSheet.Cells(i+2,j+1).value = nodesList[i].selectSingleNode(fieldList[j].nodeName).text;
}
}
oXL.Visible = true;
oXL.UserControl = true;
}

//指定页面区域内容导入Word
//eDiv:要导出具体内容的div
function ExpHtmlToWord(eDiv)
{
var oWD = new ActiveXObject("Word.Application");
var oDC = oWD.Documents.Add("",0,1);
var oRange =oDC.Range(0,1);
var sel = document.body.createTextRange();

sel.moveToElementText(eDiv);
sel.select();
sel.execCommand("Copy");
oRange.Paste();
oWD.Application.Visible = true;
}

----------------------------------------------------------------------------------------------------------

var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile("c:\\testfile.txt", true);
a.WriteLine("This is a test.");
a.Close();

FileSystemObject

直接在服务端生成excel,txt,html发送给客户端下载岂不省事? 何必多此一举非要到客户端转个圈圈呢.

=======================================================================

两种方案都可以实现,在netsuite中,我们可以使用nlapiPrintRecord(type, id, mode, properties)

Parameters
? type {string} [required] - Print operation type: TRANSACTION|STATMENT
? id {string} [required] - The internal ID of the transaction or statement being printed
? mode {string} [optional] - The output type: PDF|HTML|DEFAULT. DEFAULT uses
the user/company preference for print output
? properties {string} [optional] - Name/value pairs used to configure the print operation.
Currently only supported for STATEMENT. Note that even if type is set to
STATEMENT, properties is still an optional argument.
? STATEMENT: openonly (T|F), statementdate, startdate

Returns
? nlobjFile object

Example

function printTrans()
{
//print the transaction to a PDF file object
var file = nlapiPrintRecord('TRANSACTION', 1799, 'DEFAULT', null);
//send the PDF as an attachment
nlapiSendEmail('-5', 'test@totemsuite.com', 'Incoming Transaction', 'Please see attached transaction', null,
null, null, file);
}


This sample shows how to create a PDF of a particular transaction. First the file variable is set
to a PDF file object. This PDF is then returned as an nlobjResponse object. The response object
content type is set to PDF (using the nlobjFile.getType() method). Finally, the the output of
the response object is written to the server.
var file = nlapiPrintRecord('TRANSACTION', 1799, 'PDF', null);
response.setContentType(file.getType());
response.write(file.getValue());

直接在服务端生成相应的文件来给用户下载等动作的思路:

nlobjFile
Primary object used to encapsulate files (attachments). Note that the following functions
return a reference to this object:
? nlapiCreateFile(name, type, contents)//用来创建文件
? nlapiLoadFile(id)
? nlapiMergeRecord(id, baseType, baseId, altType, altId, fields)
? nlapiPrintRecord(type, id, mode, properties)
nlobjFile Methods
? "getId()" on page 1
? "getName()" on page 1
? "getSize()" on page 1
? "getType()" on page 1
? "getURL()" on page 1
? "getValue()" on page 1
? "setName(name)" on page 1

nlapiCreateFile(name, type, contents) //服务端专用
Instantiates and returns an nlobjFile object.
Use this API to create ad-hoc, non-binary virtual files for use as email or fax attachments. Note
that ad-hoc attachments created using nlapiCreateFile are "virtual" documents/attachments
They are not actual file objects that can be stored in the NetSuite file cabinet.
The nlapiCreateFile API can also be used for streaming to clients (via Suitelets). For streaming
or attaching binary content, you can call the following:
? nlapiLoadRecord(type, id)
? nlapiPrintRecord(type, id, mode, properties)
? nlapiMergeRecord(id, baseType, baseId, altType, altId, fields)
Each of these APIs can load or generate binary content.
Important: nlapiCreateFile is a server-side-only function.
Parameters
? name {string} [required] - The name of the file
? type {string} [required] - The file type. For a list of supported file types, see Supported
File Types in the SuiteScript Reference Guide. Note that when specifiying the type for
an ad-hoc email or fax attachment, only non-binary types are supported (for example
PLAINTEXT, HTML, XML)
? contents {string} [required] - The contents of the file

function sendAttachment()
{
var newAttachment = nlapiCreateFile('helloworld.txt', 'PLAINTEXT', 'Hello World\nHello World');

var pdfcontents = nlapiMergeTemplate(.....)
var fileObj = nlapiCreateFile('mypdf.pdf', 'PDF', pdfcontents)

原文地址:https://www.cnblogs.com/backuper/p/1350230.html