buttons.html5.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  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 but
  537. '<patternFill patternType="none" />'+ // to be valid to the schema, use a patternFill
  538. '</fill>'+
  539. '<fill>'+
  540. '<patternFill patternType="solid">'+
  541. '<fgColor rgb="FFD9D9D9" />'+
  542. '<bgColor indexed="64" />'+
  543. '</patternFill>'+
  544. '</fill>'+
  545. '<fill>'+
  546. '<patternFill patternType="solid">'+
  547. '<fgColor rgb="FFD99795" />'+
  548. '<bgColor indexed="64" />'+
  549. '</patternFill>'+
  550. '</fill>'+
  551. '<fill>'+
  552. '<patternFill patternType="solid">'+
  553. '<fgColor rgb="ffc6efce" />'+
  554. '<bgColor indexed="64" />'+
  555. '</patternFill>'+
  556. '</fill>'+
  557. '<fill>'+
  558. '<patternFill patternType="solid">'+
  559. '<fgColor rgb="ffc6cfef" />'+
  560. '<bgColor indexed="64" />'+
  561. '</patternFill>'+
  562. '</fill>'+
  563. '</fills>'+
  564. '<borders count="2">'+
  565. '<border>'+
  566. '<left />'+
  567. '<right />'+
  568. '<top />'+
  569. '<bottom />'+
  570. '<diagonal />'+
  571. '</border>'+
  572. '<border diagonalUp="false" diagonalDown="false">'+
  573. '<left style="thin">'+
  574. '<color auto="1" />'+
  575. '</left>'+
  576. '<right style="thin">'+
  577. '<color auto="1" />'+
  578. '</right>'+
  579. '<top style="thin">'+
  580. '<color auto="1" />'+
  581. '</top>'+
  582. '<bottom style="thin">'+
  583. '<color auto="1" />'+
  584. '</bottom>'+
  585. '<diagonal />'+
  586. '</border>'+
  587. '</borders>'+
  588. '<cellStyleXfs count="1">'+
  589. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" />'+
  590. '</cellStyleXfs>'+
  591. '<cellXfs count="67">'+
  592. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  593. '<xf numFmtId="0" fontId="1" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  594. '<xf numFmtId="0" fontId="2" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  595. '<xf numFmtId="0" fontId="3" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  596. '<xf numFmtId="0" fontId="4" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  597. '<xf numFmtId="0" fontId="0" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  598. '<xf numFmtId="0" fontId="1" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  599. '<xf numFmtId="0" fontId="2" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  600. '<xf numFmtId="0" fontId="3" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  601. '<xf numFmtId="0" fontId="4" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  602. '<xf numFmtId="0" fontId="0" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  603. '<xf numFmtId="0" fontId="1" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  604. '<xf numFmtId="0" fontId="2" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  605. '<xf numFmtId="0" fontId="3" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  606. '<xf numFmtId="0" fontId="4" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  607. '<xf numFmtId="0" fontId="0" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  608. '<xf numFmtId="0" fontId="1" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  609. '<xf numFmtId="0" fontId="2" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  610. '<xf numFmtId="0" fontId="3" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  611. '<xf numFmtId="0" fontId="4" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  612. '<xf numFmtId="0" fontId="0" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  613. '<xf numFmtId="0" fontId="1" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  614. '<xf numFmtId="0" fontId="2" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  615. '<xf numFmtId="0" fontId="3" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  616. '<xf numFmtId="0" fontId="4" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  617. '<xf numFmtId="0" fontId="0" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  618. '<xf numFmtId="0" fontId="1" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  619. '<xf numFmtId="0" fontId="2" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  620. '<xf numFmtId="0" fontId="3" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  621. '<xf numFmtId="0" fontId="4" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  622. '<xf numFmtId="0" fontId="0" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  623. '<xf numFmtId="0" fontId="1" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  624. '<xf numFmtId="0" fontId="2" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  625. '<xf numFmtId="0" fontId="3" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  626. '<xf numFmtId="0" fontId="4" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  627. '<xf numFmtId="0" fontId="0" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  628. '<xf numFmtId="0" fontId="1" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  629. '<xf numFmtId="0" fontId="2" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  630. '<xf numFmtId="0" fontId="3" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  631. '<xf numFmtId="0" fontId="4" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  632. '<xf numFmtId="0" fontId="0" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  633. '<xf numFmtId="0" fontId="1" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  634. '<xf numFmtId="0" fontId="2" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  635. '<xf numFmtId="0" fontId="3" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  636. '<xf numFmtId="0" fontId="4" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  637. '<xf numFmtId="0" fontId="0" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  638. '<xf numFmtId="0" fontId="1" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  639. '<xf numFmtId="0" fontId="2" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  640. '<xf numFmtId="0" fontId="3" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  641. '<xf numFmtId="0" fontId="4" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  642. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
  643. '<alignment horizontal="left"/>'+
  644. '</xf>'+
  645. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
  646. '<alignment horizontal="center"/>'+
  647. '</xf>'+
  648. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
  649. '<alignment horizontal="right"/>'+
  650. '</xf>'+
  651. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
  652. '<alignment horizontal="fill"/>'+
  653. '</xf>'+
  654. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
  655. '<alignment textRotation="90"/>'+
  656. '</xf>'+
  657. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
  658. '<alignment wrapText="1"/>'+
  659. '</xf>'+
  660. '<xf numFmtId="9" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  661. '<xf numFmtId="164" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  662. '<xf numFmtId="165" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  663. '<xf numFmtId="166" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  664. '<xf numFmtId="167" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  665. '<xf numFmtId="168" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  666. '<xf numFmtId="169" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  667. '<xf numFmtId="3" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  668. '<xf numFmtId="4" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  669. '<xf numFmtId="1" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  670. '<xf numFmtId="2" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  671. '</cellXfs>'+
  672. '<cellStyles count="1">'+
  673. '<cellStyle name="Normal" xfId="0" builtinId="0" />'+
  674. '</cellStyles>'+
  675. '<dxfs count="0" />'+
  676. '<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleMedium4" />'+
  677. '</styleSheet>'
  678. };
  679. // Note we could use 3 `for` loops for the styles, but when gzipped there is
  680. // virtually no difference in size, since the above can be easily compressed
  681. // Pattern matching for special number formats. Perhaps this should be exposed
  682. // via an API in future?
  683. // Ref: section 3.8.30 - built in formatters in open spreadsheet
  684. // https://www.ecma-international.org/news/TC45_current_work/Office%20Open%20XML%20Part%204%20-%20Markup%20Language%20Reference.pdf
  685. var _excelSpecials = [
  686. { match: /^\-?\d+\.\d%$/, style: 60, fmt: function (d) { return d/100; } }, // Precent with d.p.
  687. { match: /^\-?\d+\.?\d*%$/, style: 56, fmt: function (d) { return d/100; } }, // Percent
  688. { match: /^\-?\$[\d,]+.?\d*$/, style: 57 }, // Dollars
  689. { match: /^\-?£[\d,]+.?\d*$/, style: 58 }, // Pounds
  690. { match: /^\-?€[\d,]+.?\d*$/, style: 59 }, // Euros
  691. { match: /^\-?\d+$/, style: 65 }, // Numbers without thousand separators
  692. { match: /^\-?\d+\.\d{2}$/, style: 66 }, // Numbers 2 d.p. without thousands separators
  693. { match: /^\([\d,]+\)$/, style: 61, fmt: function (d) { return -1 * d.replace(/[\(\)]/g, ''); } }, // Negative numbers indicated by brackets
  694. { match: /^\([\d,]+\.\d{2}\)$/, style: 62, fmt: function (d) { return -1 * d.replace(/[\(\)]/g, ''); } }, // Negative numbers indicated by brackets - 2d.p.
  695. { match: /^\-?[\d,]+$/, style: 63 }, // Numbers with thousand separators
  696. { match: /^\-?[\d,]+\.\d{2}$/, style: 64 } // Numbers with 2 d.p. and thousands separators
  697. ];
  698. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  699. * Buttons
  700. */
  701. //
  702. // Copy to clipboard
  703. //
  704. DataTable.ext.buttons.copyHtml5 = {
  705. className: 'buttons-copy buttons-html5',
  706. text: function ( dt ) {
  707. return dt.i18n( 'buttons.copy', 'Copy' );
  708. },
  709. action: function ( e, dt, button, config ) {
  710. this.processing( true );
  711. var that = this;
  712. var exportData = _exportData( dt, config );
  713. var info = dt.buttons.exportInfo( config );
  714. var newline = _newLine(config);
  715. var output = exportData.str;
  716. var hiddenDiv = $('<div/>')
  717. .css( {
  718. height: 1,
  719. width: 1,
  720. overflow: 'hidden',
  721. position: 'fixed',
  722. top: 0,
  723. left: 0
  724. } );
  725. if ( info.title ) {
  726. output = info.title + newline + newline + output;
  727. }
  728. if ( info.messageTop ) {
  729. output = info.messageTop + newline + newline + output;
  730. }
  731. if ( info.messageBottom ) {
  732. output = output + newline + newline + info.messageBottom;
  733. }
  734. if ( config.customize ) {
  735. output = config.customize( output, config, dt );
  736. }
  737. var textarea = $('<textarea readonly/>')
  738. .val( output )
  739. .appendTo( hiddenDiv );
  740. // For browsers that support the copy execCommand, try to use it
  741. if ( document.queryCommandSupported('copy') ) {
  742. hiddenDiv.appendTo( dt.table().container() );
  743. textarea[0].focus();
  744. textarea[0].select();
  745. try {
  746. var successful = document.execCommand( 'copy' );
  747. hiddenDiv.remove();
  748. if (successful) {
  749. dt.buttons.info(
  750. dt.i18n( 'buttons.copyTitle', 'Copy to clipboard' ),
  751. dt.i18n( 'buttons.copySuccess', {
  752. 1: 'Copied one row to clipboard',
  753. _: 'Copied %d rows to clipboard'
  754. }, exportData.rows ),
  755. 2000
  756. );
  757. this.processing( false );
  758. return;
  759. }
  760. }
  761. catch (t) {}
  762. }
  763. // Otherwise we show the text box and instruct the user to use it
  764. var message = $('<span>'+dt.i18n( 'buttons.copyKeys',
  765. 'Press <i>ctrl</i> or <i>\u2318</i> + <i>C</i> to copy the table data<br>to your system clipboard.<br><br>'+
  766. 'To cancel, click this message or press escape.' )+'</span>'
  767. )
  768. .append( hiddenDiv );
  769. dt.buttons.info( dt.i18n( 'buttons.copyTitle', 'Copy to clipboard' ), message, 0 );
  770. // Select the text so when the user activates their system clipboard
  771. // it will copy that text
  772. textarea[0].focus();
  773. textarea[0].select();
  774. // Event to hide the message when the user is done
  775. var container = $(message).closest('.dt-button-info');
  776. var close = function () {
  777. container.off( 'click.buttons-copy' );
  778. $(document).off( '.buttons-copy' );
  779. dt.buttons.info( false );
  780. };
  781. container.on( 'click.buttons-copy', close );
  782. $(document)
  783. .on( 'keydown.buttons-copy', function (e) {
  784. if ( e.keyCode === 27 ) { // esc
  785. close();
  786. that.processing( false );
  787. }
  788. } )
  789. .on( 'copy.buttons-copy cut.buttons-copy', function () {
  790. close();
  791. that.processing( false );
  792. } );
  793. },
  794. exportOptions: {},
  795. fieldSeparator: '\t',
  796. fieldBoundary: '',
  797. header: true,
  798. footer: false,
  799. title: '*',
  800. messageTop: '*',
  801. messageBottom: '*'
  802. };
  803. //
  804. // CSV export
  805. //
  806. DataTable.ext.buttons.csvHtml5 = {
  807. bom: false,
  808. className: 'buttons-csv buttons-html5',
  809. available: function () {
  810. return window.FileReader !== undefined && window.Blob;
  811. },
  812. text: function ( dt ) {
  813. return dt.i18n( 'buttons.csv', 'CSV' );
  814. },
  815. action: function ( e, dt, button, config ) {
  816. this.processing( true );
  817. // Set the text
  818. var output = _exportData( dt, config ).str;
  819. var info = dt.buttons.exportInfo(config);
  820. var charset = config.charset;
  821. if ( config.customize ) {
  822. output = config.customize( output, config, dt );
  823. }
  824. if ( charset !== false ) {
  825. if ( ! charset ) {
  826. charset = document.characterSet || document.charset;
  827. }
  828. if ( charset ) {
  829. charset = ';charset='+charset;
  830. }
  831. }
  832. else {
  833. charset = '';
  834. }
  835. if ( config.bom ) {
  836. output = '\ufeff' + output;
  837. }
  838. _saveAs(
  839. new Blob( [output], {type: 'text/csv'+charset} ),
  840. info.filename,
  841. true
  842. );
  843. this.processing( false );
  844. },
  845. filename: '*',
  846. extension: '.csv',
  847. exportOptions: {},
  848. fieldSeparator: ',',
  849. fieldBoundary: '"',
  850. escapeChar: '"',
  851. charset: null,
  852. header: true,
  853. footer: false
  854. };
  855. //
  856. // Excel (xlsx) export
  857. //
  858. DataTable.ext.buttons.excelHtml5 = {
  859. className: 'buttons-excel buttons-html5',
  860. available: function () {
  861. return window.FileReader !== undefined && _jsZip() !== undefined && ! _isDuffSafari() && _serialiser;
  862. },
  863. text: function ( dt ) {
  864. return dt.i18n( 'buttons.excel', 'Excel' );
  865. },
  866. action: function ( e, dt, button, config ) {
  867. this.processing( true );
  868. var that = this;
  869. var rowPos = 0;
  870. var getXml = function ( type ) {
  871. var str = excelStrings[ type ];
  872. //str = str.replace( /xmlns:/g, 'xmlns_' ).replace( /mc:/g, 'mc_' );
  873. return $.parseXML( str );
  874. };
  875. var rels = getXml('xl/worksheets/sheet1.xml');
  876. var relsGet = rels.getElementsByTagName( "sheetData" )[0];
  877. var xlsx = {
  878. _rels: {
  879. ".rels": getXml('_rels/.rels')
  880. },
  881. xl: {
  882. _rels: {
  883. "workbook.xml.rels": getXml('xl/_rels/workbook.xml.rels')
  884. },
  885. "workbook.xml": getXml('xl/workbook.xml'),
  886. "styles.xml": getXml('xl/styles.xml'),
  887. "worksheets": {
  888. "sheet1.xml": rels
  889. }
  890. },
  891. "[Content_Types].xml": getXml('[Content_Types].xml')
  892. };
  893. var data = dt.buttons.exportData( config.exportOptions );
  894. var currentRow, rowNode;
  895. var addRow = function ( row ) {
  896. currentRow = rowPos+1;
  897. rowNode = _createNode( rels, "row", { attr: {r:currentRow} } );
  898. for ( var i=0, ien=row.length ; i<ien ; i++ ) {
  899. // Concat both the Cell Columns as a letter and the Row of the cell.
  900. var cellId = createCellPos(i) + '' + currentRow;
  901. var cell = null;
  902. // For null, undefined of blank cell, continue so it doesn't create the _createNode
  903. if ( row[i] === null || row[i] === undefined || row[i] === '' ) {
  904. if ( config.createEmptyCells === true ) {
  905. row[i] = '';
  906. }
  907. else {
  908. continue;
  909. }
  910. }
  911. var originalContent = row[i];
  912. row[i] = $.trim( row[i] );
  913. // Special number formatting options
  914. for ( var j=0, jen=_excelSpecials.length ; j<jen ; j++ ) {
  915. var special = _excelSpecials[j];
  916. // TODO Need to provide the ability for the specials to say
  917. // if they are returning a string, since at the moment it is
  918. // assumed to be a number
  919. if ( row[i].match && ! row[i].match(/^0\d+/) && row[i].match( special.match ) ) {
  920. var val = row[i].replace(/[^\d\.\-]/g, '');
  921. if ( special.fmt ) {
  922. val = special.fmt( val );
  923. }
  924. cell = _createNode( rels, 'c', {
  925. attr: {
  926. r: cellId,
  927. s: special.style
  928. },
  929. children: [
  930. _createNode( rels, 'v', { text: val } )
  931. ]
  932. } );
  933. break;
  934. }
  935. }
  936. if ( ! cell ) {
  937. if ( typeof row[i] === 'number' || (
  938. row[i].match &&
  939. row[i].match(/^-?\d+(\.\d+)?$/) &&
  940. ! row[i].match(/^0\d+/) )
  941. ) {
  942. // Detect numbers - don't match numbers with leading zeros
  943. // or a negative anywhere but the start
  944. cell = _createNode( rels, 'c', {
  945. attr: {
  946. t: 'n',
  947. r: cellId
  948. },
  949. children: [
  950. _createNode( rels, 'v', { text: row[i] } )
  951. ]
  952. } );
  953. }
  954. else {
  955. // String output - replace non standard characters for text output
  956. var text = ! originalContent.replace ?
  957. originalContent :
  958. originalContent.replace(/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F-\x9F]/g, '');
  959. cell = _createNode( rels, 'c', {
  960. attr: {
  961. t: 'inlineStr',
  962. r: cellId
  963. },
  964. children:{
  965. row: _createNode( rels, 'is', {
  966. children: {
  967. row: _createNode( rels, 't', {
  968. text: text,
  969. attr: {
  970. 'xml:space': 'preserve'
  971. }
  972. } )
  973. }
  974. } )
  975. }
  976. } );
  977. }
  978. }
  979. rowNode.appendChild( cell );
  980. }
  981. relsGet.appendChild(rowNode);
  982. rowPos++;
  983. };
  984. $( 'sheets sheet', xlsx.xl['workbook.xml'] ).attr( 'name', _sheetname( config ) );
  985. if ( config.customizeData ) {
  986. config.customizeData( data );
  987. }
  988. var mergeCells = function ( row, colspan ) {
  989. var mergeCells = $('mergeCells', rels);
  990. mergeCells[0].appendChild( _createNode( rels, 'mergeCell', {
  991. attr: {
  992. ref: 'A'+row+':'+createCellPos(colspan)+row
  993. }
  994. } ) );
  995. mergeCells.attr( 'count', parseFloat(mergeCells.attr( 'count' ))+1 );
  996. $('row:eq('+(row-1)+') c', rels).attr( 's', '51' ); // centre
  997. };
  998. // Title and top messages
  999. var exportInfo = dt.buttons.exportInfo( config );
  1000. if ( exportInfo.title ) {
  1001. addRow( [exportInfo.title], rowPos );
  1002. mergeCells( rowPos, data.header.length-1 );
  1003. }
  1004. if ( exportInfo.messageTop ) {
  1005. addRow( [exportInfo.messageTop], rowPos );
  1006. mergeCells( rowPos, data.header.length-1 );
  1007. }
  1008. // Table itself
  1009. if ( config.header ) {
  1010. addRow( data.header, rowPos );
  1011. $('row:last c', rels).attr( 's', '2' ); // bold
  1012. }
  1013. for ( var n=0, ie=data.body.length ; n<ie ; n++ ) {
  1014. addRow( data.body[n], rowPos );
  1015. }
  1016. if ( config.footer && data.footer ) {
  1017. addRow( data.footer, rowPos);
  1018. $('row:last c', rels).attr( 's', '2' ); // bold
  1019. }
  1020. // Below the table
  1021. if ( exportInfo.messageBottom ) {
  1022. addRow( [exportInfo.messageBottom], rowPos );
  1023. mergeCells( rowPos, data.header.length-1 );
  1024. }
  1025. // Set column widths
  1026. var cols = _createNode( rels, 'cols' );
  1027. $('worksheet', rels).prepend( cols );
  1028. for ( var i=0, ien=data.header.length ; i<ien ; i++ ) {
  1029. cols.appendChild( _createNode( rels, 'col', {
  1030. attr: {
  1031. min: i+1,
  1032. max: i+1,
  1033. width: _excelColWidth( data, i ),
  1034. customWidth: 1
  1035. }
  1036. } ) );
  1037. }
  1038. // Let the developer customise the document if they want to
  1039. if ( config.customize ) {
  1040. config.customize( xlsx, config, dt );
  1041. }
  1042. // Excel doesn't like an empty mergeCells tag
  1043. if ( $('mergeCells', rels).children().length === 0 ) {
  1044. $('mergeCells', rels).remove();
  1045. }
  1046. var jszip = _jsZip();
  1047. var zip = new jszip();
  1048. var zipConfig = {
  1049. type: 'blob',
  1050. mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  1051. };
  1052. _addToZip( zip, xlsx );
  1053. if ( zip.generateAsync ) {
  1054. // JSZip 3+
  1055. zip
  1056. .generateAsync( zipConfig )
  1057. .then( function ( blob ) {
  1058. _saveAs( blob, exportInfo.filename );
  1059. that.processing( false );
  1060. } );
  1061. }
  1062. else {
  1063. // JSZip 2.5
  1064. _saveAs(
  1065. zip.generate( zipConfig ),
  1066. exportInfo.filename
  1067. );
  1068. this.processing( false );
  1069. }
  1070. },
  1071. filename: '*',
  1072. extension: '.xlsx',
  1073. exportOptions: {},
  1074. header: true,
  1075. footer: false,
  1076. title: '*',
  1077. messageTop: '*',
  1078. messageBottom: '*',
  1079. createEmptyCells: false
  1080. };
  1081. //
  1082. // PDF export - using pdfMake - http://pdfmake.org
  1083. //
  1084. DataTable.ext.buttons.pdfHtml5 = {
  1085. className: 'buttons-pdf buttons-html5',
  1086. available: function () {
  1087. return window.FileReader !== undefined && _pdfMake();
  1088. },
  1089. text: function ( dt ) {
  1090. return dt.i18n( 'buttons.pdf', 'PDF' );
  1091. },
  1092. action: function ( e, dt, button, config ) {
  1093. this.processing( true );
  1094. var that = this;
  1095. var data = dt.buttons.exportData( config.exportOptions );
  1096. var info = dt.buttons.exportInfo( config );
  1097. var rows = [];
  1098. if ( config.header ) {
  1099. rows.push( $.map( data.header, function ( d ) {
  1100. return {
  1101. text: typeof d === 'string' ? d : d+'',
  1102. style: 'tableHeader'
  1103. };
  1104. } ) );
  1105. }
  1106. for ( var i=0, ien=data.body.length ; i<ien ; i++ ) {
  1107. rows.push( $.map( data.body[i], function ( d ) {
  1108. return {
  1109. text: typeof d === 'string' ? d : d+'',
  1110. style: i % 2 ? 'tableBodyEven' : 'tableBodyOdd'
  1111. };
  1112. } ) );
  1113. }
  1114. if ( config.footer && data.footer) {
  1115. rows.push( $.map( data.footer, function ( d ) {
  1116. return {
  1117. text: typeof d === 'string' ? d : d+'',
  1118. style: 'tableFooter'
  1119. };
  1120. } ) );
  1121. }
  1122. var doc = {
  1123. pageSize: config.pageSize,
  1124. pageOrientation: config.orientation,
  1125. content: [
  1126. {
  1127. table: {
  1128. headerRows: 1,
  1129. body: rows
  1130. },
  1131. layout: 'noBorders'
  1132. }
  1133. ],
  1134. styles: {
  1135. tableHeader: {
  1136. bold: true,
  1137. fontSize: 11,
  1138. color: 'white',
  1139. fillColor: '#2d4154',
  1140. alignment: 'center'
  1141. },
  1142. tableBodyEven: {},
  1143. tableBodyOdd: {
  1144. fillColor: '#f3f3f3'
  1145. },
  1146. tableFooter: {
  1147. bold: true,
  1148. fontSize: 11,
  1149. color: 'white',
  1150. fillColor: '#2d4154'
  1151. },
  1152. title: {
  1153. alignment: 'center',
  1154. fontSize: 15
  1155. },
  1156. message: {}
  1157. },
  1158. defaultStyle: {
  1159. fontSize: 10
  1160. }
  1161. };
  1162. if ( info.messageTop ) {
  1163. doc.content.unshift( {
  1164. text: info.messageTop,
  1165. style: 'message',
  1166. margin: [ 0, 0, 0, 12 ]
  1167. } );
  1168. }
  1169. if ( info.messageBottom ) {
  1170. doc.content.push( {
  1171. text: info.messageBottom,
  1172. style: 'message',
  1173. margin: [ 0, 0, 0, 12 ]
  1174. } );
  1175. }
  1176. if ( info.title ) {
  1177. doc.content.unshift( {
  1178. text: info.title,
  1179. style: 'title',
  1180. margin: [ 0, 0, 0, 12 ]
  1181. } );
  1182. }
  1183. if ( config.customize ) {
  1184. config.customize( doc, config, dt );
  1185. }
  1186. var pdf = _pdfMake().createPdf( doc );
  1187. if ( config.download === 'open' && ! _isDuffSafari() ) {
  1188. pdf.open();
  1189. }
  1190. else {
  1191. pdf.download( info.filename );
  1192. }
  1193. this.processing( false );
  1194. },
  1195. title: '*',
  1196. filename: '*',
  1197. extension: '.pdf',
  1198. exportOptions: {},
  1199. orientation: 'portrait',
  1200. pageSize: 'A4',
  1201. header: true,
  1202. footer: false,
  1203. messageTop: '*',
  1204. messageBottom: '*',
  1205. customize: null,
  1206. download: 'download'
  1207. };
  1208. return DataTable.Buttons;
  1209. }));