Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

1464 Zeilen
43KB

  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 = _;
  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. ( new window.DOMParser() ).parseFromString( excelStrings['xl/worksheets/sheet1.xml'], 'text/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="68">'+
  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. '<xf numFmtId="14" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  687. '</cellXfs>'+
  688. '<cellStyles count="1">'+
  689. '<cellStyle name="Normal" xfId="0" builtinId="0" />'+
  690. '</cellStyles>'+
  691. '<dxfs count="0" />'+
  692. '<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleMedium4" />'+
  693. '</styleSheet>'
  694. };
  695. // Note we could use 3 `for` loops for the styles, but when gzipped there is
  696. // virtually no difference in size, since the above can be easily compressed
  697. // Pattern matching for special number formats. Perhaps this should be exposed
  698. // via an API in future?
  699. // Ref: section 3.8.30 - built in formatters in open spreadsheet
  700. // https://www.ecma-international.org/news/TC45_current_work/Office%20Open%20XML%20Part%204%20-%20Markup%20Language%20Reference.pdf
  701. var _excelSpecials = [
  702. { match: /^\-?\d+\.\d%$/, style: 60, fmt: function (d) { return d/100; } }, // Percent with d.p.
  703. { match: /^\-?\d+\.?\d*%$/, style: 56, fmt: function (d) { return d/100; } }, // Percent
  704. { match: /^\-?\$[\d,]+.?\d*$/, style: 57 }, // Dollars
  705. { match: /^\-?£[\d,]+.?\d*$/, style: 58 }, // Pounds
  706. { match: /^\-?€[\d,]+.?\d*$/, style: 59 }, // Euros
  707. { match: /^\-?\d+$/, style: 65 }, // Numbers without thousand separators
  708. { match: /^\-?\d+\.\d{2}$/, style: 66 }, // Numbers 2 d.p. without thousands separators
  709. { match: /^\([\d,]+\)$/, style: 61, fmt: function (d) { return -1 * d.replace(/[\(\)]/g, ''); } }, // Negative numbers indicated by brackets
  710. { match: /^\([\d,]+\.\d{2}\)$/, style: 62, fmt: function (d) { return -1 * d.replace(/[\(\)]/g, ''); } }, // Negative numbers indicated by brackets - 2d.p.
  711. { match: /^\-?[\d,]+$/, style: 63 }, // Numbers with thousand separators
  712. { match: /^\-?[\d,]+\.\d{2}$/, style: 64 },
  713. { match: /^[\d]{4}\-[\d]{2}\-[\d]{2}$/, style: 67, fmt: function (d) {return Math.round(25569 + (Date.parse(d) / (86400 * 1000)));}} //Date yyyy-mm-dd
  714. ];
  715. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  716. * Buttons
  717. */
  718. //
  719. // Copy to clipboard
  720. //
  721. DataTable.ext.buttons.copyHtml5 = {
  722. className: 'buttons-copy buttons-html5',
  723. text: function ( dt ) {
  724. return dt.i18n( 'buttons.copy', 'Copy' );
  725. },
  726. action: function ( e, dt, button, config ) {
  727. this.processing( true );
  728. var that = this;
  729. var exportData = _exportData( dt, config );
  730. var info = dt.buttons.exportInfo( config );
  731. var newline = _newLine(config);
  732. var output = exportData.str;
  733. var hiddenDiv = $('<div/>')
  734. .css( {
  735. height: 1,
  736. width: 1,
  737. overflow: 'hidden',
  738. position: 'fixed',
  739. top: 0,
  740. left: 0
  741. } );
  742. if ( info.title ) {
  743. output = info.title + newline + newline + output;
  744. }
  745. if ( info.messageTop ) {
  746. output = info.messageTop + newline + newline + output;
  747. }
  748. if ( info.messageBottom ) {
  749. output = output + newline + newline + info.messageBottom;
  750. }
  751. if ( config.customize ) {
  752. output = config.customize( output, config, dt );
  753. }
  754. var textarea = $('<textarea readonly/>')
  755. .val( output )
  756. .appendTo( hiddenDiv );
  757. // For browsers that support the copy execCommand, try to use it
  758. if ( document.queryCommandSupported('copy') ) {
  759. hiddenDiv.appendTo( dt.table().container() );
  760. textarea[0].focus();
  761. textarea[0].select();
  762. try {
  763. var successful = document.execCommand( 'copy' );
  764. hiddenDiv.remove();
  765. if (successful) {
  766. dt.buttons.info(
  767. dt.i18n( 'buttons.copyTitle', 'Copy to clipboard' ),
  768. dt.i18n( 'buttons.copySuccess', {
  769. 1: 'Copied one row to clipboard',
  770. _: 'Copied %d rows to clipboard'
  771. }, exportData.rows ),
  772. 2000
  773. );
  774. this.processing( false );
  775. return;
  776. }
  777. }
  778. catch (t) {}
  779. }
  780. // Otherwise we show the text box and instruct the user to use it
  781. var message = $('<span>'+dt.i18n( 'buttons.copyKeys',
  782. 'Press <i>ctrl</i> or <i>\u2318</i> + <i>C</i> to copy the table data<br>to your system clipboard.<br><br>'+
  783. 'To cancel, click this message or press escape.' )+'</span>'
  784. )
  785. .append( hiddenDiv );
  786. dt.buttons.info( dt.i18n( 'buttons.copyTitle', 'Copy to clipboard' ), message, 0 );
  787. // Select the text so when the user activates their system clipboard
  788. // it will copy that text
  789. textarea[0].focus();
  790. textarea[0].select();
  791. // Event to hide the message when the user is done
  792. var container = $(message).closest('.dt-button-info');
  793. var close = function () {
  794. container.off( 'click.buttons-copy' );
  795. $(document).off( '.buttons-copy' );
  796. dt.buttons.info( false );
  797. };
  798. container.on( 'click.buttons-copy', close );
  799. $(document)
  800. .on( 'keydown.buttons-copy', function (e) {
  801. if ( e.keyCode === 27 ) { // esc
  802. close();
  803. that.processing( false );
  804. }
  805. } )
  806. .on( 'copy.buttons-copy cut.buttons-copy', function () {
  807. close();
  808. that.processing( false );
  809. } );
  810. },
  811. exportOptions: {},
  812. fieldSeparator: '\t',
  813. fieldBoundary: '',
  814. header: true,
  815. footer: false,
  816. title: '*',
  817. messageTop: '*',
  818. messageBottom: '*'
  819. };
  820. //
  821. // CSV export
  822. //
  823. DataTable.ext.buttons.csvHtml5 = {
  824. bom: false,
  825. className: 'buttons-csv buttons-html5',
  826. available: function () {
  827. return window.FileReader !== undefined && window.Blob;
  828. },
  829. text: function ( dt ) {
  830. return dt.i18n( 'buttons.csv', 'CSV' );
  831. },
  832. action: function ( e, dt, button, config ) {
  833. this.processing( true );
  834. // Set the text
  835. var output = _exportData( dt, config ).str;
  836. var info = dt.buttons.exportInfo(config);
  837. var charset = config.charset;
  838. if ( config.customize ) {
  839. output = config.customize( output, config, dt );
  840. }
  841. if ( charset !== false ) {
  842. if ( ! charset ) {
  843. charset = document.characterSet || document.charset;
  844. }
  845. if ( charset ) {
  846. charset = ';charset='+charset;
  847. }
  848. }
  849. else {
  850. charset = '';
  851. }
  852. if ( config.bom ) {
  853. output = String.fromCharCode(0xFEFF) + output;
  854. }
  855. _saveAs(
  856. new Blob( [output], {type: 'text/csv'+charset} ),
  857. info.filename,
  858. true
  859. );
  860. this.processing( false );
  861. },
  862. filename: '*',
  863. extension: '.csv',
  864. exportOptions: {},
  865. fieldSeparator: ',',
  866. fieldBoundary: '"',
  867. escapeChar: '"',
  868. charset: null,
  869. header: true,
  870. footer: false
  871. };
  872. //
  873. // Excel (xlsx) export
  874. //
  875. DataTable.ext.buttons.excelHtml5 = {
  876. className: 'buttons-excel buttons-html5',
  877. available: function () {
  878. return window.FileReader !== undefined && _jsZip() !== undefined && ! _isDuffSafari() && _serialiser;
  879. },
  880. text: function ( dt ) {
  881. return dt.i18n( 'buttons.excel', 'Excel' );
  882. },
  883. action: function ( e, dt, button, config ) {
  884. this.processing( true );
  885. var that = this;
  886. var rowPos = 0;
  887. var dataStartRow, dataEndRow;
  888. var getXml = function ( type ) {
  889. var str = excelStrings[ type ];
  890. //str = str.replace( /xmlns:/g, 'xmlns_' ).replace( /mc:/g, 'mc_' );
  891. return $.parseXML( str );
  892. };
  893. var rels = getXml('xl/worksheets/sheet1.xml');
  894. var relsGet = rels.getElementsByTagName( "sheetData" )[0];
  895. var xlsx = {
  896. _rels: {
  897. ".rels": getXml('_rels/.rels')
  898. },
  899. xl: {
  900. _rels: {
  901. "workbook.xml.rels": getXml('xl/_rels/workbook.xml.rels')
  902. },
  903. "workbook.xml": getXml('xl/workbook.xml'),
  904. "styles.xml": getXml('xl/styles.xml'),
  905. "worksheets": {
  906. "sheet1.xml": rels
  907. }
  908. },
  909. "[Content_Types].xml": getXml('[Content_Types].xml')
  910. };
  911. var data = dt.buttons.exportData( config.exportOptions );
  912. var currentRow, rowNode;
  913. var addRow = function ( row ) {
  914. currentRow = rowPos+1;
  915. rowNode = _createNode( rels, "row", { attr: {r:currentRow} } );
  916. for ( var i=0, ien=row.length ; i<ien ; i++ ) {
  917. // Concat both the Cell Columns as a letter and the Row of the cell.
  918. var cellId = createCellPos(i) + '' + currentRow;
  919. var cell = null;
  920. // For null, undefined of blank cell, continue so it doesn't create the _createNode
  921. if ( row[i] === null || row[i] === undefined || row[i] === '' ) {
  922. if ( config.createEmptyCells === true ) {
  923. row[i] = '';
  924. }
  925. else {
  926. continue;
  927. }
  928. }
  929. var originalContent = row[i];
  930. row[i] = typeof row[i].trim === 'function'
  931. ? row[i].trim()
  932. : row[i];
  933. // Special number formatting options
  934. for ( var j=0, jen=_excelSpecials.length ; j<jen ; j++ ) {
  935. var special = _excelSpecials[j];
  936. // TODO Need to provide the ability for the specials to say
  937. // if they are returning a string, since at the moment it is
  938. // assumed to be a number
  939. if ( row[i].match && ! row[i].match(/^0\d+/) && row[i].match( special.match ) ) {
  940. var val = row[i].replace(/[^\d\.\-]/g, '');
  941. if ( special.fmt ) {
  942. val = special.fmt( val );
  943. }
  944. cell = _createNode( rels, 'c', {
  945. attr: {
  946. r: cellId,
  947. s: special.style
  948. },
  949. children: [
  950. _createNode( rels, 'v', { text: val } )
  951. ]
  952. } );
  953. break;
  954. }
  955. }
  956. if ( ! cell ) {
  957. if ( typeof row[i] === 'number' || (
  958. row[i].match &&
  959. row[i].match(/^-?\d+(\.\d+)?([eE]\-?\d+)?$/) && // Includes exponential format
  960. ! row[i].match(/^0\d+/) )
  961. ) {
  962. // Detect numbers - don't match numbers with leading zeros
  963. // or a negative anywhere but the start
  964. cell = _createNode( rels, 'c', {
  965. attr: {
  966. t: 'n',
  967. r: cellId
  968. },
  969. children: [
  970. _createNode( rels, 'v', { text: row[i] } )
  971. ]
  972. } );
  973. }
  974. else {
  975. // String output - replace non standard characters for text output
  976. var text = ! originalContent.replace ?
  977. originalContent :
  978. originalContent.replace(/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F-\x9F]/g, '');
  979. cell = _createNode( rels, 'c', {
  980. attr: {
  981. t: 'inlineStr',
  982. r: cellId
  983. },
  984. children:{
  985. row: _createNode( rels, 'is', {
  986. children: {
  987. row: _createNode( rels, 't', {
  988. text: text,
  989. attr: {
  990. 'xml:space': 'preserve'
  991. }
  992. } )
  993. }
  994. } )
  995. }
  996. } );
  997. }
  998. }
  999. rowNode.appendChild( cell );
  1000. }
  1001. relsGet.appendChild(rowNode);
  1002. rowPos++;
  1003. };
  1004. if ( config.customizeData ) {
  1005. config.customizeData( data );
  1006. }
  1007. var mergeCells = function ( row, colspan ) {
  1008. var mergeCells = $('mergeCells', rels);
  1009. mergeCells[0].appendChild( _createNode( rels, 'mergeCell', {
  1010. attr: {
  1011. ref: 'A'+row+':'+createCellPos(colspan)+row
  1012. }
  1013. } ) );
  1014. mergeCells.attr( 'count', parseFloat(mergeCells.attr( 'count' ))+1 );
  1015. $('row:eq('+(row-1)+') c', rels).attr( 's', '51' ); // centre
  1016. };
  1017. // Title and top messages
  1018. var exportInfo = dt.buttons.exportInfo( config );
  1019. if ( exportInfo.title ) {
  1020. addRow( [exportInfo.title], rowPos );
  1021. mergeCells( rowPos, data.header.length-1 );
  1022. }
  1023. if ( exportInfo.messageTop ) {
  1024. addRow( [exportInfo.messageTop], rowPos );
  1025. mergeCells( rowPos, data.header.length-1 );
  1026. }
  1027. // Table itself
  1028. if ( config.header ) {
  1029. addRow( data.header, rowPos );
  1030. $('row:last c', rels).attr( 's', '2' ); // bold
  1031. }
  1032. dataStartRow = rowPos;
  1033. for ( var n=0, ie=data.body.length ; n<ie ; n++ ) {
  1034. addRow( data.body[n], rowPos );
  1035. }
  1036. dataEndRow = rowPos;
  1037. if ( config.footer && data.footer ) {
  1038. addRow( data.footer, rowPos);
  1039. $('row:last c', rels).attr( 's', '2' ); // bold
  1040. }
  1041. // Below the table
  1042. if ( exportInfo.messageBottom ) {
  1043. addRow( [exportInfo.messageBottom], rowPos );
  1044. mergeCells( rowPos, data.header.length-1 );
  1045. }
  1046. // Set column widths
  1047. var cols = _createNode( rels, 'cols' );
  1048. $('worksheet', rels).prepend( cols );
  1049. for ( var i=0, ien=data.header.length ; i<ien ; i++ ) {
  1050. cols.appendChild( _createNode( rels, 'col', {
  1051. attr: {
  1052. min: i+1,
  1053. max: i+1,
  1054. width: _excelColWidth( data, i ),
  1055. customWidth: 1
  1056. }
  1057. } ) );
  1058. }
  1059. // Workbook modifications
  1060. var workbook = xlsx.xl['workbook.xml'];
  1061. $( 'sheets sheet', workbook ).attr( 'name', _sheetname( config ) );
  1062. // Auto filter for columns
  1063. if ( config.autoFilter ) {
  1064. $('mergeCells', rels).before( _createNode( rels, 'autoFilter', {
  1065. attr: {
  1066. ref: 'A'+dataStartRow+':'+createCellPos(data.header.length-1)+dataEndRow
  1067. }
  1068. } ) );
  1069. $('definedNames', workbook).append( _createNode( workbook, 'definedName', {
  1070. attr: {
  1071. name: '_xlnm._FilterDatabase',
  1072. localSheetId: '0',
  1073. hidden: 1
  1074. },
  1075. text: _sheetname(config)+'!$A$'+dataStartRow+':'+createCellPos(data.header.length-1)+dataEndRow
  1076. } ) );
  1077. }
  1078. // Let the developer customise the document if they want to
  1079. if ( config.customize ) {
  1080. config.customize( xlsx, config, dt );
  1081. }
  1082. // Excel doesn't like an empty mergeCells tag
  1083. if ( $('mergeCells', rels).children().length === 0 ) {
  1084. $('mergeCells', rels).remove();
  1085. }
  1086. var jszip = _jsZip();
  1087. var zip = new jszip();
  1088. var zipConfig = {
  1089. type: 'blob',
  1090. mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  1091. };
  1092. _addToZip( zip, xlsx );
  1093. if ( zip.generateAsync ) {
  1094. // JSZip 3+
  1095. zip
  1096. .generateAsync( zipConfig )
  1097. .then( function ( blob ) {
  1098. _saveAs( blob, exportInfo.filename );
  1099. that.processing( false );
  1100. } );
  1101. }
  1102. else {
  1103. // JSZip 2.5
  1104. _saveAs(
  1105. zip.generate( zipConfig ),
  1106. exportInfo.filename
  1107. );
  1108. this.processing( false );
  1109. }
  1110. },
  1111. filename: '*',
  1112. extension: '.xlsx',
  1113. exportOptions: {},
  1114. header: true,
  1115. footer: false,
  1116. title: '*',
  1117. messageTop: '*',
  1118. messageBottom: '*',
  1119. createEmptyCells: false,
  1120. autoFilter: false,
  1121. sheetName: ''
  1122. };
  1123. //
  1124. // PDF export - using pdfMake - http://pdfmake.org
  1125. //
  1126. DataTable.ext.buttons.pdfHtml5 = {
  1127. className: 'buttons-pdf buttons-html5',
  1128. available: function () {
  1129. return window.FileReader !== undefined && _pdfMake();
  1130. },
  1131. text: function ( dt ) {
  1132. return dt.i18n( 'buttons.pdf', 'PDF' );
  1133. },
  1134. action: function ( e, dt, button, config ) {
  1135. this.processing( true );
  1136. var that = this;
  1137. var data = dt.buttons.exportData( config.exportOptions );
  1138. var info = dt.buttons.exportInfo( config );
  1139. var rows = [];
  1140. if ( config.header ) {
  1141. rows.push( $.map( data.header, function ( d ) {
  1142. return {
  1143. text: typeof d === 'string' ? d : d+'',
  1144. style: 'tableHeader'
  1145. };
  1146. } ) );
  1147. }
  1148. for ( var i=0, ien=data.body.length ; i<ien ; i++ ) {
  1149. rows.push( $.map( data.body[i], function ( d ) {
  1150. if ( d === null || d === undefined ) {
  1151. d = '';
  1152. }
  1153. return {
  1154. text: typeof d === 'string' ? d : d+'',
  1155. style: i % 2 ? 'tableBodyEven' : 'tableBodyOdd'
  1156. };
  1157. } ) );
  1158. }
  1159. if ( config.footer && data.footer) {
  1160. rows.push( $.map( data.footer, function ( d ) {
  1161. return {
  1162. text: typeof d === 'string' ? d : d+'',
  1163. style: 'tableFooter'
  1164. };
  1165. } ) );
  1166. }
  1167. var doc = {
  1168. pageSize: config.pageSize,
  1169. pageOrientation: config.orientation,
  1170. content: [
  1171. {
  1172. table: {
  1173. headerRows: 1,
  1174. body: rows
  1175. },
  1176. layout: 'noBorders'
  1177. }
  1178. ],
  1179. styles: {
  1180. tableHeader: {
  1181. bold: true,
  1182. fontSize: 11,
  1183. color: 'white',
  1184. fillColor: '#2d4154',
  1185. alignment: 'center'
  1186. },
  1187. tableBodyEven: {},
  1188. tableBodyOdd: {
  1189. fillColor: '#f3f3f3'
  1190. },
  1191. tableFooter: {
  1192. bold: true,
  1193. fontSize: 11,
  1194. color: 'white',
  1195. fillColor: '#2d4154'
  1196. },
  1197. title: {
  1198. alignment: 'center',
  1199. fontSize: 15
  1200. },
  1201. message: {}
  1202. },
  1203. defaultStyle: {
  1204. fontSize: 10
  1205. }
  1206. };
  1207. if ( info.messageTop ) {
  1208. doc.content.unshift( {
  1209. text: info.messageTop,
  1210. style: 'message',
  1211. margin: [ 0, 0, 0, 12 ]
  1212. } );
  1213. }
  1214. if ( info.messageBottom ) {
  1215. doc.content.push( {
  1216. text: info.messageBottom,
  1217. style: 'message',
  1218. margin: [ 0, 0, 0, 12 ]
  1219. } );
  1220. }
  1221. if ( info.title ) {
  1222. doc.content.unshift( {
  1223. text: info.title,
  1224. style: 'title',
  1225. margin: [ 0, 0, 0, 12 ]
  1226. } );
  1227. }
  1228. if ( config.customize ) {
  1229. config.customize( doc, config, dt );
  1230. }
  1231. var pdf = _pdfMake().createPdf( doc );
  1232. if ( config.download === 'open' && ! _isDuffSafari() ) {
  1233. pdf.open();
  1234. }
  1235. else {
  1236. pdf.download( info.filename );
  1237. }
  1238. this.processing( false );
  1239. },
  1240. title: '*',
  1241. filename: '*',
  1242. extension: '.pdf',
  1243. exportOptions: {},
  1244. orientation: 'portrait',
  1245. pageSize: 'A4',
  1246. header: true,
  1247. footer: false,
  1248. messageTop: '*',
  1249. messageBottom: '*',
  1250. customize: null,
  1251. download: 'download'
  1252. };
  1253. return DataTable.Buttons;
  1254. }));