No Description

buttons.html5.js 43KB

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