excel_creator.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /**
  2. * DevExtreme (exporter/excel_creator.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var _class = require("../core/class");
  11. var _class2 = _interopRequireDefault(_class);
  12. var _window = require("../core/utils/window");
  13. var _type = require("../core/utils/type");
  14. var _extend = require("../core/utils/extend");
  15. var _ui = require("../ui/widget/ui.errors");
  16. var _ui2 = _interopRequireDefault(_ui);
  17. var _string = require("../core/utils/string");
  18. var _string2 = _interopRequireDefault(_string);
  19. var _jszip = require("jszip");
  20. var _jszip2 = _interopRequireDefault(_jszip);
  21. var _file_saver = require("./file_saver");
  22. var _file_saver2 = _interopRequireDefault(_file_saver);
  23. var _excel_format_converter = require("./excel_format_converter");
  24. var _excel_format_converter2 = _interopRequireDefault(_excel_format_converter);
  25. var _excel = require("./excel/excel.file");
  26. var _excel2 = _interopRequireDefault(_excel);
  27. function _interopRequireDefault(obj) {
  28. return obj && obj.__esModule ? obj : {
  29. "default": obj
  30. }
  31. }
  32. function _typeof(obj) {
  33. "@babel/helpers - typeof";
  34. return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(obj) {
  35. return typeof obj
  36. } : function(obj) {
  37. return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj
  38. }, _typeof(obj)
  39. }
  40. var XML_TAG = '<?xml version="1.0" encoding="utf-8"?>';
  41. var GROUP_SHEET_PR_XML = '<sheetPr><outlinePr summaryBelow="0"/></sheetPr>';
  42. var SINGLE_SHEET_PR_XML = "<sheetPr/>";
  43. var BASE_STYLE_XML2 = '<borders count="1"><border><left style="thin"><color rgb="FFD3D3D3"/></left><right style="thin"><color rgb="FFD3D3D3"/></right><top style="thin"><color rgb="FFD3D3D3"/></top><bottom style="thin"><color rgb="FFD3D3D3"/></bottom></border></borders><cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs>';
  44. var OPEN_XML_FORMAT_URL = "http://schemas.openxmlformats.org";
  45. var RELATIONSHIP_PART_NAME = "rels";
  46. var XL_FOLDER_NAME = "xl";
  47. var WORKBOOK_FILE_NAME = "workbook.xml";
  48. var CONTENTTYPES_FILE_NAME = "[Content_Types].xml";
  49. var SHAREDSTRING_FILE_NAME = "sharedStrings.xml";
  50. var STYLE_FILE_NAME = "styles.xml";
  51. var WORKSHEETS_FOLDER = "worksheets";
  52. var WORKSHEET_FILE_NAME = "sheet1.xml";
  53. var WORKSHEET_HEADER_XML = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><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">';
  54. var VALID_TYPES = {
  55. "boolean": "b",
  56. date: "d",
  57. number: "n",
  58. string: "s"
  59. };
  60. var EXCEL_START_TIME = Date.UTC(1899, 11, 30);
  61. var DAYS_COUNT_BEFORE_29_FEB_1900 = 60;
  62. var MAX_DIGIT_WIDTH_IN_PIXELS = 7;
  63. var UNSUPPORTED_FORMAT_MAPPING = {
  64. quarter: "shortDate",
  65. quarterAndYear: "shortDate",
  66. minute: "longTime",
  67. millisecond: "longTime"
  68. };
  69. var ExcelCreator = _class2.default.inherit({
  70. _getXMLTag: function(tagName, attributes, content) {
  71. var result = "<" + tagName;
  72. var i;
  73. var length = attributes.length;
  74. var attr;
  75. for (i = 0; i < length; i++) {
  76. attr = attributes[i];
  77. if (void 0 !== attr.value) {
  78. result = result + " " + attr.name + '="' + attr.value + '"'
  79. }
  80. }
  81. return (0, _type.isDefined)(content) ? result + ">" + content + "</" + tagName + ">" : result + " />"
  82. },
  83. _convertToExcelCellRef: function(zeroBasedRowIndex, zeroBasedCellIndex) {
  84. var columnName = "";
  85. var max = 26;
  86. var charCode;
  87. var isCellIndexFound;
  88. while (!isCellIndexFound) {
  89. charCode = 65 + (zeroBasedCellIndex >= max ? zeroBasedCellIndex % max : Math.ceil(zeroBasedCellIndex));
  90. columnName = String.fromCharCode(charCode) + columnName;
  91. if (zeroBasedCellIndex >= max) {
  92. zeroBasedCellIndex = Math.floor(zeroBasedCellIndex / max) - 1
  93. } else {
  94. isCellIndexFound = true
  95. }
  96. }
  97. return columnName + (zeroBasedRowIndex + 1)
  98. },
  99. _convertToExcelCellRefAndTrackMaxIndex: function(rowIndex, cellIndex) {
  100. if (this._maxRowIndex < Number(rowIndex)) {
  101. this._maxRowIndex = Number(rowIndex)
  102. }
  103. if (this._maxColumnIndex < Number(cellIndex)) {
  104. this._maxColumnIndex = Number(cellIndex)
  105. }
  106. return this._convertToExcelCellRef(rowIndex, cellIndex)
  107. },
  108. _getDataType: function(dataType) {
  109. return VALID_TYPES[dataType] || VALID_TYPES.string
  110. },
  111. _tryGetExcelCellDataType: function(object) {
  112. if ((0, _type.isDefined)(object)) {
  113. if ("number" === typeof object) {
  114. if (isFinite(object)) {
  115. return VALID_TYPES.number
  116. } else {
  117. return VALID_TYPES.string
  118. }
  119. } else {
  120. if ((0, _type.isString)(object)) {
  121. return VALID_TYPES.string
  122. } else {
  123. if ((0, _type.isDate)(object)) {
  124. return VALID_TYPES.number
  125. } else {
  126. if ((0, _type.isBoolean)(object)) {
  127. return VALID_TYPES.boolean
  128. }
  129. }
  130. }
  131. }
  132. }
  133. },
  134. _formatObjectConverter: function(format, dataType) {
  135. var result = {
  136. format: format,
  137. precision: format && format.precision,
  138. dataType: dataType
  139. };
  140. if ((0, _type.isObject)(format)) {
  141. return (0, _extend.extend)(result, format, {
  142. format: format.formatter || format.type,
  143. currency: format.currency
  144. })
  145. }
  146. return result
  147. },
  148. _tryConvertToExcelNumberFormat: function(format, dataType) {
  149. var newFormat = this._formatObjectConverter(format, dataType);
  150. format = newFormat.format;
  151. var currency = newFormat.currency;
  152. dataType = newFormat.dataType;
  153. if ((0, _type.isDefined)(format) && "date" === dataType) {
  154. format = UNSUPPORTED_FORMAT_MAPPING[format && format.type || format] || format
  155. }
  156. return _excel_format_converter2.default.convertFormat(format, newFormat.precision, dataType, currency)
  157. },
  158. _appendString: function(value) {
  159. if ((0, _type.isDefined)(value)) {
  160. value = String(value);
  161. if (value.length) {
  162. value = _string2.default.encodeHtml(value);
  163. if (void 0 === this._stringHash[value]) {
  164. this._stringHash[value] = this._stringArray.length;
  165. this._stringArray.push(value)
  166. }
  167. return this._stringHash[value]
  168. }
  169. }
  170. },
  171. _tryGetExcelDateValue: function(date) {
  172. var days;
  173. var totalTime;
  174. if ((0, _type.isDate)(date)) {
  175. days = Math.floor((Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) - EXCEL_START_TIME) / 864e5);
  176. if (days < DAYS_COUNT_BEFORE_29_FEB_1900) {
  177. days--
  178. }
  179. totalTime = (3600 * date.getHours() + 60 * date.getMinutes() + date.getSeconds()) / 86400;
  180. return days + totalTime
  181. }
  182. },
  183. _prepareValue: function(rowIndex, cellIndex) {
  184. var dataProvider = this._dataProvider;
  185. var cellData = dataProvider.getCellData(rowIndex, cellIndex) || {};
  186. var value = cellData.value;
  187. var cellSourceData = cellData.cellSourceData;
  188. var sourceValue;
  189. var type = this._getDataType(dataProvider.getCellType(rowIndex, cellIndex));
  190. if (type === VALID_TYPES.date && !(0, _type.isDate)(value)) {
  191. type = VALID_TYPES.string
  192. }
  193. switch (type) {
  194. case VALID_TYPES.string:
  195. sourceValue = value;
  196. value = this._appendString(value);
  197. break;
  198. case VALID_TYPES.date:
  199. sourceValue = value;
  200. value = this._tryGetExcelDateValue(value);
  201. type = VALID_TYPES.number
  202. }
  203. return {
  204. value: value,
  205. type: type,
  206. sourceValue: sourceValue,
  207. cellSourceData: cellSourceData
  208. }
  209. },
  210. _callCustomizeExcelCell: function(_ref) {
  211. var dataProvider = _ref.dataProvider,
  212. value = _ref.value,
  213. style = _ref.style,
  214. sourceData = _ref.sourceData;
  215. var styleCopy = _excel2.default.copyCellFormat(style);
  216. var args = {
  217. value: value,
  218. numberFormat: styleCopy.numberFormat,
  219. clearStyle: function() {
  220. this.horizontalAlignment = null;
  221. this.verticalAlignment = null;
  222. this.wrapTextEnabled = null;
  223. this.font = null;
  224. this.numberFormat = null
  225. }
  226. };
  227. if ((0, _type.isDefined)(styleCopy)) {
  228. if ((0, _type.isDefined)(styleCopy.alignment)) {
  229. args.horizontalAlignment = styleCopy.alignment.horizontal;
  230. args.verticalAlignment = styleCopy.alignment.vertical;
  231. args.wrapTextEnabled = styleCopy.alignment.wrapText
  232. }
  233. args.backgroundColor = styleCopy.backgroundColor;
  234. args.fillPatternType = styleCopy.fillPatternType;
  235. args.fillPatternColor = styleCopy.fillPatternColor;
  236. args.font = styleCopy.font
  237. }
  238. dataProvider.customizeExcelCell(args, sourceData);
  239. var newStyle = styleCopy || {};
  240. newStyle.font = args.font;
  241. newStyle.alignment = newStyle.alignment || {};
  242. newStyle.alignment.horizontal = args.horizontalAlignment;
  243. newStyle.alignment.vertical = args.verticalAlignment;
  244. newStyle.alignment.wrapText = args.wrapTextEnabled;
  245. newStyle.backgroundColor = args.backgroundColor;
  246. newStyle.fillPatternType = args.fillPatternType;
  247. newStyle.fillPatternColor = args.fillPatternColor;
  248. newStyle.numberFormat = args.numberFormat;
  249. return {
  250. value: args.value,
  251. style: newStyle
  252. }
  253. },
  254. _getDataArray: function() {
  255. var that = this;
  256. var rowIndex;
  257. var cellIndex;
  258. var cellsArray;
  259. var cellData;
  260. var result = [];
  261. var dataProvider = that._dataProvider;
  262. var rowsLength = dataProvider.getRowsCount();
  263. var columns = dataProvider.getColumns();
  264. var cellsLength;
  265. for (rowIndex = 0; rowIndex < rowsLength; rowIndex++) {
  266. cellsArray = [];
  267. cellsLength = columns.length;
  268. for (cellIndex = 0; cellIndex !== cellsLength; cellIndex++) {
  269. cellData = that._prepareValue(rowIndex, cellIndex);
  270. var styleArrayIndex = dataProvider.getStyleId(rowIndex, cellIndex);
  271. var cellStyleId = this._styleArrayIndexToCellStyleIdMap[styleArrayIndex];
  272. if (dataProvider.hasCustomizeExcelCell && dataProvider.hasCustomizeExcelCell()) {
  273. var value = cellData.sourceValue || cellData.value;
  274. var modifiedExcelCell = this._callCustomizeExcelCell({
  275. dataProvider: dataProvider,
  276. value: value,
  277. style: that._styleArray[styleArrayIndex],
  278. sourceData: cellData.cellSourceData
  279. });
  280. if (modifiedExcelCell.value !== value) {
  281. if (_typeof(modifiedExcelCell.value) !== _typeof(value) || "number" === typeof modifiedExcelCell.value && !isFinite(modifiedExcelCell.value)) {
  282. var cellDataType = this._tryGetExcelCellDataType(modifiedExcelCell.value);
  283. if ((0, _type.isDefined)(cellDataType)) {
  284. cellData.type = cellDataType
  285. }
  286. }
  287. switch (cellData.type) {
  288. case VALID_TYPES.string:
  289. cellData.value = this._appendString(modifiedExcelCell.value);
  290. break;
  291. case VALID_TYPES.date:
  292. cellData.value = modifiedExcelCell.value;
  293. break;
  294. case VALID_TYPES.number:
  295. var newValue = modifiedExcelCell.value;
  296. var excelDateValue = this._tryGetExcelDateValue(newValue);
  297. if ((0, _type.isDefined)(excelDateValue)) {
  298. newValue = excelDateValue
  299. }
  300. cellData.value = newValue;
  301. break;
  302. default:
  303. cellData.value = modifiedExcelCell.value
  304. }
  305. }
  306. cellStyleId = this._excelFile.registerCellFormat(modifiedExcelCell.style)
  307. }
  308. cellsArray.push({
  309. style: cellStyleId,
  310. value: cellData.value,
  311. type: cellData.type
  312. })
  313. }
  314. if (!that._needSheetPr && dataProvider.getGroupLevel(rowIndex) > 0) {
  315. that._needSheetPr = true
  316. }
  317. result.push(cellsArray)
  318. }
  319. return result
  320. },
  321. _calculateWidth: function(pixelsWidth) {
  322. pixelsWidth = parseInt(pixelsWidth, 10);
  323. if (!pixelsWidth || pixelsWidth < 5) {
  324. pixelsWidth = 100
  325. }
  326. return Math.min(255, Math.floor((pixelsWidth - 5) / MAX_DIGIT_WIDTH_IN_PIXELS * 100 + .5) / 100)
  327. },
  328. _prepareStyleData: function() {
  329. var _this = this;
  330. var that = this;
  331. var styles = that._dataProvider.getStyles();
  332. that._dataProvider.getColumns().forEach(function(column) {
  333. that._colsArray.push(that._calculateWidth(column.width))
  334. });
  335. var fonts = [{
  336. size: 11,
  337. color: {
  338. theme: 1
  339. },
  340. name: "Calibri",
  341. family: 2,
  342. scheme: "minor",
  343. bold: false
  344. }, {
  345. size: 11,
  346. color: {
  347. theme: 1
  348. },
  349. name: "Calibri",
  350. family: 2,
  351. scheme: "minor",
  352. bold: true
  353. }];
  354. this._excelFile.registerFont(fonts[0]);
  355. this._excelFile.registerFont(fonts[1]);
  356. styles.forEach(function(style) {
  357. var numberFormat = that._tryConvertToExcelNumberFormat(style.format, style.dataType);
  358. if (!(0, _type.isDefined)(numberFormat)) {
  359. numberFormat = 0
  360. }
  361. that._styleArray.push({
  362. font: fonts[Number(!!style.bold)],
  363. numberFormat: numberFormat,
  364. alignment: {
  365. vertical: "top",
  366. wrapText: !!style.wrapText,
  367. horizontal: style.alignment || "left"
  368. }
  369. })
  370. });
  371. that._styleArrayIndexToCellStyleIdMap = that._styleArray.map(function(item) {
  372. return _this._excelFile.registerCellFormat(item)
  373. })
  374. },
  375. _prepareCellData: function() {
  376. this._cellsArray = this._getDataArray()
  377. },
  378. _createXMLRelationships: function(xmlRelationships) {
  379. return this._getXMLTag("Relationships", [{
  380. name: "xmlns",
  381. value: OPEN_XML_FORMAT_URL + "/package/2006/relationships"
  382. }], xmlRelationships)
  383. },
  384. _createXMLRelationship: function(id, type, target) {
  385. return this._getXMLTag("Relationship", [{
  386. name: "Id",
  387. value: "rId" + id
  388. }, {
  389. name: "Type",
  390. value: OPEN_XML_FORMAT_URL + "/officeDocument/2006/relationships/" + type
  391. }, {
  392. name: "Target",
  393. value: target
  394. }])
  395. },
  396. _getWorkbookContent: function() {
  397. var content = '<bookViews><workbookView xWindow="0" yWindow="0" windowWidth="0" windowHeight="0"/></bookViews><sheets><sheet name="Sheet" sheetId="1" r:id="rId1" /></sheets><definedNames><definedName name="_xlnm.Print_Titles" localSheetId="0">Sheet!$1:$1</definedName><definedName name="_xlnm._FilterDatabase" hidden="0" localSheetId="0">Sheet!$A$1:$F$6332</definedName></definedNames>';
  398. return XML_TAG + this._getXMLTag("workbook", [{
  399. name: "xmlns:r",
  400. value: OPEN_XML_FORMAT_URL + "/officeDocument/2006/relationships"
  401. }, {
  402. name: "xmlns",
  403. value: OPEN_XML_FORMAT_URL + "/spreadsheetml/2006/main"
  404. }], content)
  405. },
  406. _getContentTypesContent: function() {
  407. return XML_TAG + '<Types xmlns="' + OPEN_XML_FORMAT_URL + '/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" /><Default Extension="xml" ContentType="application/xml" /><Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" /><Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml" /><Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml" /><Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml" /></Types>'
  408. },
  409. _generateStylesXML: function() {
  410. var that = this;
  411. var folder = that._zip.folder(XL_FOLDER_NAME);
  412. var XML = "";
  413. XML += this._excelFile.generateNumberFormatsXml();
  414. XML += this._excelFile.generateFontsXml();
  415. XML += this._excelFile.generateFillsXml();
  416. XML += BASE_STYLE_XML2;
  417. XML += this._excelFile.generateCellFormatsXml();
  418. XML += that._getXMLTag("cellStyles", [{
  419. name: "count",
  420. value: 1
  421. }], that._getXMLTag("cellStyle", [{
  422. name: "name",
  423. value: "Normal"
  424. }, {
  425. name: "xfId",
  426. value: 0
  427. }, {
  428. name: "builtinId",
  429. value: 0
  430. }]));
  431. XML = XML_TAG + that._getXMLTag("styleSheet", [{
  432. name: "xmlns",
  433. value: OPEN_XML_FORMAT_URL + "/spreadsheetml/2006/main"
  434. }], XML);
  435. folder.file(STYLE_FILE_NAME, XML);
  436. that._styleArray = []
  437. },
  438. _generateStringsXML: function() {
  439. var folder = this._zip.folder(XL_FOLDER_NAME);
  440. var stringIndex;
  441. var stringsLength = this._stringArray.length;
  442. var sharedStringXml = XML_TAG;
  443. for (stringIndex = 0; stringIndex < stringsLength; stringIndex++) {
  444. this._stringArray[stringIndex] = this._getXMLTag("si", [], this._getXMLTag("t", [], this._stringArray[stringIndex]))
  445. }
  446. sharedStringXml += this._getXMLTag("sst", [{
  447. name: "xmlns",
  448. value: OPEN_XML_FORMAT_URL + "/spreadsheetml/2006/main"
  449. }, {
  450. name: "count",
  451. value: this._stringArray.length
  452. }, {
  453. name: "uniqueCount",
  454. value: this._stringArray.length
  455. }], this._stringArray.join(""));
  456. folder.file(SHAREDSTRING_FILE_NAME, sharedStringXml);
  457. this._stringArray = []
  458. },
  459. _getPaneXML: function() {
  460. var attributes = [{
  461. name: "activePane",
  462. value: "bottomLeft"
  463. }, {
  464. name: "state",
  465. value: "frozen"
  466. }];
  467. var frozenArea = this._dataProvider.getFrozenArea();
  468. if (!(frozenArea.x || frozenArea.y)) {
  469. return ""
  470. }
  471. if (frozenArea.x) {
  472. attributes.push({
  473. name: "xSplit",
  474. value: frozenArea.x
  475. })
  476. }
  477. if (frozenArea.y) {
  478. attributes.push({
  479. name: "ySplit",
  480. value: frozenArea.y
  481. })
  482. }
  483. attributes.push({
  484. name: "topLeftCell",
  485. value: this._convertToExcelCellRefAndTrackMaxIndex(frozenArea.y, frozenArea.x)
  486. });
  487. return this._getXMLTag("pane", attributes)
  488. },
  489. _getAutoFilterXML: function(maxCellIndex) {
  490. if (this._options.autoFilterEnabled) {
  491. return '<autoFilter ref="A' + this._dataProvider.getHeaderRowCount() + ":" + maxCellIndex + '" />'
  492. }
  493. return ""
  494. },
  495. _getIgnoredErrorsXML: function(maxCellIndex) {
  496. if (this._options.ignoreErrors) {
  497. return '<ignoredErrors><ignoredError sqref="A1:' + maxCellIndex + '" numberStoredAsText="1" /></ignoredErrors>'
  498. }
  499. return ""
  500. },
  501. _generateWorksheetXML: function() {
  502. var colIndex;
  503. var rowIndex;
  504. var cellData;
  505. var xmlCells;
  506. var xmlRows = [];
  507. var rowsLength = this._cellsArray.length;
  508. var cellsLength;
  509. var colsLength = this._colsArray.length;
  510. var rSpans = "1:" + colsLength;
  511. var headerRowCount = this._dataProvider.getHeaderRowCount ? this._dataProvider.getHeaderRowCount() : 1;
  512. var xmlResult = [WORKSHEET_HEADER_XML];
  513. xmlResult.push(this._needSheetPr ? GROUP_SHEET_PR_XML : SINGLE_SHEET_PR_XML);
  514. xmlResult.push('<dimension ref="A1:C1"/>');
  515. xmlResult.push("<sheetViews><sheetView ");
  516. xmlResult.push(this._rtlEnabled ? 'rightToLeft="1" ' : "");
  517. xmlResult.push('tabSelected="1" workbookViewId="0">');
  518. xmlResult.push(this._getPaneXML());
  519. xmlResult.push("</sheetView></sheetViews>");
  520. xmlResult.push('<sheetFormatPr defaultRowHeight="15"');
  521. xmlResult.push(' outlineLevelRow="' + (this._dataProvider.getRowsCount() > 0 ? this._dataProvider.getGroupLevel(0) : 0) + '"');
  522. xmlResult.push(' x14ac:dyDescent="0.25"/>');
  523. for (colIndex = 0; colIndex < colsLength; colIndex++) {
  524. this._colsArray[colIndex] = this._getXMLTag("col", [{
  525. name: "width",
  526. value: this._colsArray[colIndex]
  527. }, {
  528. name: "min",
  529. value: Number(colIndex) + 1
  530. }, {
  531. name: "max",
  532. value: Number(colIndex) + 1
  533. }])
  534. }
  535. xmlResult.push(this._getXMLTag("cols", [], this._colsArray.join("")) + "<sheetData>");
  536. for (rowIndex = 0; rowIndex < rowsLength; rowIndex++) {
  537. xmlCells = [];
  538. cellsLength = this._cellsArray[rowIndex].length;
  539. for (colIndex = 0; colIndex < cellsLength; colIndex++) {
  540. rowIndex = Number(rowIndex);
  541. cellData = this._cellsArray[rowIndex][colIndex];
  542. xmlCells.push(this._getXMLTag("c", [{
  543. name: "r",
  544. value: this._convertToExcelCellRefAndTrackMaxIndex(rowIndex, colIndex)
  545. }, {
  546. name: "s",
  547. value: cellData.style
  548. }, {
  549. name: "t",
  550. value: cellData.type
  551. }], (0, _type.isDefined)(cellData.value) ? this._getXMLTag("v", [], cellData.value) : null))
  552. }
  553. xmlRows.push(this._getXMLTag("row", [{
  554. name: "r",
  555. value: Number(rowIndex) + 1
  556. }, {
  557. name: "spans",
  558. value: rSpans
  559. }, {
  560. name: "outlineLevel",
  561. value: rowIndex >= headerRowCount ? this._dataProvider.getGroupLevel(rowIndex) : 0
  562. }, {
  563. name: "x14ac:dyDescent",
  564. value: "0.25"
  565. }], xmlCells.join("")));
  566. this._cellsArray[rowIndex] = null;
  567. if (xmlRows.length > 1e4) {
  568. xmlResult.push(xmlRows.join(""));
  569. xmlRows = []
  570. }
  571. }
  572. xmlResult.push(xmlRows.join(""));
  573. xmlRows = [];
  574. var rightBottomCellRef = this._convertToExcelCellRef(this._maxRowIndex, this._maxColumnIndex);
  575. xmlResult.push("</sheetData>" + this._getAutoFilterXML(rightBottomCellRef) + this._generateMergingXML() + this._getIgnoredErrorsXML(rightBottomCellRef) + "</worksheet>");
  576. this._zip.folder(XL_FOLDER_NAME).folder(WORKSHEETS_FOLDER).file(WORKSHEET_FILE_NAME, xmlResult.join(""));
  577. this._colsArray = [];
  578. this._cellsArray = [];
  579. xmlResult = []
  580. },
  581. _generateMergingXML: function() {
  582. var k;
  583. var l;
  584. var cellIndex;
  585. var rowIndex;
  586. var rowsLength = (0, _type.isDefined)(this._dataProvider.getHeaderRowCount) ? this._dataProvider.getHeaderRowCount() : this._dataProvider.getRowsCount();
  587. var columnsLength = this._dataProvider.getColumns().length;
  588. var usedArea = [];
  589. var mergeArray = [];
  590. var mergeIndex;
  591. var mergeXML = "";
  592. for (rowIndex = 0; rowIndex < rowsLength; rowIndex++) {
  593. for (cellIndex = 0; cellIndex !== columnsLength; cellIndex++) {
  594. if (!(0, _type.isDefined)(usedArea[rowIndex]) || !(0, _type.isDefined)(usedArea[rowIndex][cellIndex])) {
  595. var cellMerge = this._dataProvider.getCellMerging(rowIndex, cellIndex);
  596. if (cellMerge.colspan || cellMerge.rowspan) {
  597. mergeArray.push({
  598. start: this._convertToExcelCellRefAndTrackMaxIndex(rowIndex, cellIndex),
  599. end: this._convertToExcelCellRefAndTrackMaxIndex(rowIndex + (cellMerge.rowspan || 0), cellIndex + (cellMerge.colspan || 0))
  600. });
  601. for (k = rowIndex; k <= rowIndex + cellMerge.rowspan || 0; k++) {
  602. for (l = cellIndex; l <= cellIndex + cellMerge.colspan || 0; l++) {
  603. if (!(0, _type.isDefined)(usedArea[k])) {
  604. usedArea[k] = []
  605. }
  606. usedArea[k][l] = true
  607. }
  608. }
  609. }
  610. }
  611. }
  612. }
  613. var mergeArrayLength = mergeArray.length;
  614. for (mergeIndex = 0; mergeIndex < mergeArrayLength; mergeIndex++) {
  615. mergeXML += this._getXMLTag("mergeCell", [{
  616. name: "ref",
  617. value: mergeArray[mergeIndex].start + ":" + mergeArray[mergeIndex].end
  618. }])
  619. }
  620. return mergeXML.length ? this._getXMLTag("mergeCells", [{
  621. name: "count",
  622. value: mergeArrayLength
  623. }], mergeXML) : ""
  624. },
  625. _generateCommonXML: function() {
  626. var relsFileContent = XML_TAG + this._createXMLRelationships(this._createXMLRelationship(1, "officeDocument", "xl/" + WORKBOOK_FILE_NAME));
  627. var folder = this._zip.folder(XL_FOLDER_NAME);
  628. var relsXML = XML_TAG;
  629. this._zip.folder("_" + RELATIONSHIP_PART_NAME).file("." + RELATIONSHIP_PART_NAME, relsFileContent);
  630. var xmlRelationships = this._createXMLRelationship(1, "worksheet", "worksheets/" + WORKSHEET_FILE_NAME) + this._createXMLRelationship(2, "styles", STYLE_FILE_NAME) + this._createXMLRelationship(3, "sharedStrings", SHAREDSTRING_FILE_NAME);
  631. relsXML += this._createXMLRelationships(xmlRelationships);
  632. folder.folder("_" + RELATIONSHIP_PART_NAME).file(WORKBOOK_FILE_NAME + ".rels", relsXML);
  633. folder.file(WORKBOOK_FILE_NAME, this._getWorkbookContent());
  634. this._zip.file(CONTENTTYPES_FILE_NAME, this._getContentTypesContent())
  635. },
  636. _generateContent: function() {
  637. this._prepareStyleData();
  638. this._prepareCellData();
  639. this._generateWorkXML();
  640. this._generateCommonXML()
  641. },
  642. _generateWorkXML: function() {
  643. this._generateStylesXML();
  644. this._generateStringsXML();
  645. this._generateWorksheetXML()
  646. },
  647. ctor: function(dataProvider, options) {
  648. this._rtlEnabled = options && !!options.rtlEnabled;
  649. this._options = options;
  650. this._maxRowIndex = 0;
  651. this._maxColumnIndex = 0;
  652. this._stringArray = [];
  653. this._stringHash = {};
  654. this._styleArray = [];
  655. this._colsArray = [];
  656. this._cellsArray = [];
  657. this._needSheetPr = false;
  658. this._dataProvider = dataProvider;
  659. this._excelFile = new _excel2.default;
  660. if ((0, _type.isDefined)(ExcelCreator.JSZip)) {
  661. this._zip = new ExcelCreator.JSZip
  662. } else {
  663. this._zip = null
  664. }
  665. },
  666. _checkZipState: function() {
  667. if (!this._zip) {
  668. throw _ui2.default.Error("E1041", "JSZip")
  669. }
  670. },
  671. ready: function() {
  672. return this._dataProvider.ready()
  673. },
  674. getData: function(isBlob) {
  675. var options = {
  676. type: isBlob ? "blob" : "base64",
  677. compression: "DEFLATE",
  678. mimeType: _file_saver2.default.MIME_TYPES.EXCEL
  679. };
  680. this._checkZipState();
  681. this._generateContent();
  682. return this._zip.generateAsync ? this._zip.generateAsync(options) : this._zip.generate(options)
  683. }
  684. });
  685. ExcelCreator.JSZip = _jszip2.default;
  686. exports.ExcelCreator = ExcelCreator;
  687. exports.getData = function(data, options, callback) {
  688. var excelCreator = new exports.ExcelCreator(data, options);
  689. excelCreator._checkZipState();
  690. excelCreator.ready().done(function() {
  691. if (excelCreator._zip.generateAsync) {
  692. excelCreator.getData((0, _type.isFunction)((0, _window.getWindow)().Blob)).then(callback)
  693. } else {
  694. callback(excelCreator.getData((0, _type.isFunction)((0, _window.getWindow)().Blob)))
  695. }
  696. })
  697. };