rpt_excel_util.js 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. /**
  2. * Created by Tony on 2017/4/1.
  3. */
  4. let JV = require('../rpt_component/jpc_value_define');
  5. let fs = require('fs');
  6. let JSZip = require("jszip");
  7. let strUtil = require('../../../public/stringUtil');
  8. let jpcCmnHelper = require('../rpt_component/helper/jpc_helper_common');
  9. let DPI = jpcCmnHelper.getScreenDPI()[0];
  10. let fsUtil = require('../../../public/fsUtil');
  11. const dftHeadXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
  12. function writeContentTypes(sheets, isSinglePage) {
  13. let rst = [];
  14. rst.push(dftHeadXml + '\r\n');
  15. rst.push('<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">');
  16. //...
  17. rst.push('<Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/>');
  18. rst.push('<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>');
  19. rst.push('<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>');
  20. rst.push('<Default Extension="xml" ContentType="application/xml"/>');
  21. rst.push('<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>');
  22. rst.push('<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>');
  23. rst.push('<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>');
  24. if (isSinglePage) {
  25. rst.push('<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>');
  26. } else {
  27. for (let i = 0; i < sheets.length; i++) {
  28. rst.push('<Override PartName="/xl/worksheets/sheet' + (i + 1) + '.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>');
  29. }
  30. }
  31. rst.push('<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>');
  32. rst.push('</Types>');
  33. return rst;
  34. }
  35. function writeRootRels(){
  36. let rst = [];
  37. rst.push(dftHeadXml + '\r\n');
  38. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  39. rst.push('<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>');
  40. rst.push('<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>');
  41. rst.push('<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>');
  42. rst.push('</Relationships>');
  43. return rst;
  44. }
  45. function writeApp(sheets, isSinglePage) {
  46. let rst = [];
  47. rst.push(dftHeadXml + '\r\n');
  48. rst.push('<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">');
  49. rst.push('<Application>Microsoft Excel</Application>');
  50. rst.push('<DocSecurity>0</DocSecurity>');
  51. rst.push('<ScaleCrop>false</ScaleCrop>');
  52. rst.push('<HeadingPairs>');
  53. rst.push('<vt:vector size="2" baseType="variant">');
  54. rst.push('<vt:variant><vt:lpstr>工作表</vt:lpstr></vt:variant>');
  55. if (isSinglePage) rst.push('<vt:variant><vt:i4>1</vt:i4></vt:variant>')
  56. else rst.push('<vt:variant><vt:i4>' + sheets.length + '</vt:i4></vt:variant>');
  57. rst.push('</vt:vector>');
  58. rst.push('</HeadingPairs>');
  59. rst.push('<TitlesOfParts>');
  60. if (isSinglePage) {
  61. rst.push('<vt:vector size="1" baseType="lpstr">');
  62. rst.push('<vt:lpstr>' + sheets[0].sheetName + '</vt:lpstr>');
  63. } else {
  64. rst.push('<vt:vector size="' + sheets.length + '" baseType="lpstr">');
  65. for (let i = 0; i < sheets.length; i++) {
  66. rst.push('<vt:lpstr>' + sheets[i].sheetName + '</vt:lpstr>');
  67. }
  68. }
  69. rst.push('</vt:vector>');
  70. rst.push('</TitlesOfParts>');
  71. rst.push('<Company>SmartCost</Company>');
  72. rst.push('<LinksUpToDate>false</LinksUpToDate>');
  73. rst.push('<SharedDoc>false</SharedDoc>');
  74. rst.push('<HyperlinksChanged>false</HyperlinksChanged>');
  75. rst.push('<AppVersion>12.0000</AppVersion>');
  76. rst.push('</Properties>');
  77. return rst;
  78. }
  79. function writeCore() {
  80. let rst = [];
  81. let p_fillZero = function(val){
  82. let rst = val;
  83. if (val < 10) {
  84. rst = '0' + val;
  85. }
  86. return rst;
  87. };
  88. rst.push(dftHeadXml + '\r\n');
  89. 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">');
  90. rst.push('<dc:creator>SmartCost</dc:creator>');
  91. rst.push('<cp:lastModifiedBy>SmartCost</cp:lastModifiedBy>');
  92. let dt = new Date(), dtStr = dt.getFullYear() + '-' + p_fillZero(dt.getMonth()+1) + '-' + p_fillZero(dt.getDate()) + 'T' +
  93. p_fillZero(dt.getHours()) + ':' + p_fillZero(dt.getMinutes()) + ':' + p_fillZero(dt.getSeconds()) + 'Z';
  94. rst.push('<dcterms:created xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:created>');
  95. rst.push('<dcterms:modified xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:modified>');
  96. //rst.push('');
  97. rst.push('</cp:coreProperties>');
  98. return rst;
  99. }
  100. function writeXlWorkBook(sheets, isSinglePage){
  101. let rst = [];
  102. rst.push(dftHeadXml + '\r\n');
  103. rst.push('<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  104. rst.push('<fileVersion appName="xl" lastEdited="4" lowestEdited="4" rupBuild="4505"/>');
  105. rst.push('<workbookPr defaultThemeVersion="124226"/>');
  106. rst.push('<bookViews><workbookView xWindow="360" yWindow="345" windowWidth="14655" windowHeight="4305"/></bookViews>');
  107. rst.push('<sheets>');
  108. if (isSinglePage) {
  109. rst.push('<sheet name="' + sheets[0].sheetName + '" sheetId="1" r:id="rId1"/>');
  110. } else {
  111. for (let i = 0; i < sheets.length; i++) {
  112. rst.push('<sheet name="' + sheets[i].sheetName + '" sheetId="' + (i + 1) + '" r:id="rId' + (i + 1) + '"/>');
  113. }
  114. }
  115. rst.push('</sheets>');
  116. rst.push('<calcPr calcId="124519"/>');
  117. rst.push('</workbook>');
  118. return rst;
  119. }
  120. function writeXlRels(sheets, isSinglePage){
  121. let rst = [], idx = 1;
  122. rst.push(dftHeadXml + '\r\n');
  123. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  124. if (isSinglePage) {
  125. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>');
  126. idx++;
  127. } else {
  128. for (let i = 0; i < sheets.length; i++) {
  129. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet' + (i + 1) + '.xml"/>');
  130. idx++;
  131. }
  132. }
  133. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>');
  134. idx++;
  135. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>');
  136. idx++;
  137. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/>');
  138. rst.push('</Relationships>');
  139. return rst;
  140. }
  141. function writeTheme(){
  142. let rst = fs.readFileSync(__dirname + '/excel_base_files/theme1.xml', 'utf8', 'r');
  143. return rst;
  144. }
  145. function writeStyles(stylesObj){
  146. let rst = [];
  147. rst.push(dftHeadXml + '\r\n');
  148. rst.push('<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">');
  149. //1. push fonts
  150. rst.push('<fonts count="' + stylesObj.fonts.length + '">');
  151. for (let i = 0; i < stylesObj.fonts.length; i++) {
  152. let font = stylesObj.fonts[i];
  153. rst.push('<font>');
  154. if (strUtil.convertStrToBoolean(font[JV.FONT_PROPS[3]])) {
  155. rst.push('<b/>');
  156. }
  157. if (strUtil.convertStrToBoolean(font[JV.FONT_PROPS[5]])) {
  158. rst.push('<u/>');
  159. }
  160. rst.push('<sz val="' + font.size + '"/>');
  161. rst.push('<color indexed="' + font.colorIdx + '"/>');
  162. rst.push('<name val="' + font[JV.FONT_PROPS[0]] + '"/>');
  163. rst.push('<charset val="' + font.charset + '"/>');
  164. rst.push('</font>');
  165. }
  166. rst.push('</fonts>');
  167. //2. push default fills
  168. rst.push('<fills count="2"><fill><patternFill patternType="none" /></fill><fill><patternFill patternType="gray125" /></fill></fills>');
  169. //3. push borders
  170. rst.push('<borders count="' + stylesObj.borders.length + '">');
  171. let private_setBorder = function(border, borderDirection) {
  172. if (parseInt(border[borderDirection][JV.PROP_LINE_WEIGHT]) === 0) {
  173. rst.push('<' + borderDirection.toLowerCase() + '/>');
  174. } else {
  175. let bW = 'thin';
  176. if (parseInt(border[borderDirection][JV.PROP_LINE_WEIGHT]) === 2) bW = 'medium';
  177. if (parseInt(border[borderDirection][JV.PROP_LINE_WEIGHT]) > 2) bW = 'thick';
  178. rst.push('<' + borderDirection.toLowerCase() + ' style="' + bW + '">' + '<color indexed="64"/>' + '</' + borderDirection.toLowerCase() + '>');
  179. }
  180. };
  181. for (let i = 0; i < stylesObj.borders.length; i++) {
  182. let border = stylesObj.borders[i];
  183. rst.push('<border>');
  184. private_setBorder(border, JV.PROP_LEFT);
  185. private_setBorder(border, JV.PROP_RIGHT);
  186. private_setBorder(border, JV.PROP_TOP);
  187. private_setBorder(border, JV.PROP_BOTTOM);
  188. rst.push('<diagonal />');
  189. rst.push('</border>');
  190. }
  191. rst.push('</borders>');
  192. //4. push cellStyleXfs
  193. rst.push('<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"><alignment vertical="center"/></xf></cellStyleXfs>');
  194. //5. push cellXfs
  195. rst.push('<cellXfs count="' + stylesObj.cellXfs.length + '">');
  196. for (let i = 0; i < stylesObj.cellXfs.length; i++) {
  197. let excelStyle = stylesObj.cellXfs[i];
  198. rst.push('<xf numFmtId="0" fontId="' + excelStyle.fontId + '" fillId="0" borderId="' + excelStyle.borderId + '" xfId="0">');
  199. //pageData[JV.NODE_FONT_COLLECTION] excelStyle.fontId
  200. let alignStr = "<alignment";
  201. let textRotation = 0;
  202. let newHorizontal = excelStyle[JV.CONTROL_PROPS[2]];
  203. let newVertical = excelStyle[JV.CONTROL_PROPS[3]];
  204. if (parseInt(excelStyle.fontAngle) !== 0) {
  205. let tmpH = newHorizontal, tmpV = newVertical;
  206. if (excelStyle.fontAngle > 0) {
  207. textRotation = 180;
  208. if (newHorizontal === "left") {
  209. tmpV = 'top';
  210. } else if (newHorizontal === "right") {
  211. tmpV = 'bottom';
  212. } else {
  213. tmpV = 'center';
  214. }
  215. if (newVertical === "top") {
  216. tmpH = 'right';
  217. } else if (newVertical === "bottom") {
  218. tmpH = 'left';
  219. } else {
  220. tmpH = 'center';
  221. }
  222. } else {
  223. textRotation = 90;
  224. if (newHorizontal === "left") {
  225. tmpV = 'bottom';
  226. } else if (newHorizontal === "right") {
  227. tmpV = 'top';
  228. } else {
  229. tmpV = 'center';
  230. }
  231. if (newVertical === "top") {
  232. tmpH = 'left';
  233. } else if (newVertical === "bottom") {
  234. tmpH = 'right';
  235. } else {
  236. tmpH = 'center';
  237. }
  238. }
  239. newHorizontal = tmpH;
  240. newVertical = tmpV;
  241. }
  242. alignStr += ' horizontal="' + newHorizontal + '" vertical="' + newVertical + '"';
  243. if (strUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[0]])) {
  244. alignStr += ' shrinkToFit="1"';
  245. }
  246. if (strUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[4]])) {
  247. alignStr += ' wrapText="1"';
  248. }
  249. if (textRotation !== 0) {
  250. alignStr += ' textRotation="' + textRotation + '"';
  251. }
  252. alignStr += '/>';
  253. rst.push(alignStr);
  254. rst.push('</xf>');
  255. }
  256. rst.push('</cellXfs>');
  257. //6. others (xfl style / dxfs / tableStyles)
  258. rst.push('<cellStyles count="1"><cellStyle name="常规" xfId="0" builtinId="0"/></cellStyles>');
  259. rst.push('<dxfs count="0"/>');
  260. rst.push('<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleLight16"/>');
  261. rst.push('</styleSheet>');
  262. return rst;
  263. }
  264. function writeSharedString(sharedStrList){
  265. let rst = [];
  266. if (sharedStrList && sharedStrList.length > 0) {
  267. rst.push(dftHeadXml + '\r\n');
  268. rst.push('<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="' + sharedStrList.length + '" uniqueCount="' + sharedStrList.length + '">');
  269. for (let i = 0; i < sharedStrList.length; i++) {
  270. //rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
  271. if (typeof sharedStrList[i] === 'string') {
  272. if (sharedStrList[i].indexOf('|') >= 0) {
  273. //rst.push('<si><t>' + sharedStrList[i].split('|').join('\r\n') + '</t></si>');
  274. rst.push('<si><t>' + sharedStrList[i].split('|').join('\n') + '</t></si>');
  275. } else {
  276. rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
  277. }
  278. // rst.push('<si><t>' + sharedStrList[i].replace('|','\r\n') + '</t></si>');
  279. } else {
  280. rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
  281. }
  282. }
  283. rst.push('</sst>');
  284. }
  285. return rst;
  286. }
  287. function writeSheets(pageData, paperSize, sharedStrList, stylesObj, isSinglePage){
  288. let rst = [];
  289. let private_pushDftFont = function(){
  290. let font = {};
  291. if (!(stylesObj.fonts)) {
  292. stylesObj.fonts = [];
  293. }
  294. font[JV.FONT_PROPS[0]] = "宋体"; //font name
  295. font.size = 12;
  296. font.charset = 134;
  297. font.colorIdx = "8";
  298. stylesObj.fonts.push(font);
  299. };
  300. let private_buildFirstDftStyle = function () {
  301. stylesObj.cellXfs = [];
  302. stylesObj.borders = [];
  303. let fontId = 0;
  304. let borderId = 0;
  305. let border = {};
  306. border[JV.PROP_LEFT] = {};
  307. border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = 0;
  308. border[JV.PROP_RIGHT] = {};
  309. border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = 0;
  310. border[JV.PROP_TOP] = {};
  311. border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = 0;
  312. border[JV.PROP_BOTTOM] = {};
  313. border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = 0;
  314. stylesObj.borders.push(border);
  315. let cellControl = pageData[JV.NODE_CONTROL_COLLECTION].Default;
  316. let sheetControl = {};
  317. sheetControl.fontId = fontId;
  318. sheetControl.borderId = borderId;
  319. sheetControl.fontAngle = 0;
  320. for (let i = 0; i < JV.CONTROL_PROPS.length; i++) {
  321. sheetControl[JV.CONTROL_PROPS[i]] = cellControl[JV.CONTROL_PROPS[i]];
  322. }
  323. stylesObj.cellXfs.push(sheetControl);
  324. }
  325. private_pushDftFont();
  326. private_buildFirstDftStyle();
  327. if (isSinglePage) {
  328. rst.push(writeSheet(pageData, null, paperSize, sharedStrList, stylesObj));
  329. } else {
  330. for (let i = 0; i < pageData.items.length; i++) {
  331. rst.push(writeSheet(pageData, pageData.items[i], paperSize, sharedStrList, stylesObj));
  332. }
  333. }
  334. return rst;
  335. }
  336. function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj){
  337. let rst = [], xPos = [], yPos = [], yMultiPos = [], headerStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  338. let cacheBorderCell = {};
  339. let currentPageMergePos = null; //在 JV.PAGING_OPTION_INFINITY 场合应用
  340. let private_pre_analyze_pos = function(){
  341. let cell, pos;
  342. let self_analyze_sheet_pos = function (theShtData, theXPos, theYPos) {
  343. // theShtData.cells.sort(function(cell1, cell2) {
  344. // let rst = 0;
  345. // if (cell1[JV.PROP_AREA][JV.PROP_TOP] > cell2[JV.PROP_AREA][JV.PROP_TOP]) {
  346. // rst = 1;
  347. // } else if (cell1[JV.PROP_AREA][JV.PROP_TOP] < cell2[JV.PROP_AREA][JV.PROP_TOP]) {
  348. // rst = -1;
  349. // } else {
  350. // if (cell1[JV.PROP_AREA][JV.PROP_LEFT] > cell2[JV.PROP_AREA][JV.PROP_LEFT]) {
  351. // rst = 1;
  352. // } else if (cell1[JV.PROP_AREA][JV.PROP_LEFT] < cell2[JV.PROP_AREA][JV.PROP_LEFT]) {
  353. // rst = -1;
  354. // }
  355. // }
  356. // return rst;
  357. // });
  358. for (let i = 0; i < theShtData.cells.length; i++) {
  359. cell = theShtData.cells[i];
  360. pos = cell[JV.PROP_AREA][JV.PROP_LEFT];
  361. if (theXPos.indexOf(pos) < 0) theXPos.push(pos);
  362. pos = cell[JV.PROP_AREA][JV.PROP_RIGHT];
  363. if (theXPos.indexOf(pos) < 0) theXPos.push(pos);
  364. pos = cell[JV.PROP_AREA][JV.PROP_TOP];
  365. if (theYPos.indexOf(pos) < 0) theYPos.push(pos);
  366. pos = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  367. if (theYPos.indexOf(pos) < 0) theYPos.push(pos);
  368. }
  369. };
  370. xPos.push(0);
  371. if (sheetData) {
  372. //current sheet data
  373. yPos.push(0);
  374. self_analyze_sheet_pos(sheetData, xPos, yPos);
  375. xPos.sort(private_array_sort);
  376. yPos.sort(private_array_sort);
  377. } else {
  378. //total data in one sheet
  379. for (let shtItemData of pageData.items) {
  380. let tmpPos = [];
  381. tmpPos.push(0);
  382. self_analyze_sheet_pos(shtItemData, xPos, tmpPos);
  383. tmpPos.sort(private_array_sort);
  384. yMultiPos.push(tmpPos);
  385. }
  386. xPos.sort(private_array_sort);
  387. yPos = yMultiPos[0];
  388. }
  389. };
  390. let private_array_sort = function(i1, i2){
  391. let rst = 0;
  392. if (i1 > i2) {rst = 1} else
  393. if (i1 < i2) rst = -1;
  394. return rst;
  395. };
  396. let private_getCellIdxStr = function(idx){
  397. let rst = 'A';
  398. if (idx < 26) {
  399. rst = headerStr[idx];
  400. } else if (idx < 26*26+26) {
  401. let ti = Math.floor(idx / 26), tj = idx % 26;
  402. rst = headerStr[ti - 1] + headerStr[tj];
  403. } else if (idx < 26*26*26+26) {
  404. let ti = Math.floor(idx / (26*26)), tj = Math.floor((idx - ti * 26*26) / 26), tk = idx % 26;
  405. rst = headerStr[ti - 1] + headerStr[tj-1] + headerStr[tk];
  406. }
  407. return rst;
  408. };
  409. let private_getSharedStrIdx = function(val) {
  410. let rst = sharedStrList.indexOf(val);
  411. if (rst < 0) {
  412. sharedStrList.push(val);
  413. rst = sharedStrList.length - 1;
  414. }
  415. return rst;
  416. };
  417. let private_getFontId = function(cell) {
  418. let rst = 0, hasFont = false;
  419. if (!(stylesObj.fonts)) {
  420. stylesObj.fonts = [];
  421. //for (let i = 0; i < sheetData.font_collection)
  422. }
  423. let sheetFont = null;
  424. if (typeof cell[JV.PROP_FONT] === "string") {
  425. sheetFont = pageData[JV.NODE_FONT_COLLECTION][cell[JV.PROP_FONT]];
  426. } else {
  427. sheetFont = cell[JV.PROP_FONT];
  428. }
  429. for (let i = 0; i < stylesObj.fonts.length; i++) {
  430. let font = stylesObj.fonts[i];
  431. if (sheetFont) {
  432. if (font[JV.FONT_PROPS[0]] === sheetFont[JV.FONT_PROPS[0]] && font.size === Math.round(sheetFont[JV.FONT_PROPS[1]] * 3 / 4)
  433. && font[JV.FONT_PROPS[3]] === sheetFont[JV.FONT_PROPS[3]] && font[JV.FONT_PROPS[5]] === sheetFont[JV.FONT_PROPS[5]]) {
  434. hasFont = true;
  435. rst = i;
  436. break;
  437. }
  438. } else {
  439. break;
  440. }
  441. }
  442. if (!hasFont) {
  443. let font = {};
  444. font[JV.FONT_PROPS[0]] = sheetFont[JV.FONT_PROPS[0]]; //font name
  445. font.size = Math.round(sheetFont[JV.FONT_PROPS[1]] * 3 / 4);
  446. font.charset = 134;
  447. font.colorIdx = "8";
  448. font[JV.FONT_PROPS[3]] = sheetFont[JV.FONT_PROPS[3]]; //font bold
  449. font[JV.FONT_PROPS[5]] = sheetFont[JV.FONT_PROPS[5]]; //font underline
  450. stylesObj.fonts.push(font);
  451. rst = stylesObj.fonts.length - 1;
  452. }
  453. return rst;
  454. };
  455. let private_checkBorder = function(cell, border, sheetBorder) {
  456. let rst = true, borderLineWidths = [], sheetBorderLineWidths = [];
  457. borderLineWidths.push(border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]);
  458. borderLineWidths.push(border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]);
  459. borderLineWidths.push(border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]);
  460. borderLineWidths.push(border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]);
  461. if (sheetBorder[JV.PROP_LEFT] && sheetBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]) {
  462. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_LEFT, true));
  463. } else {
  464. sheetBorderLineWidths.push(0);
  465. }
  466. if (sheetBorder[JV.PROP_RIGHT] && sheetBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]) {
  467. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_RIGHT, true));
  468. } else {
  469. sheetBorderLineWidths.push(0);
  470. }
  471. if (sheetBorder[JV.PROP_TOP] && sheetBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]) {
  472. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_TOP, false));
  473. } else {
  474. sheetBorderLineWidths.push(0);
  475. }
  476. if (sheetBorder[JV.PROP_BOTTOM] && sheetBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]) {
  477. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_BOTTOM, false));
  478. } else {
  479. sheetBorderLineWidths.push(0);
  480. }
  481. for (let i = 0; i < 4; i++) {
  482. if (borderLineWidths[i] != sheetBorderLineWidths[i]) {
  483. rst = false;
  484. break;
  485. }
  486. }
  487. return rst;
  488. };
  489. let private_chkAndGetMergeLine = function(cell, sheetBorder, borderStr, needFurtherChk) {
  490. let rst = 0,
  491. mergeBorder = (sheetData)?sheetData[JV.PROP_PAGE_MERGE_BORDER]:pageData[JV.BAND_PROP_MERGE_BAND],
  492. mergeBand = pageData[JV.BAND_PROP_MERGE_BAND]
  493. ;
  494. if (sheetBorder[borderStr] && sheetBorder[borderStr][JV.PROP_LINE_WEIGHT] !== undefined) {
  495. rst = sheetBorder[borderStr][JV.PROP_LINE_WEIGHT];
  496. }
  497. if (currentPageMergePos) {
  498. let side = currentPageMergePos[borderStr];
  499. if (side.indexOf(cell[JV.PROP_AREA][borderStr]) >= 0) {
  500. if (needFurtherChk) {
  501. let topSide = currentPageMergePos[JV.PROP_TOP];
  502. let bottomSide = currentPageMergePos[JV.PROP_BOTTOM];
  503. for (let i = 0; i < topSide.length; i++) {
  504. if (cell[JV.PROP_AREA][JV.PROP_TOP] >= topSide[i]) {
  505. if (cell[JV.PROP_AREA][JV.PROP_BOTTOM] <= bottomSide[i]) {
  506. let destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
  507. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  508. break;
  509. }
  510. } else {
  511. break;
  512. }
  513. }
  514. } else {
  515. let destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
  516. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  517. }
  518. }
  519. } else {
  520. if (cell[JV.PROP_AREA][borderStr] === mergeBorder[borderStr]) {
  521. let destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
  522. if (needFurtherChk) {
  523. if (cell[JV.PROP_AREA][JV.PROP_TOP] >= mergeBorder[JV.PROP_TOP] &&
  524. cell[JV.PROP_AREA][JV.PROP_BOTTOM] <= mergeBorder[JV.PROP_BOTTOM]) {
  525. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  526. }
  527. } else {
  528. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  529. }
  530. }
  531. }
  532. return parseInt(rst);
  533. };
  534. let private_getIniBorder = function() {
  535. let rst = {};
  536. rst[JV.PROP_LEFT] = {};
  537. rst[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = 0;
  538. rst[JV.PROP_RIGHT] = {};
  539. rst[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = 0;
  540. rst[JV.PROP_TOP] = {};
  541. rst[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = 0;
  542. rst[JV.PROP_BOTTOM] = {};
  543. rst[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = 0;
  544. return rst;
  545. };
  546. let private_getBorderId = function(cell) {
  547. let rst = 0, hasBorder = false;
  548. if (!(stylesObj.borders)) {
  549. stylesObj.borders = [];
  550. }
  551. let sheetBorder = pageData[JV.NODE_STYLE_COLLECTION][cell.style];
  552. let mergedBorder = private_getIniBorder();
  553. mergedBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_LEFT, true);
  554. mergedBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_RIGHT, true);
  555. mergedBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_TOP, false);
  556. mergedBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_BOTTOM, false);
  557. for (let i = 0; i < stylesObj.borders.length; i++) {
  558. let border = stylesObj.borders[i];
  559. if (private_checkBorder(cell, border, mergedBorder)) {
  560. hasBorder = true;
  561. rst = i;
  562. break;
  563. }
  564. }
  565. if (!hasBorder) {
  566. stylesObj.borders.push(mergedBorder);
  567. rst = stylesObj.borders.length - 1;
  568. }
  569. return rst;
  570. };
  571. let private_checkControl = function(cellControl, sheetControl) {
  572. let rst = true;
  573. for (let i = 0; i < JV.CONTROL_PROPS.length; i++) {
  574. if (cellControl[JV.CONTROL_PROPS[i]] != sheetControl[JV.CONTROL_PROPS[i]]) {
  575. rst = false;
  576. break;
  577. }
  578. }
  579. return rst;
  580. };
  581. let private_getStyleId = function(cell) {
  582. let rst = 1, hasStyle = false;
  583. if (!(stylesObj.cellXfs)) stylesObj.cellXfs = [];
  584. let fontId = private_getFontId(cell);
  585. let fontAngle = 0;
  586. if (typeof cell[JV.PROP_FONT] === "string") {
  587. fontAngle = parseInt(pageData[JV.NODE_FONT_COLLECTION][cell[JV.PROP_FONT]].FontAngle);
  588. } else {
  589. fontAngle = parseInt(cell[JV.PROP_FONT].FontAngle);
  590. }
  591. let borderId = private_getBorderId(cell);
  592. let cellControl = null;
  593. if (typeof cell[JV.PROP_CONTROL] === "string") {
  594. cellControl = pageData[JV.NODE_CONTROL_COLLECTION][cell[JV.PROP_CONTROL]];
  595. } else {
  596. cellControl = cell[JV.PROP_CONTROL];
  597. }
  598. for (let i = 0; i < stylesObj.cellXfs.length; i++) {
  599. let sheetControl = stylesObj.cellXfs[i];
  600. if (sheetControl.fontId === fontId && sheetControl.borderId === borderId) {
  601. if (private_checkControl(cellControl, sheetControl)) {
  602. rst = i;
  603. hasStyle = true;
  604. break;
  605. }
  606. }
  607. }
  608. if (!hasStyle) {
  609. let sheetControl = {};
  610. sheetControl.fontId = fontId;
  611. sheetControl.borderId = borderId;
  612. sheetControl.fontAngle = fontAngle;
  613. for (let i = 0; i < JV.CONTROL_PROPS.length; i++) {
  614. sheetControl[JV.CONTROL_PROPS[i]] = cellControl[JV.CONTROL_PROPS[i]];
  615. }
  616. stylesObj.cellXfs.push(sheetControl);
  617. rst = stylesObj.cellXfs.length - 1;
  618. }
  619. return rst;
  620. };
  621. let private_setCols = function(){
  622. //remark: 1 excel unit width = 2.117 mm
  623. rst.push('<cols>');
  624. let w = 0;
  625. /*
  626. for (let i = 1; i < xPos.length - 1; i++) {
  627. w = 1.0 * (xPos[i + 1] - xPos[i]) / DPI * 25.4 / 2.117;
  628. w = Math.round(w * 1000) / 1000;
  629. rst.push('<col min="' + i +'" max="' + i +'" width="' + w + '" customWidth="1"/>');
  630. }
  631. /*/
  632. for (let i = 1; i < xPos.length - 2; i++) {
  633. w = 1.0 * (xPos[i + 1] - xPos[i]) / DPI * 25.4 / 2.117;
  634. w = Math.round(w * 1000) / 1000;
  635. rst.push('<col min="' + i +'" max="' + i +'" width="' + w + '" customWidth="1"/>');
  636. }
  637. rst.push('<col min="' + (xPos.length - 1) +'" max="' + (xPos.length - 1) +'" width="' + 10 + '" customWidth="1"/>');
  638. //*/
  639. rst.push('</cols>');
  640. };
  641. let private_setMergedCells = function() {
  642. let cell, idxR, idxL, idxT, idxB, cnt = 0;
  643. rst.push('<mergeCells count="?">');
  644. let startIdx = rst.length - 1;
  645. let self_setMergedCells = function (theData, theYPos, offsetY) {
  646. for (let i = 0; i < theData.cells.length; i++) {
  647. cell = theData.cells[i];
  648. idxR = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_RIGHT]);
  649. idxL = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_LEFT]);
  650. idxB = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_BOTTOM]);
  651. idxT = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_TOP]);
  652. if (idxR - idxL > 1 || idxB - idxT > 1) {
  653. rst.push('<mergeCell ref="' + private_getCellIdxStr(idxL - 1) + (idxT + offsetY) + ':' + private_getCellIdxStr(idxR - 2) + (idxB - 1 + offsetY) + '"/>');
  654. cnt++;
  655. }
  656. }
  657. }
  658. if (sheetData) {
  659. self_setMergedCells(sheetData, yPos, 0);
  660. } else {
  661. let osY = 0;
  662. for (let i = 0; i < pageData.items.length; i++) {
  663. let shtItemData = pageData.items[i];
  664. let tmpPos = yMultiPos[i];
  665. self_setMergedCells(shtItemData, tmpPos, osY);
  666. osY += tmpPos.length - 2;
  667. }
  668. }
  669. rst[startIdx] = '<mergeCells count="' + cnt + '">';
  670. rst.push('</mergeCells>');
  671. };
  672. let private_chkIfNeedCacheCell = function(cell){
  673. let rst = false;
  674. if (cell[JV.PROP_AREA][JV.PROP_LEFT] === pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_LEFT] ||
  675. cell[JV.PROP_AREA][JV.PROP_RIGHT] === pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_RIGHT] ||
  676. cell[JV.PROP_AREA][JV.PROP_TOP] === pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_TOP] ||
  677. cell[JV.PROP_AREA][JV.PROP_BOTTOM] === pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_BOTTOM]){
  678. if (cell[JV.PROP_AREA][JV.PROP_LEFT] >= pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_LEFT] &&
  679. cell[JV.PROP_AREA][JV.PROP_RIGHT] <= pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_RIGHT] &&
  680. cell[JV.PROP_AREA][JV.PROP_TOP] >= pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_TOP] &&
  681. cell[JV.PROP_AREA][JV.PROP_BOTTOM] <= pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_BOTTOM]) {
  682. rst = true;
  683. }
  684. }
  685. return rst;
  686. };
  687. let private_cacheMergeBandBorderIdxs = function() {
  688. let cell, idxR, idxL, idxT, idxB;
  689. let self_cachMergeIdxs = function (theData, theYPos, offsetY) {
  690. for (let i = 0; i < theData.cells.length; i++) {
  691. cell = theData.cells[i];
  692. idxR = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_RIGHT]);
  693. idxL = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_LEFT]);
  694. idxB = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_BOTTOM]);
  695. idxT = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_TOP]);
  696. if (idxR - idxL > 1 || idxB - idxT > 1) {
  697. if (private_chkIfNeedCacheCell(cell)) {
  698. for (let xi = idxL; xi < idxR; xi++) {
  699. for (let yj = idxT; yj < idxB; yj++) {
  700. cacheBorderCell[private_getCellIdxStr(xi - 1) + (yj + offsetY)] = cell;
  701. }
  702. }
  703. }
  704. }
  705. }
  706. }
  707. if (sheetData) {
  708. self_cachMergeIdxs(sheetData, yPos, 0);
  709. } else {
  710. let osY = 0;
  711. for (let i = 0; i < pageData.items.length; i++) {
  712. let shtItemData = pageData.items[i];
  713. let tmpPos = yMultiPos[i];
  714. self_cachMergeIdxs(shtItemData, tmpPos, osY);
  715. osY += tmpPos.length - 2;
  716. }
  717. }
  718. };
  719. let private_setSheetData = function(){
  720. //remark: 1 excel unit height = 0.3612 mm
  721. rst.push('<sheetData>');
  722. let spanX = xPos.length - 2, cellIdx = 0, h = 0
  723. ;
  724. let self_setDataEx = function (theShtData, theYPos, offsetY) {
  725. let rows = [];
  726. //1. build full set of blank rows/cells
  727. for (let i = 1; i < theYPos.length - 1; i++) {
  728. let rowObj = {};
  729. h = (theYPos[i+1] - theYPos[i]) / DPI * 25.4 / 0.3612;
  730. h = Math.round(h * 1000) / 1000;
  731. rowObj.height = h;
  732. rowObj.r = i + offsetY;
  733. rowObj.items = [];
  734. rows.push(rowObj);
  735. for (let j = 1; j < xPos.length - 1; j++) {
  736. let colIdxStr = private_getCellIdxStr(j - 1);
  737. let cellObj = {};
  738. cellObj.r = colIdxStr + (i + offsetY);
  739. cellObj.s = 0;
  740. cellObj.isBlank = true;
  741. rows[i - 1].items.push(cellObj);
  742. }
  743. }
  744. //2. then fill up the cell style-ids and values
  745. let rowIdx1 = 0, colIdx1 = 0, rowIdx2 = 0, colIdx2 = 0, colIdxStr = '';
  746. for (let cIdx = 0; cIdx < theShtData.cells.length; cIdx++) {
  747. let styleIdx = private_getStyleId(theShtData.cells[cIdx]);
  748. //colIdxStr = private_getCellIdxStr(j - 1);
  749. rowIdx1 = theYPos.indexOf(theShtData.cells[cIdx][JV.PROP_AREA][JV.PROP_TOP]);
  750. colIdx1 = xPos.indexOf(theShtData.cells[cIdx][JV.PROP_AREA][JV.PROP_LEFT]);
  751. let cellObj = rows[rowIdx1 - 1].items[colIdx1 - 1];
  752. cellObj.s = styleIdx;
  753. cellObj.isBlank = false;
  754. if (!(strUtil.isEmptyString(theShtData.cells[cIdx][JV.PROP_VALUE]))) {
  755. let valIdx = private_getSharedStrIdx(theShtData.cells[cIdx][JV.PROP_VALUE]);
  756. cellObj.v = valIdx;
  757. }
  758. rowIdx2 = theYPos.indexOf(theShtData.cells[cIdx][JV.PROP_AREA][JV.PROP_BOTTOM]);
  759. colIdx2 = xPos.indexOf(theShtData.cells[cIdx][JV.PROP_AREA][JV.PROP_RIGHT]);
  760. if ((rowIdx2 - rowIdx1 > 1) || (colIdx2 - colIdx1 > 1)) {
  761. for (let i = 0; i < rowIdx2 - rowIdx1; i++) {
  762. for (let j = 0; j < colIdx2 - colIdx1; j++) {
  763. if (i === 0 && j === 0) continue;
  764. cellObj = rows[rowIdx1 - 1 + i].items[colIdx1 - 1 + j];
  765. cellObj.s = styleIdx;
  766. cellObj.isBlank = true;
  767. }
  768. }
  769. }
  770. }
  771. //3. then fill up rst
  772. for (let i = 0; i < rows.length; i++) {
  773. rst.push('<row r="' + (i + 1 + offsetY) + '" spans="1:' + spanX + '" ht="' + rows[i].height + '" customHeight="1">');
  774. for (let j = 0; j < rows[i].items.length; j++) {
  775. let cellObj = rows[i].items[j];
  776. if (cellObj.v === undefined) {
  777. rst.push('<c r="' + cellObj.r + '" s="' + cellObj.s + '"/>');
  778. } else {
  779. rst.push('<c r="' + cellObj.r + '" s="' + cellObj.s + '" t="s">');
  780. rst.push('<v>' + cellObj.v + '</v>');
  781. rst.push('</c>');
  782. }
  783. }
  784. rst.push('</row>');
  785. }
  786. //4. maybe need to dispose the memory
  787. //...
  788. };
  789. //private_cacheMergeBandBorderIdxs();
  790. if (sheetData) {
  791. //current sheet data
  792. currentPageMergePos = sheetData[JV.PAGE_SPECIAL_MERGE_POS];
  793. self_setDataEx(sheetData, yPos, 0);
  794. } else {
  795. //total data in one sheet
  796. let cnt = 0;
  797. for (let i = 0; i < pageData.items.length; i++) {
  798. let shtItemData = pageData.items[i];
  799. currentPageMergePos = shtItemData[JV.PAGE_SPECIAL_MERGE_POS];
  800. let tmpPos = yMultiPos[i];
  801. cellIdx = 0;
  802. self_setDataEx(shtItemData, tmpPos, cnt);
  803. cnt += tmpPos.length - 2;
  804. }
  805. }
  806. rst.push('</sheetData>');
  807. };
  808. private_pre_analyze_pos();
  809. rst.push(dftHeadXml + '\r\n');
  810. rst.push('<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  811. let colStr = private_getCellIdxStr(xPos.length - 3);
  812. rst.push('<dimension ref="A1:' + colStr + '' + yPos.length + '"/>');
  813. rst.push('<sheetViews><sheetView tabSelected="1" workbookViewId="0">');
  814. //rst.push('<selection sqref="A1:' + colStr + '1"/>');
  815. rst.push('<selection sqref="A1:A1"/>');
  816. rst.push('</sheetView></sheetViews>');
  817. rst.push('<sheetFormatPr defaultRowHeight="13.5"/>');
  818. private_setCols();
  819. private_setSheetData();
  820. private_setMergedCells();
  821. rst.push('<phoneticPr fontId="1" type="noConversion"/>');
  822. rst.push('<pageMargins left="0.315" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
  823. let paperSizeIdx = JV.PAGES_SIZE_STR.indexOf(paperSize);
  824. let pStr = '';
  825. if (paperSizeIdx >= 0) {
  826. pStr = 'paperSize="' + JV.PAGES_SIZE_IDX[paperSizeIdx] + '"';
  827. }
  828. let orientationStr = (pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][0] > pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1])?'landscape':'portrait';
  829. if (currentPageMergePos) {
  830. orientationStr = (currentPageMergePos[JV.NODE_PAGE_SIZE][0] > currentPageMergePos[JV.NODE_PAGE_SIZE][1])?'landscape':'portrait';
  831. }
  832. rst.push('<pageSetup ' + pStr + ' fitToWidth="0" fitToHeight="0" orientation="' + orientationStr + '" />');
  833. rst.push('<headerFooter alignWithMargins="0"/>');
  834. rst.push('</worksheet>');
  835. return rst;
  836. }
  837. function mergeProperties(orgObj, newObj) {
  838. let orgPropArr = [], newPropArr = [];
  839. for (let p in orgObj) {
  840. orgPropArr.push(p);
  841. }
  842. for (let p in newObj) {
  843. newPropArr.push(p);
  844. }
  845. for (let i = 0; i < newPropArr.length; i++) {
  846. if (orgPropArr.indexOf(newPropArr[i]) < 0) {
  847. orgObj[newPropArr[i]] = newObj[newPropArr[i]];
  848. }
  849. }
  850. }
  851. module.exports = {
  852. exportExcel: function (pageData, paperSize, fName, options, custSheetNames, callback) {
  853. let rptOptions = ({singlePage: false, fileName: 'report'});
  854. if (options === 'true') {
  855. rptOptions.singlePage = true;
  856. }
  857. let isSinglePage = rptOptions.singlePage;
  858. let sheets = [];
  859. if (isSinglePage) {
  860. sheets.push({sheetName: '全部页'});
  861. } else {
  862. if (custSheetNames && custSheetNames.length === pageData.items.length) {
  863. for (let i = 0; i < pageData.items.length; i++) {
  864. sheets.push({sheetName: custSheetNames[i]});
  865. }
  866. } else {
  867. for (let i = 0; i < pageData.items.length; i++) {
  868. sheets.push({sheetName: '第' + (i + 1) + '页'});
  869. }
  870. }
  871. }
  872. //1.
  873. let file = '[Content_Types].xml';
  874. let data = writeContentTypes(sheets, isSinglePage);
  875. let zip = new JSZip();
  876. zip.file(file, data.join(''), {compression: 'DEFLATE'});
  877. //2.
  878. let zip_rels = zip.folder('_rels');
  879. file = '.rels';
  880. data = writeRootRels();
  881. zip_rels.file(file, data.join(''), {compression: 'DEFLATE'});
  882. //3.
  883. let zip_docProps = zip.folder('docProps');
  884. file = 'app.xml';
  885. data = writeApp(sheets, isSinglePage);
  886. zip_docProps.file(file, data.join(''), {compression: 'DEFLATE'});
  887. file = 'core.xml';
  888. data = writeCore();
  889. zip_docProps.file(file, data.join(''), {compression: 'DEFLATE'});
  890. //4.
  891. let zip_xl = zip.folder('xl');
  892. file = 'workbook.xml';
  893. data = writeXlWorkBook(sheets, isSinglePage);
  894. zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
  895. let zip_rels2 = zip_xl.folder('_rels');
  896. file = 'workbook.xml.rels';
  897. data = writeXlRels(sheets, isSinglePage);
  898. zip_rels2.file(file, data.join(''), {compression: 'DEFLATE'});
  899. //5.
  900. let zip_theme = zip_xl.folder('theme');
  901. file = 'theme1.xml';
  902. data = writeTheme();
  903. zip_theme.file(file, data, {compression: 'DEFLATE'});
  904. //6.
  905. let zip_worksheets = zip_xl.folder('worksheets');
  906. let sharedStrList = [], stylesObj = {};
  907. data = writeSheets(pageData, paperSize, sharedStrList, stylesObj, isSinglePage);
  908. if (isSinglePage) {
  909. for (let i = 0; i < 1; i++) {
  910. file = 'sheet' + (i + 1) + '.xml';
  911. zip_worksheets.file(file, data[i].join(''), {compression: 'DEFLATE'});
  912. }
  913. } else {
  914. for (let i = 0; i < data.length; i++) {
  915. file = 'sheet' + (i + 1) + '.xml';
  916. zip_worksheets.file(file, data[i].join(''), {compression: 'DEFLATE'});
  917. }
  918. }
  919. file = 'sharedStrings.xml';
  920. data = writeSharedString(sharedStrList);
  921. zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
  922. file = 'styles.xml';
  923. data = writeStyles(stylesObj);
  924. zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
  925. if (fName) {
  926. let newName = '' + (new Date()).valueOf();
  927. zip.generateNodeStream({type:'nodebuffer',streamFiles:true})
  928. .pipe(fs.createWriteStream(__dirname.slice(0, __dirname.length - 21) + '/tmp/' + newName + '.xlsx'))
  929. .on('finish', function () {
  930. // JSZip generates a readable stream with a "end" event,
  931. // but is piped here in a writable stream which emits a "finish" event.
  932. console.log(newName + ".xlsx was written.");
  933. if (callback) callback(newName);
  934. }
  935. );
  936. } else {
  937. //return zip.generateNodeStream({type:'nodebuffer',streamFiles:true});
  938. return zip;
  939. }
  940. },
  941. exportExcelInOneBook: function (pageDataArray, paperSize, fName, callback) {
  942. let me = this, newPageData = {};
  943. //1. 重新编排一下数据,把一份报表的pageData合并到一起作为一个Sheet输出(需要重新调整数据纵向坐标),多份报表数据就形成多个Sheet
  944. // -- 简单来说,就是重新包装数据
  945. try {
  946. // 1.1 newPageData外围属性
  947. let newContrl = {}, newFont = {}, newStyle = {};
  948. for (let i = 0; i < pageDataArray.length; i++) {
  949. mergeProperties(newContrl, pageDataArray[i][JV.NODE_CONTROL_COLLECTION]);
  950. mergeProperties(newFont, pageDataArray[i][JV.NODE_FONT_COLLECTION]);
  951. mergeProperties(newStyle, pageDataArray[i][JV.NODE_STYLE_COLLECTION]);
  952. }
  953. newPageData[JV.NODE_CONTROL_COLLECTION] = newContrl;
  954. newPageData[JV.NODE_FONT_COLLECTION] = newFont;
  955. newPageData[JV.NODE_STYLE_COLLECTION] = newStyle;
  956. newPageData[JV.NODE_PAGE_INFO] = pageDataArray[0][JV.NODE_PAGE_INFO];
  957. newPageData[JV.BAND_PROP_MERGE_BAND] = pageDataArray[0][JV.BAND_PROP_MERGE_BAND];
  958. // 1.2 重新设置pageDataArray的各个cell的Top/Bottom坐标
  959. let sheetNames = [], newPagePos = [];
  960. for (let i = 0; i < pageDataArray.length; i++) {
  961. let offsetY = 0;
  962. let mergeBand = {};
  963. mergeBand[JV.PROP_LEFT] = [];
  964. mergeBand[JV.PROP_RIGHT] = [];
  965. mergeBand[JV.PROP_TOP] = [];
  966. mergeBand[JV.PROP_BOTTOM] = [];
  967. newPagePos.push(mergeBand);
  968. mergeBand[JV.PROP_LEFT].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_LEFT]);
  969. mergeBand[JV.PROP_RIGHT].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_RIGHT]);
  970. sheetNames.push(pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_MAIN_INFO_RPT_NAME]);
  971. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  972. let maxY = 0, minY = 100000;
  973. if (pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS]) {
  974. let pos = pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_TOP][0] + offsetY;
  975. mergeBand[JV.PROP_TOP].push(pos);
  976. pos = pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_BOTTOM][0] + offsetY;
  977. mergeBand[JV.PROP_BOTTOM].push(pos);
  978. } else {
  979. mergeBand[JV.PROP_TOP].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_TOP] + offsetY);
  980. mergeBand[JV.PROP_BOTTOM].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_BOTTOM] + offsetY);
  981. }
  982. for (let k = 0; k < pageDataArray[i].items[j].cells.length; k++) {
  983. if (maxY < pageDataArray[i].items[j].cells[k][JV.PROP_AREA][JV.PROP_BOTTOM]) {
  984. maxY = pageDataArray[i].items[j].cells[k][JV.PROP_AREA][JV.PROP_BOTTOM];
  985. }
  986. if (minY > pageDataArray[i].items[j].cells[k][JV.PROP_AREA][JV.PROP_TOP]) {
  987. minY = pageDataArray[i].items[j].cells[k][JV.PROP_AREA][JV.PROP_TOP];
  988. }
  989. pageDataArray[i].items[j].cells[k][JV.PROP_AREA][JV.PROP_BOTTOM] += offsetY;
  990. pageDataArray[i].items[j].cells[k][JV.PROP_AREA][JV.PROP_TOP] += offsetY;
  991. }
  992. offsetY += (maxY - minY);
  993. }
  994. }
  995. //2. newPageData的items属性
  996. newPageData.items = [];
  997. for (let i = 0; i < pageDataArray.length; i++) {
  998. let pageItem = {};
  999. pageItem[JV.PROP_PAGE_SEQ] = i + 1;
  1000. pageItem[JV.PROP_CELLS] = [];
  1001. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1002. for (let k = 0; k < pageDataArray[i].items[j].cells.length; k++) {
  1003. pageItem[JV.PROP_CELLS].push(pageDataArray[i].items[j].cells[k]);
  1004. }
  1005. }
  1006. newPagePos[i][JV.NODE_PAGE_SIZE] = pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE];
  1007. pageItem[JV.PAGE_SPECIAL_MERGE_POS] = newPagePos[i];
  1008. newPageData.items.push(pageItem);
  1009. }
  1010. //3. everything is ok, then call me
  1011. me.exportExcel(newPageData, paperSize, fName, 'false', sheetNames, callback);
  1012. // fsUtil.writeObjToFile(newPageData, 'D:/GitHome/ConstructionOperation/tmp/combinedHeader.js');
  1013. } catch (e) {
  1014. console.log(e);
  1015. }
  1016. }
  1017. }