以下是我的代码:
define('mybiz.framework.csvsave',
['jquery', 'mybiz.framework.loggingservice', 'saveAs'], function ($,
JL, saveAs) {
'use strict';
var logger = JL('mybiz.framework.csvsave');
var generate = function (doc) {
logger.info("about to save CSV document");
var startTime = new Date();
try {
var textEncoder = new TextEncoder('utf-16le');
var csvContentEncoded = textEncoder.encode([doc]);
var bom = new Uint8Array([0xFF, 0xFE]);
var out = new Uint8Array(bom.byteLength +csvContentEncoded.byteLength);
out.set(bom, 0);
out.set(csvContentEncoded, bom.byteLength);
var csvBlob = new Blob([out], {type: 'text/csv'});
saveAs(csvBlob, "los.csv");
} catch (e) {
logger.error("Error generating CSV: " + e.message + "\n" + e.stack);
}
var stopTime = new Date();
logger.info("CSV generation completed in: " + (stopTime - startTime) + "ms");
};
return {
generate: generate
};
});
以下代码调用上面的函数:
self.saveExportCSV = function (data, event) {
var columns = getColumnsToExport(self.columnsForExport());
self.exportCSV({'columns': columns, 'generator':
csvsave.generate, event: event});
};