buttons.html5.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. /*!
  2. * HTML5 export buttons for Buttons and DataTables.
  3. * 2016 SpryMedia Ltd - datatables.net/license
  4. *
  5. * FileSaver.js (1.3.3) - MIT license
  6. * Copyright © 2016 Eli Grey - http://eligrey.com
  7. */
  8. (function( factory ){
  9. if ( typeof define === 'function' && define.amd ) {
  10. // AMD
  11. define( ['jquery', 'datatables.net', 'datatables.net-buttons'], function ( $ ) {
  12. return factory( $, window, document );
  13. } );
  14. }
  15. else if ( typeof exports === 'object' ) {
  16. // CommonJS
  17. module.exports = function (root, $, jszip, pdfmake) {
  18. if ( ! root ) {
  19. root = window;
  20. }
  21. if ( ! $ || ! $.fn.dataTable ) {
  22. $ = require('datatables.net')(root, $).$;
  23. }
  24. if ( ! $.fn.dataTable.Buttons ) {
  25. require('datatables.net-buttons')(root, $);
  26. }
  27. return factory( $, root, root.document, jszip, pdfmake );
  28. };
  29. }
  30. else {
  31. // Browser
  32. factory( jQuery, window, document );
  33. }
  34. }(function( $, window, document, jszip, pdfmake, undefined ) {
  35. 'use strict';
  36. var DataTable = $.fn.dataTable;
  37. // Allow the constructor to pass in JSZip and PDFMake from external requires.
  38. // Otherwise, use globally defined variables, if they are available.
  39. function _jsZip () {
  40. return jszip || window.JSZip;
  41. }
  42. function _pdfMake () {
  43. return pdfmake || window.pdfMake;
  44. }
  45. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  46. * FileSaver.js dependency
  47. */
  48. /*jslint bitwise: true, indent: 4, laxbreak: true, laxcomma: true, smarttabs: true, plusplus: true */
  49. var _saveAs = (function(view) {
  50. "use strict";
  51. // IE <10 is explicitly unsupported
  52. if (typeof view === "undefined" || typeof navigator !== "undefined" && /MSIE [1-9]\./.test(navigator.userAgent)) {
  53. return;
  54. }
  55. var
  56. doc = view.document
  57. // only get URL when necessary in case Blob.js hasn't overridden it yet
  58. , get_URL = function() {
  59. return view.URL || view.webkitURL || view;
  60. }
  61. , save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a")
  62. , can_use_save_link = "download" in save_link
  63. , click = function(node) {
  64. var event = new MouseEvent("click");
  65. node.dispatchEvent(event);
  66. }
  67. , is_safari = /constructor/i.test(view.HTMLElement) || view.safari
  68. , is_chrome_ios =/CriOS\/[\d]+/.test(navigator.userAgent)
  69. , throw_outside = function(ex) {
  70. (view.setImmediate || view.setTimeout)(function() {
  71. throw ex;
  72. }, 0);
  73. }
  74. , force_saveable_type = "application/octet-stream"
  75. // the Blob API is fundamentally broken as there is no "downloadfinished" event to subscribe to
  76. , arbitrary_revoke_timeout = 1000 * 40 // in ms
  77. , revoke = function(file) {
  78. var revoker = function() {
  79. if (typeof file === "string") { // file is an object URL
  80. get_URL().revokeObjectURL(file);
  81. } else { // file is a File
  82. file.remove();
  83. }
  84. };
  85. setTimeout(revoker, arbitrary_revoke_timeout);
  86. }
  87. , dispatch = function(filesaver, event_types, event) {
  88. event_types = [].concat(event_types);
  89. var i = event_types.length;
  90. while (i--) {
  91. var listener = filesaver["on" + event_types[i]];
  92. if (typeof listener === "function") {
  93. try {
  94. listener.call(filesaver, event || filesaver);
  95. } catch (ex) {
  96. throw_outside(ex);
  97. }
  98. }
  99. }
  100. }
  101. , auto_bom = function(blob) {
  102. // prepend BOM for UTF-8 XML and text/* types (including HTML)
  103. // note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
  104. if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
  105. return new Blob([String.fromCharCode(0xFEFF), blob], {type: blob.type});
  106. }
  107. return blob;
  108. }
  109. , FileSaver = function(blob, name, no_auto_bom) {
  110. if (!no_auto_bom) {
  111. blob = auto_bom(blob);
  112. }
  113. // First try a.download, then web filesystem, then object URLs
  114. var
  115. filesaver = this
  116. , type = blob.type
  117. , force = type === force_saveable_type
  118. , object_url
  119. , dispatch_all = function() {
  120. dispatch(filesaver, "writestart progress write writeend".split(" "));
  121. }
  122. // on any filesys errors revert to saving with object URLs
  123. , fs_error = function() {
  124. if ((is_chrome_ios || (force && is_safari)) && view.FileReader) {
  125. // Safari doesn't allow downloading of blob urls
  126. var reader = new FileReader();
  127. reader.onloadend = function() {
  128. var url = is_chrome_ios ? reader.result : reader.result.replace(/^data:[^;]*;/, 'data:attachment/file;');
  129. var popup = view.open(url, '_blank');
  130. if(!popup) view.location.href = url;
  131. url=undefined; // release reference before dispatching
  132. filesaver.readyState = filesaver.DONE;
  133. dispatch_all();
  134. };
  135. reader.readAsDataURL(blob);
  136. filesaver.readyState = filesaver.INIT;
  137. return;
  138. }
  139. // don't create more object URLs than needed
  140. if (!object_url) {
  141. object_url = get_URL().createObjectURL(blob);
  142. }
  143. if (force) {
  144. view.location.href = object_url;
  145. } else {
  146. var opened = view.open(object_url, "_blank");
  147. if (!opened) {
  148. // Apple does not allow window.open, see https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/WorkingwithWindowsandTabs/WorkingwithWindowsandTabs.html
  149. view.location.href = object_url;
  150. }
  151. }
  152. filesaver.readyState = filesaver.DONE;
  153. dispatch_all();
  154. revoke(object_url);
  155. }
  156. ;
  157. filesaver.readyState = filesaver.INIT;
  158. if (can_use_save_link) {
  159. object_url = get_URL().createObjectURL(blob);
  160. setTimeout(function() {
  161. save_link.href = object_url;
  162. save_link.download = name;
  163. click(save_link);
  164. dispatch_all();
  165. revoke(object_url);
  166. filesaver.readyState = filesaver.DONE;
  167. });
  168. return;
  169. }
  170. fs_error();
  171. }
  172. , FS_proto = FileSaver.prototype
  173. , saveAs = function(blob, name, no_auto_bom) {
  174. return new FileSaver(blob, name || blob.name || "download", no_auto_bom);
  175. }
  176. ;
  177. // IE 10+ (native saveAs)
  178. if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) {
  179. return function(blob, name, no_auto_bom) {
  180. name = name || blob.name || "download";
  181. if (!no_auto_bom) {
  182. blob = auto_bom(blob);
  183. }
  184. return navigator.msSaveOrOpenBlob(blob, name);
  185. };
  186. }
  187. FS_proto.abort = function(){};
  188. FS_proto.readyState = FS_proto.INIT = 0;
  189. FS_proto.WRITING = 1;
  190. FS_proto.DONE = 2;
  191. FS_proto.error =
  192. FS_proto.onwritestart =
  193. FS_proto.onprogress =
  194. FS_proto.onwrite =
  195. FS_proto.onabort =
  196. FS_proto.onerror =
  197. FS_proto.onwriteend =
  198. null;
  199. return saveAs;
  200. }(
  201. typeof self !== "undefined" && self
  202. || typeof window !== "undefined" && window
  203. || this.content
  204. ));
  205. // Expose file saver on the DataTables API. Can't attach to `DataTables.Buttons`
  206. // since this file can be loaded before Button's core!
  207. DataTable.fileSave = _saveAs;
  208. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  209. * Local (private) functions
  210. */
  211. /**
  212. * Get the sheet name for Excel exports.
  213. *
  214. * @param {object} config Button configuration
  215. */
  216. var _sheetname = function ( config )
  217. {
  218. var sheetName = 'Sheet1';
  219. if ( config.sheetName ) {
  220. sheetName = config.sheetName.replace(/[\[\]\*\/\\\?\:]/g, '');
  221. }
  222. return sheetName;
  223. };
  224. /**
  225. * Get the newline character(s)
  226. *
  227. * @param {object} config Button configuration
  228. * @return {string} Newline character
  229. */
  230. var _newLine = function ( config )
  231. {
  232. return config.newline ?
  233. config.newline :
  234. navigator.userAgent.match(/Windows/) ?
  235. '\r\n' :
  236. '\n';
  237. };
  238. /**
  239. * Combine the data from the `buttons.exportData` method into a string that
  240. * will be used in the export file.
  241. *
  242. * @param {DataTable.Api} dt DataTables API instance
  243. * @param {object} config Button configuration
  244. * @return {object} The data to export
  245. */
  246. var _exportData = function ( dt, config )
  247. {
  248. var newLine = _newLine( config );
  249. var data = dt.buttons.exportData( config.exportOptions );
  250. var boundary = config.fieldBoundary;
  251. var separator = config.fieldSeparator;
  252. var reBoundary = new RegExp( boundary, 'g' );
  253. var escapeChar = config.escapeChar !== undefined ?
  254. config.escapeChar :
  255. '\\';
  256. var join = function ( a ) {
  257. var s = '';
  258. // If there is a field boundary, then we might need to escape it in
  259. // the source data
  260. for ( var i=0, ien=a.length ; i<ien ; i++ ) {
  261. if ( i > 0 ) {
  262. s += separator;
  263. }
  264. s += boundary ?
  265. boundary + ('' + a[i]).replace( reBoundary, escapeChar+boundary ) + boundary :
  266. a[i];
  267. }
  268. return s;
  269. };
  270. var header = config.header ? join( data.header )+newLine : '';
  271. var footer = config.footer && data.footer ? newLine+join( data.footer ) : '';
  272. var body = [];
  273. for ( var i=0, ien=data.body.length ; i<ien ; i++ ) {
  274. body.push( join( data.body[i] ) );
  275. }
  276. return {
  277. str: header + body.join( newLine ) + footer,
  278. rows: body.length
  279. };
  280. };
  281. /**
  282. * Older versions of Safari (prior to tech preview 18) don't support the
  283. * download option required.
  284. *
  285. * @return {Boolean} `true` if old Safari
  286. */
  287. var _isDuffSafari = function ()
  288. {
  289. var safari = navigator.userAgent.indexOf('Safari') !== -1 &&
  290. navigator.userAgent.indexOf('Chrome') === -1 &&
  291. navigator.userAgent.indexOf('Opera') === -1;
  292. if ( ! safari ) {
  293. return false;
  294. }
  295. var version = navigator.userAgent.match( /AppleWebKit\/(\d+\.\d+)/ );
  296. if ( version && version.length > 1 && version[1]*1 < 603.1 ) {
  297. return true;
  298. }
  299. return false;
  300. };
  301. /**
  302. * Convert from numeric position to letter for column names in Excel
  303. * @param {int} n Column number
  304. * @return {string} Column letter(s) name
  305. */
  306. function createCellPos( n ){
  307. var ordA = 'A'.charCodeAt(0);
  308. var ordZ = 'Z'.charCodeAt(0);
  309. var len = ordZ - ordA + 1;
  310. var s = "";
  311. while( n >= 0 ) {
  312. s = String.fromCharCode(n % len + ordA) + s;
  313. n = Math.floor(n / len) - 1;
  314. }
  315. return s;
  316. }
  317. try {
  318. var _serialiser = new XMLSerializer();
  319. var _ieExcel;
  320. }
  321. catch (t) {}
  322. /**
  323. * Recursively add XML files from an object's structure to a ZIP file. This
  324. * allows the XSLX file to be easily defined with an object's structure matching
  325. * the files structure.
  326. *
  327. * @param {JSZip} zip ZIP package
  328. * @param {object} obj Object to add (recursive)
  329. */
  330. function _addToZip( zip, obj ) {
  331. if ( _ieExcel === undefined ) {
  332. // Detect if we are dealing with IE's _awful_ serialiser by seeing if it
  333. // drop attributes
  334. _ieExcel = _serialiser
  335. .serializeToString(
  336. $.parseXML( excelStrings['xl/worksheets/sheet1.xml'] )
  337. )
  338. .indexOf( 'xmlns:r' ) === -1;
  339. }
  340. $.each( obj, function ( name, val ) {
  341. if ( $.isPlainObject( val ) ) {
  342. var newDir = zip.folder( name );
  343. _addToZip( newDir, val );
  344. }
  345. else {
  346. if ( _ieExcel ) {
  347. // IE's XML serialiser will drop some name space attributes from
  348. // from the root node, so we need to save them. Do this by
  349. // replacing the namespace nodes with a regular attribute that
  350. // we convert back when serialised. Edge does not have this
  351. // issue
  352. var worksheet = val.childNodes[0];
  353. var i, ien;
  354. var attrs = [];
  355. for ( i=worksheet.attributes.length-1 ; i>=0 ; i-- ) {
  356. var attrName = worksheet.attributes[i].nodeName;
  357. var attrValue = worksheet.attributes[i].nodeValue;
  358. if ( attrName.indexOf( ':' ) !== -1 ) {
  359. attrs.push( { name: attrName, value: attrValue } );
  360. worksheet.removeAttribute( attrName );
  361. }
  362. }
  363. for ( i=0, ien=attrs.length ; i<ien ; i++ ) {
  364. var attr = val.createAttribute( attrs[i].name.replace( ':', '_dt_b_namespace_token_' ) );
  365. attr.value = attrs[i].value;
  366. worksheet.setAttributeNode( attr );
  367. }
  368. }
  369. var str = _serialiser.serializeToString(val);
  370. // Fix IE's XML
  371. if ( _ieExcel ) {
  372. // IE doesn't include the XML declaration
  373. if ( str.indexOf( '<?xml' ) === -1 ) {
  374. str = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+str;
  375. }
  376. // Return namespace attributes to being as such
  377. str = str.replace( /_dt_b_namespace_token_/g, ':' );
  378. }
  379. // Safari, IE and Edge will put empty name space attributes onto
  380. // various elements making them useless. This strips them out
  381. str = str.replace( /<([^<>]*?) xmlns=""([^<>]*?)>/g, '<$1 $2>' );
  382. zip.file( name, str );
  383. }
  384. } );
  385. }
  386. /**
  387. * Create an XML node and add any children, attributes, etc without needing to
  388. * be verbose in the DOM.
  389. *
  390. * @param {object} doc XML document
  391. * @param {string} nodeName Node name
  392. * @param {object} opts Options - can be `attr` (attributes), `children`
  393. * (child nodes) and `text` (text content)
  394. * @return {node} Created node
  395. */
  396. function _createNode( doc, nodeName, opts ) {
  397. var tempNode = doc.createElement( nodeName );
  398. if ( opts ) {
  399. if ( opts.attr ) {
  400. $(tempNode).attr( opts.attr );
  401. }
  402. if ( opts.children ) {
  403. $.each( opts.children, function ( key, value ) {
  404. tempNode.appendChild( value );
  405. } );
  406. }
  407. if ( opts.text !== null && opts.text !== undefined ) {
  408. tempNode.appendChild( doc.createTextNode( opts.text ) );
  409. }
  410. }
  411. return tempNode;
  412. }
  413. /**
  414. * Get the width for an Excel column based on the contents of that column
  415. * @param {object} data Data for export
  416. * @param {int} col Column index
  417. * @return {int} Column width
  418. */
  419. function _excelColWidth( data, col ) {
  420. var max = data.header[col].length;
  421. var len, lineSplit, str;
  422. if ( data.footer && data.footer[col].length > max ) {
  423. max = data.footer[col].length;
  424. }
  425. for ( var i=0, ien=data.body.length ; i<ien ; i++ ) {
  426. var point = data.body[i][col];
  427. str = point !== null && point !== undefined ?
  428. point.toString() :
  429. '';
  430. // If there is a newline character, workout the width of the column
  431. // based on the longest line in the string
  432. if ( str.indexOf('\n') !== -1 ) {
  433. lineSplit = str.split('\n');
  434. lineSplit.sort( function (a, b) {
  435. return b.length - a.length;
  436. } );
  437. len = lineSplit[0].length;
  438. }
  439. else {
  440. len = str.length;
  441. }
  442. if ( len > max ) {
  443. max = len;
  444. }
  445. // Max width rather than having potentially massive column widths
  446. if ( max > 40 ) {
  447. return 52; // 40 * 1.3
  448. }
  449. }
  450. max *= 1.3;
  451. // And a min width
  452. return max > 6 ? max : 6;
  453. }
  454. // Excel - Pre-defined strings to build a basic XLSX file
  455. var excelStrings = {
  456. "_rels/.rels":
  457. '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
  458. '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'+
  459. '<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>'+
  460. '</Relationships>',
  461. "xl/_rels/workbook.xml.rels":
  462. '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
  463. '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'+
  464. '<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>'+
  465. '<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>'+
  466. '</Relationships>',
  467. "[Content_Types].xml":
  468. '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
  469. '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">'+
  470. '<Default Extension="xml" ContentType="application/xml" />'+
  471. '<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" />'+
  472. '<Default Extension="jpeg" ContentType="image/jpeg" />'+
  473. '<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml" />'+
  474. '<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" />'+
  475. '<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml" />'+
  476. '</Types>',
  477. "xl/workbook.xml":
  478. '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
  479. '<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">'+
  480. '<fileVersion appName="xl" lastEdited="5" lowestEdited="5" rupBuild="24816"/>'+
  481. '<workbookPr showInkAnnotation="0" autoCompressPictures="0"/>'+
  482. '<bookViews>'+
  483. '<workbookView xWindow="0" yWindow="0" windowWidth="25600" windowHeight="19020" tabRatio="500"/>'+
  484. '</bookViews>'+
  485. '<sheets>'+
  486. '<sheet name="" sheetId="1" r:id="rId1"/>'+
  487. '</sheets>'+
  488. '</workbook>',
  489. "xl/worksheets/sheet1.xml":
  490. '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
  491. '<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">'+
  492. '<sheetData/>'+
  493. '<mergeCells count="0"/>'+
  494. '</worksheet>',
  495. "xl/styles.xml":
  496. '<?xml version="1.0" encoding="UTF-8"?>'+
  497. '<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">'+
  498. '<numFmts count="6">'+
  499. '<numFmt numFmtId="164" formatCode="#,##0.00_-\ [$$-45C]"/>'+
  500. '<numFmt numFmtId="165" formatCode="&quot;£&quot;#,##0.00"/>'+
  501. '<numFmt numFmtId="166" formatCode="[$€-2]\ #,##0.00"/>'+
  502. '<numFmt numFmtId="167" formatCode="0.0%"/>'+
  503. '<numFmt numFmtId="168" formatCode="#,##0;(#,##0)"/>'+
  504. '<numFmt numFmtId="169" formatCode="#,##0.00;(#,##0.00)"/>'+
  505. '</numFmts>'+
  506. '<fonts count="5" x14ac:knownFonts="1">'+
  507. '<font>'+
  508. '<sz val="11" />'+
  509. '<name val="Calibri" />'+
  510. '</font>'+
  511. '<font>'+
  512. '<sz val="11" />'+
  513. '<name val="Calibri" />'+
  514. '<color rgb="FFFFFFFF" />'+
  515. '</font>'+
  516. '<font>'+
  517. '<sz val="11" />'+
  518. '<name val="Calibri" />'+
  519. '<b />'+
  520. '</font>'+
  521. '<font>'+
  522. '<sz val="11" />'+
  523. '<name val="Calibri" />'+
  524. '<i />'+
  525. '</font>'+
  526. '<font>'+
  527. '<sz val="11" />'+
  528. '<name val="Calibri" />'+
  529. '<u />'+
  530. '</font>'+
  531. '</fonts>'+
  532. '<fills count="6">'+
  533. '<fill>'+
  534. '<patternFill patternType="none" />'+
  535. '</fill>'+
  536. '<fill/>'+ // Excel appears to use this as a dotted background regardless of values
  537. '<fill>'+
  538. '<patternFill patternType="solid">'+
  539. '<fgColor rgb="FFD9D9D9" />'+
  540. '<bgColor indexed="64" />'+
  541. '</patternFill>'+
  542. '</fill>'+
  543. '<fill>'+
  544. '<patternFill patternType="solid">'+
  545. '<fgColor rgb="FFD99795" />'+
  546. '<bgColor indexed="64" />'+
  547. '</patternFill>'+
  548. '</fill>'+
  549. '<fill>'+
  550. '<patternFill patternType="solid">'+
  551. '<fgColor rgb="ffc6efce" />'+
  552. '<bgColor indexed="64" />'+
  553. '</patternFill>'+
  554. '</fill>'+
  555. '<fill>'+
  556. '<patternFill patternType="solid">'+
  557. '<fgColor rgb="ffc6cfef" />'+
  558. '<bgColor indexed="64" />'+
  559. '</patternFill>'+
  560. '</fill>'+
  561. '</fills>'+
  562. '<borders count="2">'+
  563. '<border>'+
  564. '<left />'+
  565. '<right />'+
  566. '<top />'+
  567. '<bottom />'+
  568. '<diagonal />'+
  569. '</border>'+
  570. '<border diagonalUp="false" diagonalDown="false">'+
  571. '<left style="thin">'+
  572. '<color auto="1" />'+
  573. '</left>'+
  574. '<right style="thin">'+
  575. '<color auto="1" />'+
  576. '</right>'+
  577. '<top style="thin">'+
  578. '<color auto="1" />'+
  579. '</top>'+
  580. '<bottom style="thin">'+
  581. '<color auto="1" />'+
  582. '</bottom>'+
  583. '<diagonal />'+
  584. '</border>'+
  585. '</borders>'+
  586. '<cellStyleXfs count="1">'+
  587. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" />'+
  588. '</cellStyleXfs>'+
  589. '<cellXfs count="67">'+
  590. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  591. '<xf numFmtId="0" fontId="1" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  592. '<xf numFmtId="0" fontId="2" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  593. '<xf numFmtId="0" fontId="3" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  594. '<xf numFmtId="0" fontId="4" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  595. '<xf numFmtId="0" fontId="0" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  596. '<xf numFmtId="0" fontId="1" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  597. '<xf numFmtId="0" fontId="2" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  598. '<xf numFmtId="0" fontId="3" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  599. '<xf numFmtId="0" fontId="4" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  600. '<xf numFmtId="0" fontId="0" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  601. '<xf numFmtId="0" fontId="1" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  602. '<xf numFmtId="0" fontId="2" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  603. '<xf numFmtId="0" fontId="3" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  604. '<xf numFmtId="0" fontId="4" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  605. '<xf numFmtId="0" fontId="0" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  606. '<xf numFmtId="0" fontId="1" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  607. '<xf numFmtId="0" fontId="2" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  608. '<xf numFmtId="0" fontId="3" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  609. '<xf numFmtId="0" fontId="4" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  610. '<xf numFmtId="0" fontId="0" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  611. '<xf numFmtId="0" fontId="1" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  612. '<xf numFmtId="0" fontId="2" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  613. '<xf numFmtId="0" fontId="3" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  614. '<xf numFmtId="0" fontId="4" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  615. '<xf numFmtId="0" fontId="0" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  616. '<xf numFmtId="0" fontId="1" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  617. '<xf numFmtId="0" fontId="2" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  618. '<xf numFmtId="0" fontId="3" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  619. '<xf numFmtId="0" fontId="4" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  620. '<xf numFmtId="0" fontId="0" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  621. '<xf numFmtId="0" fontId="1" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  622. '<xf numFmtId="0" fontId="2" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  623. '<xf numFmtId="0" fontId="3" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  624. '<xf numFmtId="0" fontId="4" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  625. '<xf numFmtId="0" fontId="0" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  626. '<xf numFmtId="0" fontId="1" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  627. '<xf numFmtId="0" fontId="2" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  628. '<xf numFmtId="0" fontId="3" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  629. '<xf numFmtId="0" fontId="4" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  630. '<xf numFmtId="0" fontId="0" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  631. '<xf numFmtId="0" fontId="1" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  632. '<xf numFmtId="0" fontId="2" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  633. '<xf numFmtId="0" fontId="3" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  634. '<xf numFmtId="0" fontId="4" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  635. '<xf numFmtId="0" fontId="0" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  636. '<xf numFmtId="0" fontId="1" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  637. '<xf numFmtId="0" fontId="2" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  638. '<xf numFmtId="0" fontId="3" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  639. '<xf numFmtId="0" fontId="4" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  640. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
  641. '<alignment horizontal="left"/>'+
  642. '</xf>'+
  643. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
  644. '<alignment horizontal="center"/>'+
  645. '</xf>'+
  646. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
  647. '<alignment horizontal="right"/>'+
  648. '</xf>'+
  649. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
  650. '<alignment horizontal="fill"/>'+
  651. '</xf>'+
  652. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
  653. '<alignment textRotation="90"/>'+
  654. '</xf>'+
  655. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
  656. '<alignment wrapText="1"/>'+
  657. '</xf>'+
  658. '<xf numFmtId="9" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  659. '<xf numFmtId="164" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  660. '<xf numFmtId="165" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  661. '<xf numFmtId="166" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  662. '<xf numFmtId="167" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  663. '<xf numFmtId="168" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  664. '<xf numFmtId="169" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  665. '<xf numFmtId="3" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  666. '<xf numFmtId="4" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  667. '<xf numFmtId="1" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  668. '<xf numFmtId="2" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  669. '</cellXfs>'+
  670. '<cellStyles count="1">'+
  671. '<cellStyle name="Normal" xfId="0" builtinId="0" />'+
  672. '</cellStyles>'+
  673. '<dxfs count="0" />'+
  674. '<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleMedium4" />'+
  675. '</styleSheet>'
  676. };
  677. // Note we could use 3 `for` loops for the styles, but when gzipped there is
  678. // virtually no difference in size, since the above can be easily compressed
  679. // Pattern matching for special number formats. Perhaps this should be exposed
  680. // via an API in future?
  681. // Ref: section 3.8.30 - built in formatters in open spreadsheet
  682. // https://www.ecma-international.org/news/TC45_current_work/Office%20Open%20XML%20Part%204%20-%20Markup%20Language%20Reference.pdf
  683. var _excelSpecials = [
  684. { match: /^\-?\d+\.\d%$/, style: 60, fmt: function (d) { return d/100; } }, // Precent with d.p.
  685. { match: /^\-?\d+\.?\d*%$/, style: 56, fmt: function (d) { return d/100; } }, // Percent
  686. { match: /^\-?\$[\d,]+.?\d*$/, style: 57 }, // Dollars
  687. { match: /^\-?£[\d,]+.?\d*$/, style: 58 }, // Pounds
  688. { match: /^\-?€[\d,]+.?\d*$/, style: 59 }, // Euros
  689. { match: /^\-?\d+$/, style: 65 }, // Numbers without thousand separators
  690. { match: /^\-?\d+\.\d{2}$/, style: 66 }, // Numbers 2 d.p. without thousands separators
  691. { match: /^\([\d,]+\)$/, style: 61, fmt: function (d) { return -1 * d.replace(/[\(\)]/g, ''); } }, // Negative numbers indicated by brackets
  692. { match: /^\([\d,]+\.\d{2}\)$/, style: 62, fmt: function (d) { return -1 * d.replace(/[\(\)]/g, ''); } }, // Negative numbers indicated by brackets - 2d.p.
  693. { match: /^\-?[\d,]+$/, style: 63 }, // Numbers with thousand separators
  694. { match: /^\-?[\d,]+\.\d{2}$/, style: 64 } // Numbers with 2 d.p. and thousands separators
  695. ];
  696. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  697. * Buttons
  698. */
  699. //
  700. // Copy to clipboard
  701. //
  702. DataTable.ext.buttons.copyHtml5 = {
  703. className: 'buttons-copy buttons-html5',
  704. text: function ( dt ) {
  705. return dt.i18n( 'buttons.copy', 'Copy' );
  706. },
  707. action: function ( e, dt, button, config ) {
  708. this.processing( true );
  709. var that = this;
  710. var exportData = _exportData( dt, config );
  711. var info = dt.buttons.exportInfo( config );
  712. var newline = _newLine(config);
  713. var output = exportData.str;
  714. var hiddenDiv = $('<div/>')
  715. .css( {
  716. height: 1,
  717. width: 1,
  718. overflow: 'hidden',
  719. position: 'fixed',
  720. top: 0,
  721. left: 0
  722. } );
  723. if ( info.title ) {
  724. output = info.title + newline + newline + output;
  725. }
  726. if ( info.messageTop ) {
  727. output = info.messageTop + newline + newline + output;
  728. }
  729. if ( info.messageBottom ) {
  730. output = output + newline + newline + info.messageBottom;
  731. }
  732. if ( config.customize ) {
  733. output = config.customize( output, config );
  734. }
  735. var textarea = $('<textarea readonly/>')
  736. .val( output )
  737. .appendTo( hiddenDiv );
  738. // For browsers that support the copy execCommand, try to use it
  739. if ( document.queryCommandSupported('copy') ) {
  740. hiddenDiv.appendTo( dt.table().container() );
  741. textarea[0].focus();
  742. textarea[0].select();
  743. try {
  744. var successful = document.execCommand( 'copy' );
  745. hiddenDiv.remove();
  746. if (successful) {
  747. dt.buttons.info(
  748. dt.i18n( 'buttons.copyTitle', 'Copy to clipboard' ),
  749. dt.i18n( 'buttons.copySuccess', {
  750. 1: 'Copied one row to clipboard',
  751. _: 'Copied %d rows to clipboard'
  752. }, exportData.rows ),
  753. 2000
  754. );
  755. this.processing( false );
  756. return;
  757. }
  758. }
  759. catch (t) {}
  760. }
  761. // Otherwise we show the text box and instruct the user to use it
  762. var message = $('<span>'+dt.i18n( 'buttons.copyKeys',
  763. 'Press <i>ctrl</i> or <i>\u2318</i> + <i>C</i> to copy the table data<br>to your system clipboard.<br><br>'+
  764. 'To cancel, click this message or press escape.' )+'</span>'
  765. )
  766. .append( hiddenDiv );
  767. dt.buttons.info( dt.i18n( 'buttons.copyTitle', 'Copy to clipboard' ), message, 0 );
  768. // Select the text so when the user activates their system clipboard
  769. // it will copy that text
  770. textarea[0].focus();
  771. textarea[0].select();
  772. // Event to hide the message when the user is done
  773. var container = $(message).closest('.dt-button-info');
  774. var close = function () {
  775. container.off( 'click.buttons-copy' );
  776. $(document).off( '.buttons-copy' );
  777. dt.buttons.info( false );
  778. };
  779. container.on( 'click.buttons-copy', close );
  780. $(document)
  781. .on( 'keydown.buttons-copy', function (e) {
  782. if ( e.keyCode === 27 ) { // esc
  783. close();
  784. that.processing( false );
  785. }
  786. } )
  787. .on( 'copy.buttons-copy cut.buttons-copy', function () {
  788. close();
  789. that.processing( false );
  790. } );
  791. },
  792. exportOptions: {},
  793. fieldSeparator: '\t',
  794. fieldBoundary: '',
  795. header: true,
  796. footer: false,
  797. title: '*',
  798. messageTop: '*',
  799. messageBottom: '*'
  800. };
  801. //
  802. // CSV export
  803. //
  804. DataTable.ext.buttons.csvHtml5 = {
  805. bom: false,
  806. className: 'buttons-csv buttons-html5',
  807. available: function () {
  808. return window.FileReader !== undefined && window.Blob;
  809. },
  810. text: function ( dt ) {
  811. return dt.i18n( 'buttons.csv', 'CSV' );
  812. },
  813. action: function ( e, dt, button, config ) {
  814. this.processing( true );
  815. // Set the text
  816. var output = _exportData( dt, config ).str;
  817. var info = dt.buttons.exportInfo(config);
  818. var charset = config.charset;
  819. if ( config.customize ) {
  820. output = config.customize( output, config );
  821. }
  822. if ( charset !== false ) {
  823. if ( ! charset ) {
  824. charset = document.characterSet || document.charset;
  825. }
  826. if ( charset ) {
  827. charset = ';charset='+charset;
  828. }
  829. }
  830. else {
  831. charset = '';
  832. }
  833. if ( config.bom ) {
  834. output = '\ufeff' + output;
  835. }
  836. _saveAs(
  837. new Blob( [output], {type: 'text/csv'+charset} ),
  838. info.filename,
  839. true
  840. );
  841. this.processing( false );
  842. },
  843. filename: '*',
  844. extension: '.csv',
  845. exportOptions: {},
  846. fieldSeparator: ',',
  847. fieldBoundary: '"',
  848. escapeChar: '"',
  849. charset: null,
  850. header: true,
  851. footer: false
  852. };
  853. //
  854. // Excel (xlsx) export
  855. //
  856. DataTable.ext.buttons.excelHtml5 = {
  857. className: 'buttons-excel buttons-html5',
  858. available: function () {
  859. return window.FileReader !== undefined && _jsZip() !== undefined && ! _isDuffSafari() && _serialiser;
  860. },
  861. text: function ( dt ) {
  862. return dt.i18n( 'buttons.excel', 'Excel' );
  863. },
  864. action: function ( e, dt, button, config ) {
  865. this.processing( true );
  866. var that = this;
  867. var rowPos = 0;
  868. var getXml = function ( type ) {
  869. var str = excelStrings[ type ];
  870. //str = str.replace( /xmlns:/g, 'xmlns_' ).replace( /mc:/g, 'mc_' );
  871. return $.parseXML( str );
  872. };
  873. var rels = getXml('xl/worksheets/sheet1.xml');
  874. var relsGet = rels.getElementsByTagName( "sheetData" )[0];
  875. var xlsx = {
  876. _rels: {
  877. ".rels": getXml('_rels/.rels')
  878. },
  879. xl: {
  880. _rels: {
  881. "workbook.xml.rels": getXml('xl/_rels/workbook.xml.rels')
  882. },
  883. "workbook.xml": getXml('xl/workbook.xml'),
  884. "styles.xml": getXml('xl/styles.xml'),
  885. "worksheets": {
  886. "sheet1.xml": rels
  887. }
  888. },
  889. "[Content_Types].xml": getXml('[Content_Types].xml')
  890. };
  891. var data = dt.buttons.exportData( config.exportOptions );
  892. var currentRow, rowNode;
  893. var addRow = function ( row ) {
  894. currentRow = rowPos+1;
  895. rowNode = _createNode( rels, "row", { attr: {r:currentRow} } );
  896. for ( var i=0, ien=row.length ; i<ien ; i++ ) {
  897. // Concat both the Cell Columns as a letter and the Row of the cell.
  898. var cellId = createCellPos(i) + '' + currentRow;
  899. var cell = null;
  900. // For null, undefined of blank cell, continue so it doesn't create the _createNode
  901. if ( row[i] === null || row[i] === undefined || row[i] === '' ) {
  902. continue;
  903. }
  904. row[i] = $.trim( row[i] );
  905. // Special number formatting options
  906. for ( var j=0, jen=_excelSpecials.length ; j<jen ; j++ ) {
  907. var special = _excelSpecials[j];
  908. // TODO Need to provide the ability for the specials to say
  909. // if they are returning a string, since at the moment it is
  910. // assumed to be a number
  911. if ( row[i].match && ! row[i].match(/^0\d+/) && row[i].match( special.match ) ) {
  912. var val = row[i].replace(/[^\d\.\-]/g, '');
  913. if ( special.fmt ) {
  914. val = special.fmt( val );
  915. }
  916. cell = _createNode( rels, 'c', {
  917. attr: {
  918. r: cellId,
  919. s: special.style
  920. },
  921. children: [
  922. _createNode( rels, 'v', { text: val } )
  923. ]
  924. } );
  925. break;
  926. }
  927. }
  928. if ( ! cell ) {
  929. if ( typeof row[i] === 'number' || (
  930. row[i].match &&
  931. row[i].match(/^-?\d+(\.\d+)?$/) &&
  932. ! row[i].match(/^0\d+/) )
  933. ) {
  934. // Detect numbers - don't match numbers with leading zeros
  935. // or a negative anywhere but the start
  936. cell = _createNode( rels, 'c', {
  937. attr: {
  938. t: 'n',
  939. r: cellId
  940. },
  941. children: [
  942. _createNode( rels, 'v', { text: row[i] } )
  943. ]
  944. } );
  945. }
  946. else {
  947. // String output - replace non standard characters for text output
  948. var text = ! row[i].replace ?
  949. row[i] :
  950. row[i].replace(/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F-\x9F]/g, '');
  951. cell = _createNode( rels, 'c', {
  952. attr: {
  953. t: 'inlineStr',
  954. r: cellId
  955. },
  956. children:{
  957. row: _createNode( rels, 'is', {
  958. children: {
  959. row: _createNode( rels, 't', {
  960. text: text
  961. } )
  962. }
  963. } )
  964. }
  965. } );
  966. }
  967. }
  968. rowNode.appendChild( cell );
  969. }
  970. relsGet.appendChild(rowNode);
  971. rowPos++;
  972. };
  973. $( 'sheets sheet', xlsx.xl['workbook.xml'] ).attr( 'name', _sheetname( config ) );
  974. if ( config.customizeData ) {
  975. config.customizeData( data );
  976. }
  977. var mergeCells = function ( row, colspan ) {
  978. var mergeCells = $('mergeCells', rels);
  979. mergeCells[0].appendChild( _createNode( rels, 'mergeCell', {
  980. attr: {
  981. ref: 'A'+row+':'+createCellPos(colspan)+row
  982. }
  983. } ) );
  984. mergeCells.attr( 'count', mergeCells.attr( 'count' )+1 );
  985. $('row:eq('+(row-1)+') c', rels).attr( 's', '51' ); // centre
  986. };
  987. // Title and top messages
  988. var exportInfo = dt.buttons.exportInfo( config );
  989. if ( exportInfo.title ) {
  990. addRow( [exportInfo.title], rowPos );
  991. mergeCells( rowPos, data.header.length-1 );
  992. }
  993. if ( exportInfo.messageTop ) {
  994. addRow( [exportInfo.messageTop], rowPos );
  995. mergeCells( rowPos, data.header.length-1 );
  996. }
  997. // Table itself
  998. if ( config.header ) {
  999. addRow( data.header, rowPos );
  1000. $('row:last c', rels).attr( 's', '2' ); // bold
  1001. }
  1002. for ( var n=0, ie=data.body.length ; n<ie ; n++ ) {
  1003. addRow( data.body[n], rowPos );
  1004. }
  1005. if ( config.footer && data.footer ) {
  1006. addRow( data.footer, rowPos);
  1007. $('row:last c', rels).attr( 's', '2' ); // bold
  1008. }
  1009. // Below the table
  1010. if ( exportInfo.messageBottom ) {
  1011. addRow( [exportInfo.messageBottom], rowPos );
  1012. mergeCells( rowPos, data.header.length-1 );
  1013. }
  1014. // Set column widths
  1015. var cols = _createNode( rels, 'cols' );
  1016. $('worksheet', rels).prepend( cols );
  1017. for ( var i=0, ien=data.header.length ; i<ien ; i++ ) {
  1018. cols.appendChild( _createNode( rels, 'col', {
  1019. attr: {
  1020. min: i+1,
  1021. max: i+1,
  1022. width: _excelColWidth( data, i ),
  1023. customWidth: 1
  1024. }
  1025. } ) );
  1026. }
  1027. // Let the developer customise the document if they want to
  1028. if ( config.customize ) {
  1029. config.customize( xlsx );
  1030. }
  1031. var jszip = _jsZip();
  1032. var zip = new jszip();
  1033. var zipConfig = {
  1034. type: 'blob',
  1035. mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  1036. };
  1037. _addToZip( zip, xlsx );
  1038. if ( zip.generateAsync ) {
  1039. // JSZip 3+
  1040. zip
  1041. .generateAsync( zipConfig )
  1042. .then( function ( blob ) {
  1043. _saveAs( blob, exportInfo.filename );
  1044. that.processing( false );
  1045. } );
  1046. }
  1047. else {
  1048. // JSZip 2.5
  1049. _saveAs(
  1050. zip.generate( zipConfig ),
  1051. exportInfo.filename
  1052. );
  1053. this.processing( false );
  1054. }
  1055. },
  1056. filename: '*',
  1057. extension: '.xlsx',
  1058. exportOptions: {},
  1059. header: true,
  1060. footer: false,
  1061. title: '*',
  1062. messageTop: '*',
  1063. messageBottom: '*'
  1064. };
  1065. //
  1066. // PDF export - using pdfMake - http://pdfmake.org
  1067. //
  1068. DataTable.ext.buttons.pdfHtml5 = {
  1069. className: 'buttons-pdf buttons-html5',
  1070. available: function () {
  1071. return window.FileReader !== undefined && _pdfMake();
  1072. },
  1073. text: function ( dt ) {
  1074. return dt.i18n( 'buttons.pdf', 'PDF' );
  1075. },
  1076. action: function ( e, dt, button, config ) {
  1077. this.processing( true );
  1078. var that = this;
  1079. var data = dt.buttons.exportData( config.exportOptions );
  1080. var info = dt.buttons.exportInfo( config );
  1081. var rows = [];
  1082. if ( config.header ) {
  1083. rows.push( $.map( data.header, function ( d ) {
  1084. return {
  1085. text: typeof d === 'string' ? d : d+'',
  1086. style: 'tableHeader'
  1087. };
  1088. } ) );
  1089. }
  1090. for ( var i=0, ien=data.body.length ; i<ien ; i++ ) {
  1091. rows.push( $.map( data.body[i], function ( d ) {
  1092. return {
  1093. text: typeof d === 'string' ? d : d+'',
  1094. style: i % 2 ? 'tableBodyEven' : 'tableBodyOdd'
  1095. };
  1096. } ) );
  1097. }
  1098. if ( config.footer && data.footer) {
  1099. rows.push( $.map( data.footer, function ( d ) {
  1100. return {
  1101. text: typeof d === 'string' ? d : d+'',
  1102. style: 'tableFooter'
  1103. };
  1104. } ) );
  1105. }
  1106. var doc = {
  1107. pageSize: config.pageSize,
  1108. pageOrientation: config.orientation,
  1109. content: [
  1110. {
  1111. table: {
  1112. headerRows: 1,
  1113. body: rows
  1114. },
  1115. layout: 'noBorders'
  1116. }
  1117. ],
  1118. styles: {
  1119. tableHeader: {
  1120. bold: true,
  1121. fontSize: 11,
  1122. color: 'white',
  1123. fillColor: '#2d4154',
  1124. alignment: 'center'
  1125. },
  1126. tableBodyEven: {},
  1127. tableBodyOdd: {
  1128. fillColor: '#f3f3f3'
  1129. },
  1130. tableFooter: {
  1131. bold: true,
  1132. fontSize: 11,
  1133. color: 'white',
  1134. fillColor: '#2d4154'
  1135. },
  1136. title: {
  1137. alignment: 'center',
  1138. fontSize: 15
  1139. },
  1140. message: {}
  1141. },
  1142. defaultStyle: {
  1143. fontSize: 10
  1144. }
  1145. };
  1146. if ( info.messageTop ) {
  1147. doc.content.unshift( {
  1148. text: info.messageTop,
  1149. style: 'message',
  1150. margin: [ 0, 0, 0, 12 ]
  1151. } );
  1152. }
  1153. if ( info.messageBottom ) {
  1154. doc.content.push( {
  1155. text: info.messageBottom,
  1156. style: 'message',
  1157. margin: [ 0, 0, 0, 12 ]
  1158. } );
  1159. }
  1160. if ( info.title ) {
  1161. doc.content.unshift( {
  1162. text: info.title,
  1163. style: 'title',
  1164. margin: [ 0, 0, 0, 12 ]
  1165. } );
  1166. }
  1167. if ( config.customize ) {
  1168. config.customize( doc, config );
  1169. }
  1170. var pdf = _pdfMake().createPdf( doc );
  1171. if ( config.download === 'open' && ! _isDuffSafari() ) {
  1172. pdf.open();
  1173. this.processing( false );
  1174. }
  1175. else {
  1176. pdf.getBuffer( function (buffer) {
  1177. var blob = new Blob( [buffer], {type:'application/pdf'} );
  1178. _saveAs( blob, info.filename );
  1179. that.processing( false );
  1180. } );
  1181. }
  1182. },
  1183. title: '*',
  1184. filename: '*',
  1185. extension: '.pdf',
  1186. exportOptions: {},
  1187. orientation: 'portrait',
  1188. pageSize: 'A4',
  1189. header: true,
  1190. footer: false,
  1191. messageTop: '*',
  1192. messageBottom: '*',
  1193. customize: null,
  1194. download: 'download'
  1195. };
  1196. return DataTable.Buttons;
  1197. }));