rpt_excel_util.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /**
  2. * Created by Tony on 2017/4/1.
  3. */
  4. var JV = require('../rpt_component/Jpc_ValueDefine');
  5. var fs = require('fs');
  6. var JSZip = require("jszip");
  7. var strUtil = require('../../../public/stringUtil');
  8. var jpcCmnHelper = require('../rpt_component/helper/Jpc_Helper_Common');
  9. var DPI = jpcCmnHelper.getScreenDPI()[0];
  10. const dftHeadXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
  11. function writeContentTypes(sheets) {
  12. var rst = [];
  13. rst.push(dftHeadXml + '\r\n');
  14. rst.push('<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">');
  15. //...
  16. rst.push('<Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/>');
  17. rst.push('<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>');
  18. rst.push('<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>');
  19. rst.push('<Default Extension="xml" ContentType="application/xml"/>');
  20. rst.push('<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>');
  21. rst.push('<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>');
  22. rst.push('<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>');
  23. for (var i = 0; i < sheets.length; i++) {
  24. rst.push('<Override PartName="/xl/worksheets/sheet' + (i + 1) + '.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>')
  25. }
  26. rst.push('<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>');
  27. rst.push('</Types>');
  28. return rst;
  29. }
  30. function writeRootRels(){
  31. var rst = [];
  32. rst.push(dftHeadXml + '\r\n');
  33. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  34. rst.push('<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>');
  35. rst.push('<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>');
  36. rst.push('<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>');
  37. rst.push('</Relationships>');
  38. return rst;
  39. }
  40. function writeApp(sheets) {
  41. var rst = [];
  42. rst.push(dftHeadXml + '\r\n');
  43. rst.push('<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">');
  44. rst.push('<Application>Microsoft Excel</Application>');
  45. rst.push('<DocSecurity>0</DocSecurity>');
  46. rst.push('<ScaleCrop>false</ScaleCrop>');
  47. rst.push('<HeadingPairs>');
  48. rst.push('<vt:vector size="2" baseType="variant">');
  49. rst.push('<vt:variant><vt:lpstr>工作表</vt:lpstr></vt:variant>');
  50. rst.push('<vt:variant><vt:i4>' + sheets.length + '</vt:i4></vt:variant>');
  51. rst.push('</vt:vector>');
  52. rst.push('</HeadingPairs>');
  53. rst.push('<TitlesOfParts>');
  54. rst.push('<vt:vector size="' + sheets.length + '" baseType="lpstr">');
  55. for (var i = 0; i < sheets.length; i++) {
  56. rst.push('<vt:lpstr>' + sheets[i].sheetName + '</vt:lpstr>')
  57. }
  58. rst.push('</vt:vector>');
  59. rst.push('</TitlesOfParts>');
  60. rst.push('<Company>SmartCost</Company>');
  61. rst.push('<LinksUpToDate>false</LinksUpToDate>');
  62. rst.push('<SharedDoc>false</SharedDoc>');
  63. rst.push('<HyperlinksChanged>false</HyperlinksChanged>');
  64. rst.push('<AppVersion>12.0000</AppVersion>');
  65. //rst.push('');
  66. rst.push('</Properties>');
  67. return rst;
  68. }
  69. function writeCore() {
  70. var rst = [];
  71. p_fillZero = function(val){
  72. var rst = val;
  73. if (val < 10) {
  74. rst = '0' + val;
  75. }
  76. return rst;
  77. };
  78. rst.push(dftHeadXml + '\r\n');
  79. rst.push('<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">');
  80. rst.push('<dc:creator>SmartCost</dc:creator>');
  81. rst.push('<cp:lastModifiedBy>SmartCost</cp:lastModifiedBy>');
  82. var dt = new Date(), dtStr = dt.getFullYear() + '-' + p_fillZero(dt.getMonth()+1) + '-' + p_fillZero(dt.getDate()) + 'T' +
  83. p_fillZero(dt.getHours()) + ':' + p_fillZero(dt.getMinutes()) + ':' + p_fillZero(dt.getSeconds()) + 'Z';
  84. rst.push('<dcterms:created xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:created>');
  85. rst.push('<dcterms:modified xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:modified>');
  86. //rst.push('');
  87. rst.push('</cp:coreProperties>');
  88. return rst;
  89. }
  90. function writeXlWorkBook(sheets){
  91. var rst = [];
  92. rst.push(dftHeadXml + '\r\n');
  93. rst.push('<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  94. rst.push('<fileVersion appName="xl" lastEdited="4" lowestEdited="4" rupBuild="4505"/>');
  95. rst.push('<workbookPr defaultThemeVersion="124226"/>');
  96. rst.push('<bookViews><workbookView xWindow="360" yWindow="345" windowWidth="14655" windowHeight="4305"/></bookViews>');
  97. rst.push('<sheets>');
  98. for (var i = 0; i < sheets.length; i++) {
  99. rst.push('<sheet name="' + sheets[i].sheetName + '" sheetId="' + (i + 1) + '" r:id="rId' + (i + 1) + '"/>')
  100. }
  101. rst.push('</sheets>');
  102. rst.push('<calcPr calcId="124519"/>');
  103. //rst.push('');
  104. rst.push('</workbook>');
  105. return rst;
  106. }
  107. function writeXlRels(sheets){
  108. var rst = [], idx = 1;
  109. rst.push(dftHeadXml + '\r\n');
  110. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  111. for (var i = 0; i < sheets.length; i++) {
  112. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet' + (i + 1) + '.xml"/>')
  113. idx++;
  114. }
  115. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>');
  116. idx++;
  117. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>');
  118. idx++;
  119. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/>');
  120. //rst.push('');
  121. rst.push('</Relationships>');
  122. return rst;
  123. }
  124. function writeTheme(){
  125. var rst = fs.readFileSync(__dirname + '/excel_base_files/theme1.xml', 'utf8', 'r');
  126. return rst;
  127. }
  128. function writeStyles(stylesObj){
  129. var rst = [];
  130. rst.push(dftHeadXml + '\r\n');
  131. rst.push('<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">');
  132. //1. push fonts
  133. rst.push('<fonts count="' + stylesObj.fonts.length + '">')
  134. for (var i = 0; i < stylesObj.fonts.length; i++) {
  135. var font = stylesObj.fonts[i];
  136. rst.push('<font>');
  137. if (strUtil.convertStrToBoolean(font[JV.FONT_PROPS[3]])) {
  138. rst.push('<b/>');
  139. }
  140. rst.push('<sz val="' + font.size + '"/>');
  141. rst.push('<color indexed="' + font.colorIdx + '"/>');
  142. rst.push('<name val="' + font[JV.FONT_PROPS[0]] + '"/>');
  143. rst.push('<charset val="' + font.charset + '"/>');
  144. rst.push('</font>');
  145. }
  146. rst.push('</fonts>');
  147. //2. push default fills
  148. rst.push('<fills count="2"><fill><patternFill patternType="none" /></fill><fill><patternFill patternType="gray125" /></fill></fills>');
  149. //3. push borders
  150. rst.push('<borders count="' + stylesObj.borders.length + '">')
  151. private_setBorder = function(border, borderDirection) {
  152. if (border[borderDirection][JV.PROP_LINE_WEIGHT] == 0) {
  153. rst.push('<' + borderDirection.toLowerCase() + '/>');
  154. } else {
  155. var bW = 'thin';
  156. if (border[borderDirection][JV.PROP_LINE_WEIGHT] == 2) bW = 'medium';
  157. if (border[borderDirection][JV.PROP_LINE_WEIGHT] > 2) bW = 'thick';
  158. rst.push('<' + borderDirection.toLowerCase() + ' style="' + bW + '">' + '<color indexed="64"/>' + '</' + borderDirection.toLowerCase() + '>');
  159. }
  160. };
  161. for (var i = 0; i < stylesObj.borders.length; i++) {
  162. var border = stylesObj.borders[i];
  163. rst.push('<border>');
  164. private_setBorder(border, JV.PROP_LEFT);
  165. private_setBorder(border, JV.PROP_RIGHT);
  166. private_setBorder(border, JV.PROP_TOP);
  167. private_setBorder(border, JV.PROP_BOTTOM);
  168. rst.push('<diagonal />');
  169. rst.push('</border>');
  170. }
  171. rst.push('</borders>');
  172. //4. push cellStyleXfs
  173. rst.push('<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"><alignment vertical="center"/></xf></cellStyleXfs>');
  174. //5. push cellXfs
  175. rst.push('<cellXfs count="' + stylesObj.cellXfs.length + '">');
  176. for (var i = 0; i < stylesObj.cellXfs.length; i++) {
  177. var excelStyle = stylesObj.cellXfs[i];
  178. rst.push('<xf numFmtId="0" fontId="' + excelStyle.fontId + '" fillId="0" borderId="' + excelStyle.borderId + '" xfId="0">');
  179. var alignStr = '<alignment horizontal="' + excelStyle[JV.CONTROL_PROPS[2]] + '" vertical="' + excelStyle[JV.CONTROL_PROPS[3]] + '"';
  180. if (strUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[1]])) {
  181. alignStr = alignStr + ' shrinkToFit="1"';
  182. }
  183. if (strUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[4]])) {
  184. alignStr = alignStr + ' wrapText="1"';
  185. }
  186. alignStr = alignStr + '/>';
  187. rst.push(alignStr);
  188. rst.push('</xf>');
  189. }
  190. rst.push('</cellXfs>');
  191. //6. others (xfl style / dxfs / tableStyles)
  192. rst.push('<cellStyles count="1"><cellStyle name="常规" xfId="0" builtinId="0"/></cellStyles>');
  193. rst.push('<dxfs count="0"/>');
  194. rst.push('<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleLight16"/>');
  195. rst.push('</styleSheet>');
  196. return rst;
  197. }
  198. function writeSharedString(sharedStrList){
  199. var rst = [];
  200. if (sharedStrList && sharedStrList.length > 0) {
  201. rst.push(dftHeadXml + '\r\n');
  202. rst.push('<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="' + sharedStrList.length + '" uniqueCount="' + sharedStrList.length + '">');
  203. for (var i = 0; i < sharedStrList.length; i++) {
  204. rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
  205. }
  206. rst.push('</sst>');
  207. }
  208. return rst;
  209. }
  210. function writeSheets(pageData, sharedStrList, stylesObj){
  211. var rst = [];
  212. private_pushDftFont = function(){
  213. var font = {};
  214. if (!(stylesObj.fonts)) {
  215. stylesObj.fonts = [];
  216. }
  217. font[JV.FONT_PROPS[0]] = "宋体"; //font name
  218. font.size = 11;
  219. font.charset = 134;
  220. font.colorIdx = "8";
  221. stylesObj.fonts.push(font);
  222. };
  223. private_pushDftFont();
  224. for (var i = 0; i < pageData.items.length; i++) {
  225. rst.push(writeSheet(pageData, pageData.items[i], sharedStrList, stylesObj));
  226. }
  227. return rst;
  228. }
  229. function writeSheet(pageData, sheetData, sharedStrList, stylesObj){
  230. var rst = [], xPos = [], yPos = [], headerStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  231. var cacheBorderCell = {};
  232. xPos.push(0);
  233. yPos.push(0);
  234. private_pre_analyze_pos = function(){
  235. var cell, pos;
  236. sheetData.cells.sort(function(cell1, cell2) {
  237. var rst = 0;
  238. if (cell1[JV.PROP_AREA][JV.PROP_TOP] > cell2[JV.PROP_AREA][JV.PROP_TOP]) {
  239. rst = 1;
  240. } else if (cell1[JV.PROP_AREA][JV.PROP_TOP] < cell2[JV.PROP_AREA][JV.PROP_TOP]) {
  241. rst = -1;
  242. } else {
  243. if (cell1[JV.PROP_AREA][JV.PROP_LEFT] > cell2[JV.PROP_AREA][JV.PROP_LEFT]) {
  244. rst = 1;
  245. } else if (cell1[JV.PROP_AREA][JV.PROP_LEFT] < cell2[JV.PROP_AREA][JV.PROP_LEFT]) {
  246. rst = -1;
  247. }
  248. }
  249. return rst;
  250. });
  251. for (var i = 0; i < sheetData.cells.length; i++) {
  252. cell = sheetData.cells[i];
  253. pos = cell[JV.PROP_AREA][JV.PROP_LEFT];
  254. if (xPos.indexOf(pos) < 0) xPos.push(pos);
  255. pos = cell[JV.PROP_AREA][JV.PROP_RIGHT];
  256. if (xPos.indexOf(pos) < 0) xPos.push(pos);
  257. pos = cell[JV.PROP_AREA][JV.PROP_TOP];
  258. if (yPos.indexOf(pos) < 0) yPos.push(pos);
  259. pos = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  260. if (yPos.indexOf(pos) < 0) yPos.push(pos);
  261. }
  262. xPos.sort(private_array_sort);
  263. yPos.sort(private_array_sort);
  264. };
  265. private_array_sort = function(i1, i2){
  266. var rst = 0;
  267. if (i1 > i2) {rst = 1} else
  268. if (i1 < i2) rst = -1;
  269. return rst;
  270. };
  271. private_getCellIdxStr = function(idx){
  272. var rst = 'A';
  273. if (idx < 26) {
  274. rst = headerStr[idx];
  275. } else if (idx < 26*26+26) {
  276. var ti = Math.floor(idx / 26), tj = idx % 26;
  277. rst = headerStr[ti - 1] + headerStr[tj];
  278. } else if (idx < 26*26*26+26) {
  279. var ti = Math.floor(idx / (26*26)), tj = Math.floor((idx - ti * 26*26) / 26), tk = idx % 26;
  280. rst = headerStr[ti - 1] + headerStr[tj-1] + headerStr[tk];
  281. }
  282. return rst;
  283. };
  284. private_getSharedStrIdx = function(val) {
  285. var rst = sharedStrList.indexOf(val);
  286. if (rst < 0) {
  287. sharedStrList.push(val);
  288. rst = sharedStrList.length - 1;
  289. }
  290. return rst;
  291. };
  292. private_getFontId = function(cell) {
  293. var rst = 0, hasFont = false;
  294. if (!(stylesObj.fonts)) {
  295. stylesObj.fonts = [];
  296. //for (var i = 0; i < sheetData.font_collection)
  297. }
  298. var sheetFont = pageData.font_collection[cell.font];
  299. for (var i = 0; i < stylesObj.fonts.length; i++) {
  300. var font = stylesObj.fonts[i];
  301. if (sheetFont) {
  302. if (font[JV.FONT_PROPS[0]] === sheetFont[JV.FONT_PROPS[0]] && font.size === Math.round(sheetFont[JV.FONT_PROPS[1]] * 3 / 4) && font[JV.FONT_PROPS[3]] == sheetFont[JV.FONT_PROPS[3]]) {
  303. hasFont = true;
  304. rst = i;
  305. break;
  306. }
  307. } else {
  308. break;
  309. }
  310. }
  311. if (!hasFont) {
  312. var font = {};
  313. font[JV.FONT_PROPS[0]] = sheetFont[JV.FONT_PROPS[0]]; //font name
  314. font.size = Math.round(sheetFont[JV.FONT_PROPS[1]] * 3 / 4);
  315. font.charset = 134;
  316. font.colorIdx = "8";
  317. font[JV.FONT_PROPS[3]] = sheetFont[JV.FONT_PROPS[3]]; //font bold
  318. stylesObj.fonts.push(font);
  319. rst = stylesObj.fonts.length - 1;
  320. }
  321. return rst;
  322. };
  323. private_checkBorder = function(cell, border, sheetBorder) {
  324. var rst = true, borderLineWidths = [], sheetBorderLineWidths = [];
  325. borderLineWidths.push(border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]);
  326. borderLineWidths.push(border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]);
  327. borderLineWidths.push(border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]);
  328. borderLineWidths.push(border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]);
  329. if (sheetBorder[JV.PROP_LEFT] && sheetBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]) {
  330. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_LEFT));
  331. } else {
  332. sheetBorderLineWidths.push(0);
  333. }
  334. if (sheetBorder[JV.PROP_RIGHT] && sheetBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]) {
  335. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_RIGHT));
  336. } else {
  337. sheetBorderLineWidths.push(0);
  338. }
  339. if (sheetBorder[JV.PROP_TOP] && sheetBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]) {
  340. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_TOP));
  341. } else {
  342. sheetBorderLineWidths.push(0);
  343. }
  344. if (sheetBorder[JV.PROP_BOTTOM] && sheetBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]) {
  345. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_BOTTOM));
  346. } else {
  347. sheetBorderLineWidths.push(0);
  348. }
  349. for (var i = 0; i < 4; i++) {
  350. if (borderLineWidths[i] != sheetBorderLineWidths[i]) {
  351. rst = false;
  352. break;
  353. }
  354. }
  355. return rst;
  356. };
  357. private_chkAndGetMergeLine = function(cell, sheetBorder, borderStr) {
  358. var rst = sheetBorder[borderStr][JV.PROP_LINE_WEIGHT], mergeBorder = pageData[JV.BAND_PROP_MERGE_BAND];
  359. if (cell[JV.PROP_AREA][borderStr] == mergeBorder[borderStr]) {
  360. var destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBorder[JV.PROP_STYLE][JV.PROP_ID]];
  361. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  362. }
  363. return parseInt(rst);
  364. };
  365. private_getIniBorder = function() {
  366. var rst = {};
  367. rst[JV.PROP_LEFT] = {};
  368. rst[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = 0;
  369. rst[JV.PROP_RIGHT] = {};
  370. rst[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = 0;
  371. rst[JV.PROP_TOP] = {};
  372. rst[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = 0;
  373. rst[JV.PROP_BOTTOM] = {};
  374. rst[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = 0;
  375. return rst;
  376. };
  377. private_getBorderId = function(cell) {
  378. var rst = 0, hasBorder = false;
  379. if (!(stylesObj.borders)) {
  380. stylesObj.borders = [];
  381. }
  382. var sheetBorder = pageData[JV.NODE_STYLE_COLLECTION][cell.style];
  383. for (var i = 0; i < stylesObj.borders.length; i++) {
  384. var border = stylesObj.borders[i];
  385. if (private_checkBorder(cell, border, sheetBorder)) {
  386. hasBorder = true;
  387. rst = i;
  388. break;
  389. }
  390. }
  391. if (!hasBorder) {
  392. var border = private_getIniBorder();
  393. if (sheetBorder && sheetBorder[JV.PROP_LEFT]) {
  394. border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_LEFT);
  395. }
  396. if (sheetBorder && sheetBorder[JV.PROP_RIGHT]) {
  397. border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_RIGHT);
  398. }
  399. if (sheetBorder && sheetBorder[JV.PROP_TOP]) {
  400. border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_TOP);
  401. }
  402. if (sheetBorder && sheetBorder[JV.PROP_BOTTOM]) {
  403. border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_BOTTOM);
  404. }
  405. stylesObj.borders.push(border);
  406. rst = stylesObj.borders.length - 1;
  407. }
  408. return rst;
  409. };
  410. private_checkControl = function(cellControl, sheetControl) {
  411. var rst = true;
  412. for (var i = 0; i < JV.CONTROL_PROPS.length; i++) {
  413. if (cellControl[JV.CONTROL_PROPS[i]] != sheetControl[JV.CONTROL_PROPS[i]]) {
  414. rst = false;
  415. break;
  416. }
  417. }
  418. return rst;
  419. };
  420. private_getStyleId = function(cell) {
  421. var rst = 1, hasStyle = false;
  422. if (!(stylesObj.cellXfs)) stylesObj.cellXfs = [];
  423. var fontId = private_getFontId(cell);
  424. var borderId = private_getBorderId(cell);
  425. var cellControl = pageData[JV.NODE_CONTROL_COLLECTION][cell[JV.PROP_CONTROL]];
  426. for (var i = 0; i < stylesObj.cellXfs.length; i++) {
  427. var sheetControl = stylesObj.cellXfs[i];
  428. if (sheetControl.fontId == fontId && sheetControl.borderId == borderId) {
  429. if (private_checkControl(cellControl, sheetControl)) {
  430. rst = i;
  431. hasStyle = true;
  432. break;
  433. }
  434. }
  435. }
  436. if (!hasStyle) {
  437. var sheetControl = {};
  438. sheetControl.fontId = fontId;
  439. sheetControl.borderId = borderId;
  440. for (var i = 0; i < JV.CONTROL_PROPS.length; i++) {
  441. sheetControl[JV.CONTROL_PROPS[i]] = cellControl[JV.CONTROL_PROPS[i]];
  442. }
  443. stylesObj.cellXfs.push(sheetControl);
  444. rst = stylesObj.cellXfs.length - 1;
  445. }
  446. return rst;
  447. };
  448. private_setCols = function(){
  449. //remark: 1 excel width = 2.117 mm
  450. rst.push('<cols>');
  451. var w = 0;
  452. for (var i = 1; i < xPos.length - 1; i++) {
  453. w = 1.0 * (xPos[i + 1] - xPos[i]) / DPI * 25.4 / 2.117;
  454. w = Math.round(w * 1000) / 1000;
  455. rst.push('<col min="' + i +'" max="' + i +'" width="' + w + '" customWidth="1"/>');
  456. }
  457. rst.push('</cols>');
  458. };
  459. private_setMergedCells = function() {
  460. var cell, idxR, idxL, idxT, idxB, cnt = 0;
  461. rst.push('<mergeCells count="?">');
  462. var startIdx = rst.length - 1;
  463. for (var i = 0; i < sheetData.cells.length; i++) {
  464. cell = sheetData.cells[i];
  465. idxR = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_RIGHT]);
  466. idxL = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_LEFT]);
  467. idxB = yPos.indexOf(cell[JV.PROP_AREA][JV.PROP_BOTTOM]);
  468. idxT = yPos.indexOf(cell[JV.PROP_AREA][JV.PROP_TOP]);
  469. if (idxR - idxL > 1 || idxB - idxT > 1) {
  470. rst.push('<mergeCell ref="' + private_getCellIdxStr(idxL - 1) + idxT + ':' + private_getCellIdxStr(idxR - 2) + (idxB - 1) + '"/>');
  471. cnt++;
  472. }
  473. }
  474. rst[startIdx] = '<mergeCells count="' + cnt + '">';
  475. rst.push('</mergeCells>');
  476. };
  477. private_chkIfNeedCacheCell = function(cell){
  478. var rst = false;
  479. if (cell[JV.PROP_AREA][JV.PROP_LEFT] == pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_LEFT] ||
  480. cell[JV.PROP_AREA][JV.PROP_RIGHT] == pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_RIGHT] ||
  481. cell[JV.PROP_AREA][JV.PROP_TOP] == pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_TOP] ||
  482. cell[JV.PROP_AREA][JV.PROP_BOTTOM] == pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_BOTTOM]){
  483. if (cell[JV.PROP_AREA][JV.PROP_LEFT] >= pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_LEFT] &&
  484. cell[JV.PROP_AREA][JV.PROP_RIGHT] <= pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_RIGHT] &&
  485. cell[JV.PROP_AREA][JV.PROP_TOP] >= pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_TOP] &&
  486. cell[JV.PROP_AREA][JV.PROP_BOTTOM] <= pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_BOTTOM]) {
  487. rst = true;
  488. }
  489. }
  490. return rst;
  491. };
  492. private_cacheMergeBandBorderIdxs = function() {
  493. var cell, idxR, idxL, idxT, idxB;
  494. for (var i = 0; i < sheetData.cells.length; i++) {
  495. cell = sheetData.cells[i];
  496. idxR = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_RIGHT]);
  497. idxL = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_LEFT]);
  498. idxB = yPos.indexOf(cell[JV.PROP_AREA][JV.PROP_BOTTOM]);
  499. idxT = yPos.indexOf(cell[JV.PROP_AREA][JV.PROP_TOP]);
  500. if (idxR - idxL > 1 || idxB - idxT > 1) {
  501. if (private_chkIfNeedCacheCell(cell)) {
  502. for (var xi = idxL; xi < idxR; xi++) {
  503. for (var yj = idxT; yj < idxB; yj++) {
  504. cacheBorderCell[private_getCellIdxStr(xi - 1) + yj] = cell;
  505. }
  506. }
  507. }
  508. }
  509. }
  510. };
  511. private_getMergedCellStyleId = function(preStyleId, colIdxStr) {
  512. var rst = preStyleId;
  513. if (cacheBorderCell[colIdxStr]) {
  514. rst = private_getStyleId(cacheBorderCell[colIdxStr]);
  515. }
  516. return rst;
  517. };
  518. private_setSheetData = function(){
  519. //remark: 1 excel height = 0.3612 mm
  520. rst.push('<sheetData>');
  521. var spanX = xPos.length - 2, cellIdx = 0, h = 0,
  522. hasMoreCols = true, nextColIdx = -1,
  523. nextRowIdx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP])
  524. ;
  525. private_cacheMergeBandBorderIdxs();
  526. for (var i = 1; i < yPos.length - 1; i++) {
  527. h = 1.0 * (yPos[i+1] - yPos[i]) / DPI * 25.4 / 0.3612;
  528. h = Math.round(h * 1000) / 1000;
  529. rst.push('<row r="' + i + '" spans="1:' + spanX + '" ht="' + h + '" customHeight="1">');
  530. //then put the cells of this row
  531. var colIdxStr = '';
  532. hasMoreCols = true;
  533. while (nextRowIdx < i) {
  534. if (cellIdx >= sheetData.cells.length || nextRowIdx > i) {
  535. break;
  536. } else {
  537. cellIdx++;
  538. nextRowIdx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
  539. }
  540. }
  541. if (nextRowIdx > i) {
  542. hasMoreCols = false;
  543. }
  544. nextColIdx = xPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_LEFT]);
  545. var preStyleIdx = 1;
  546. for (var j = 1; j < xPos.length - 1; j++) {
  547. colIdxStr = private_getCellIdxStr(j - 1);
  548. if (hasMoreCols) {
  549. if (nextColIdx == j) {
  550. var styleIdx = private_getStyleId(sheetData.cells[cellIdx]);
  551. preStyleIdx = styleIdx;
  552. if (strUtil.isEmptyString(sheetData.cells[cellIdx][JV.PROP_VALUE])) {
  553. rst.push('<c r="' + colIdxStr + i + '" s="' + styleIdx + '"/>');
  554. //should setup the right style instead!
  555. } else {
  556. var valIdx = private_getSharedStrIdx(sheetData.cells[cellIdx][JV.PROP_VALUE]);
  557. rst.push('<c r="' + colIdxStr + i + '" s="' + styleIdx + '" t="s">');
  558. rst.push('<v>' + valIdx + '</v>');
  559. rst.push('</c>');
  560. }
  561. cellIdx++;
  562. if (cellIdx < sheetData.cells.length) {
  563. nextRowIdx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
  564. if (nextRowIdx > i) {
  565. hasMoreCols = false;
  566. } else {
  567. nextColIdx = xPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_LEFT]);
  568. }
  569. } else {
  570. hasMoreCols = false;
  571. }
  572. } else if (nextColIdx < 0) {
  573. //impossible!
  574. console.log('has abnormal case!');
  575. hasMoreCols = false;
  576. } else {
  577. //rst.push('<c r="' + colIdxStr + i + '" s="' + preStyleIdx + '"/>');
  578. rst.push('<c r="' + colIdxStr + i + '" s="' + private_getMergedCellStyleId(preStyleIdx, colIdxStr + i) + '"/>');
  579. }
  580. } else {
  581. //rst.push('<c r="' + colIdxStr + i + '" s="' + preStyleIdx + '"/>');
  582. rst.push('<c r="' + colIdxStr + i + '" s="' + private_getMergedCellStyleId(preStyleIdx, colIdxStr + i) + '"/>');
  583. }
  584. }
  585. rst.push('</row>');
  586. }
  587. //sheetData.cells.length
  588. rst.push('</sheetData>');
  589. };
  590. private_pre_analyze_pos();
  591. rst.push(dftHeadXml + '\r\n');
  592. rst.push('<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  593. var colStr = private_getCellIdxStr(xPos.length - 3);
  594. rst.push('<dimension ref="A1:' + colStr + '' + yPos.length + '"/>');
  595. rst.push('<sheetViews><sheetView tabSelected="1" workbookViewId="0">');
  596. rst.push('<selection sqref="A1:' + colStr + '1"/>');
  597. rst.push('</sheetView></sheetViews>');
  598. rst.push('<sheetFormatPr defaultRowHeight="13.5"/>');
  599. private_setCols();
  600. private_setSheetData();
  601. private_setMergedCells();
  602. rst.push('<phoneticPr fontId="1" type="noConversion"/>');
  603. rst.push('<pageMargins left="0.315" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
  604. //rst.push('<pageSetup paperSize="9" fitToWidth="0" fitToHeight="0" orientation="landscape" horizontalDpi="300" verticalDpi="300"/>');
  605. rst.push('<headerFooter alignWithMargins="0"/>');
  606. rst.push('</worksheet>');
  607. //rst.push('');
  608. return rst;
  609. }
  610. module.exports = {
  611. exportExcel: function (pageData, fName, options) {
  612. var rptOptions = (options || {singlePage: false, fileName: 'report'});
  613. var sheets = [];
  614. for (var i = 0; i < pageData.items.length; i++) {
  615. sheets.push({sheetName: '第' + (i + 1) + '页'});
  616. }
  617. //1.
  618. var file = '[Content_Types].xml';
  619. var data = writeContentTypes(sheets);
  620. var zip = new JSZip();
  621. zip.file(file, data.join(''), {compression: 'DEFLATE'});
  622. //2.
  623. var zip_rels = zip.folder('_rels');
  624. file = '.rels';
  625. data = writeRootRels();
  626. zip_rels.file(file, data.join(''), {compression: 'DEFLATE'});
  627. //3.
  628. var zip_docProps = zip.folder('docProps');
  629. file = 'app.xml';
  630. data = writeApp(sheets);
  631. zip_docProps.file(file, data.join(''), {compression: 'DEFLATE'});
  632. file = 'core.xml';
  633. data = writeCore();
  634. zip_docProps.file(file, data.join(''), {compression: 'DEFLATE'});
  635. //4.
  636. var zip_xl = zip.folder('xl');
  637. file = 'workbook.xml';
  638. data = writeXlWorkBook(sheets);
  639. zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
  640. var zip_rels2 = zip_xl.folder('_rels');
  641. file = 'workbook.xml.rels';
  642. data = writeXlRels(sheets);
  643. zip_rels2.file(file, data.join(''), {compression: 'DEFLATE'});
  644. //5.
  645. var zip_theme = zip_xl.folder('theme');
  646. file = 'theme1.xml';
  647. data = writeTheme();
  648. zip_theme.file(file, data, {compression: 'DEFLATE'});
  649. //6.
  650. var zip_worksheets = zip_xl.folder('worksheets');
  651. var sharedStrList = [], stylesObj = {};
  652. data = writeSheets(pageData, sharedStrList, stylesObj);
  653. for (var i = 0; i < data.length; i++) {
  654. file = 'sheet' + (i + 1) + '.xml';
  655. zip_worksheets.file(file, data[i].join(''), {compression: 'DEFLATE'});
  656. }
  657. file = 'sharedStrings.xml';
  658. data = writeSharedString(sharedStrList);
  659. zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
  660. file = 'styles.xml';
  661. data = writeStyles(stylesObj);
  662. zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
  663. zip.generateNodeStream({type:'nodebuffer',streamFiles:true})
  664. //.pipe(fs.createWriteStream('../../../tmp/outExcel.xlsx'))
  665. .pipe(fs.createWriteStream('../../../tmp/' + fName + '.zip'))
  666. .on('finish', function () {
  667. // JSZip generates a readable stream with a "end" event,
  668. // but is piped here in a writable stream which emits a "finish" event.
  669. console.log("outExcel.xlsx was written.");
  670. }
  671. );
  672. }
  673. }