rpt_excel_util.js 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. 'use strict';
  2. /**
  3. * Created by Tony on 2017/4/1.
  4. */
  5. const JV = require('../rpt_component/jpc_value_define');
  6. const fs = require('fs');
  7. const JSZip = require('jszip');
  8. const strUtil = require('../public/stringUtil');
  9. const jpcCmnHelper = require('../rpt_component/helper/jpc_helper_common');
  10. const DPI = jpcCmnHelper.getScreenDPI()[0];
  11. const fsUtil = require('../public/fsUtil');
  12. const dftHeadXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
  13. const uuidV1 = require('uuid/v1');
  14. const atob = require('atob');
  15. function writeContentTypes(sheets, isSinglePage, hasSignature) {
  16. const rst = [];
  17. rst.push(dftHeadXml + '\r\n');
  18. rst.push('<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">');
  19. rst.push('<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>');
  20. rst.push('<Default Extension="xml" ContentType="application/xml"/>');
  21. if (hasSignature) {
  22. rst.push('<Default Extension="png" ContentType="image/png"/>');
  23. }
  24. rst.push('<Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/>');
  25. rst.push('<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>');
  26. rst.push('<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>');
  27. rst.push('<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>');
  28. rst.push('<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>');
  29. if (isSinglePage) {
  30. rst.push('<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>');
  31. if (hasSignature) {
  32. rst.push('<Override PartName="/xl/drawings/drawing1.xml" ContentType="application/vnd.openxmlformats-officedocument.drawing+xml"/>');
  33. }
  34. } else {
  35. for (let i = 0; i < sheets.length; i++) {
  36. rst.push('<Override PartName="/xl/worksheets/sheet' + (i + 1) + '.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>');
  37. if (hasSignature) {
  38. rst.push('<Override PartName="/xl/drawings/drawing' + (i + 1) + '.xml" ContentType="application/vnd.openxmlformats-officedocument.drawing+xml"/>');
  39. }
  40. }
  41. }
  42. rst.push('<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>');
  43. rst.push('</Types>');
  44. return rst;
  45. }
  46. function writeRootRels() {
  47. const rst = [];
  48. rst.push(dftHeadXml + '\r\n');
  49. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  50. rst.push('<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>');
  51. rst.push('<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>');
  52. rst.push('<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>');
  53. rst.push('</Relationships>');
  54. return rst;
  55. }
  56. function writeApp(sheets, isSinglePage) {
  57. const rst = [];
  58. rst.push(dftHeadXml + '\r\n');
  59. rst.push('<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">');
  60. rst.push('<Application>Microsoft Excel</Application>');
  61. rst.push('<DocSecurity>0</DocSecurity>');
  62. rst.push('<ScaleCrop>false</ScaleCrop>');
  63. rst.push('<HeadingPairs>');
  64. rst.push('<vt:vector size="2" baseType="variant">');
  65. rst.push('<vt:variant><vt:lpstr>工作表</vt:lpstr></vt:variant>');
  66. if (isSinglePage) rst.push('<vt:variant><vt:i4>1</vt:i4></vt:variant>');
  67. else rst.push('<vt:variant><vt:i4>' + sheets.length + '</vt:i4></vt:variant>');
  68. rst.push('</vt:vector>');
  69. rst.push('</HeadingPairs>');
  70. rst.push('<TitlesOfParts>');
  71. if (isSinglePage) {
  72. rst.push('<vt:vector size="1" baseType="lpstr">');
  73. rst.push('<vt:lpstr>' + sheets[0].sheetName + '</vt:lpstr>');
  74. } else {
  75. rst.push('<vt:vector size="' + sheets.length + '" baseType="lpstr">');
  76. for (let i = 0; i < sheets.length; i++) {
  77. rst.push('<vt:lpstr>' + sheets[i].sheetName + '</vt:lpstr>');
  78. }
  79. }
  80. rst.push('</vt:vector>');
  81. rst.push('</TitlesOfParts>');
  82. rst.push('<Company>SmartCost</Company>');
  83. rst.push('<LinksUpToDate>false</LinksUpToDate>');
  84. rst.push('<SharedDoc>false</SharedDoc>');
  85. rst.push('<HyperlinksChanged>false</HyperlinksChanged>');
  86. rst.push('<AppVersion>12.0000</AppVersion>');
  87. rst.push('</Properties>');
  88. return rst;
  89. }
  90. function writeCore() {
  91. const rst = [];
  92. const p_fillZero = function(val) {
  93. let rst = val;
  94. if (val < 10) {
  95. rst = '0' + val;
  96. }
  97. return rst;
  98. };
  99. rst.push(dftHeadXml + '\r\n');
  100. 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">');
  101. rst.push('<dc:creator>SmartCost</dc:creator>');
  102. rst.push('<cp:lastModifiedBy>SmartCost</cp:lastModifiedBy>');
  103. const dt = new Date();
  104. dt.setDate(dt.getDate() - 8 / 24); // it's GMT time, so please add the server offset time ( -8 hours )
  105. const dtStr = dt.getFullYear() + '-' + p_fillZero(dt.getMonth() + 1) + '-' + p_fillZero(dt.getDate()) + 'T' +
  106. p_fillZero(dt.getHours()) + ':' + p_fillZero(dt.getMinutes()) + ':' + p_fillZero(dt.getSeconds()) + 'Z';
  107. rst.push('<dcterms:created xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:created>');
  108. rst.push('<dcterms:modified xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:modified>');
  109. rst.push('</cp:coreProperties>');
  110. return rst;
  111. }
  112. function writeXlWorkBook(sheets, isSinglePage) {
  113. const rst = [];
  114. rst.push(dftHeadXml + '\r\n');
  115. rst.push('<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  116. rst.push('<fileVersion appName="xl" lastEdited="4" lowestEdited="4" rupBuild="4505"/>');
  117. rst.push('<workbookPr defaultThemeVersion="124226"/>');
  118. rst.push('<bookViews><workbookView xWindow="360" yWindow="345" windowWidth="14655" windowHeight="4305"/></bookViews>');
  119. rst.push('<sheets>');
  120. if (isSinglePage) {
  121. rst.push('<sheet name="' + sheets[0].sheetName + '" sheetId="1" r:id="rId1"/>');
  122. } else {
  123. for (let i = 0; i < sheets.length; i++) {
  124. rst.push('<sheet name="' + sheets[i].sheetName + '" sheetId="' + (i + 1) + '" r:id="rId' + (i + 1) + '"/>');
  125. }
  126. }
  127. rst.push('</sheets>');
  128. rst.push('<calcPr calcId="124519"/>');
  129. rst.push('</workbook>');
  130. return rst;
  131. }
  132. function writeXlRels(sheets, isSinglePage) {
  133. const rst = [];
  134. let idx = 1;
  135. rst.push(dftHeadXml + '\r\n');
  136. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  137. if (isSinglePage) {
  138. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>');
  139. idx++;
  140. } else {
  141. for (let i = 0; i < sheets.length; i++) {
  142. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet' + (i + 1) + '.xml"/>');
  143. idx++;
  144. }
  145. }
  146. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>');
  147. idx++;
  148. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>');
  149. idx++;
  150. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/>');
  151. rst.push('</Relationships>');
  152. return rst;
  153. }
  154. function writeTheme() {
  155. const rst = fs.readFileSync(__dirname + '/excel_base_files/theme1.xml', 'utf8', 'r');
  156. return rst;
  157. }
  158. function writeStyles(stylesObj, hasSignature) {
  159. const rst = [];
  160. rst.push(dftHeadXml + '\r\n');
  161. rst.push('<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">');
  162. // 1. push fonts
  163. rst.push('<fonts count="' + stylesObj.fonts.length + '">');
  164. for (let i = 0; i < stylesObj.fonts.length; i++) {
  165. const font = stylesObj.fonts[i];
  166. rst.push('<font>');
  167. if (strUtil.convertStrToBoolean(font[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]])) {
  168. rst.push('<b/>');
  169. }
  170. if (strUtil.convertStrToBoolean(font[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]])) {
  171. rst.push('<i/>');
  172. }
  173. if (strUtil.convertStrToBoolean(font[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]])) {
  174. rst.push('<u/>');
  175. }
  176. rst.push('<sz val="' + font.size + '"/>');
  177. rst.push('<color indexed="' + font.colorIdx + '"/>');
  178. rst.push('<name val="' + font[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]] + '"/>');
  179. rst.push('<charset val="' + font.charset + '"/>');
  180. rst.push('</font>');
  181. }
  182. rst.push('</fonts>');
  183. // 2. push default fills
  184. rst.push('<fills count="2"><fill><patternFill patternType="none" /></fill><fill><patternFill patternType="gray125" /></fill></fills>');
  185. // 3. push borders
  186. rst.push('<borders count="' + stylesObj.borders.length + '">');
  187. const private_setBorder = function(border, borderDirection) {
  188. if (parseInt(border[borderDirection][JV.PROP_LINE_WEIGHT]) === 0) {
  189. rst.push('<' + borderDirection.toLowerCase() + '/>');
  190. } else {
  191. let bW = 'thin';
  192. if (parseInt(border[borderDirection][JV.PROP_LINE_WEIGHT]) === 2) bW = 'medium';
  193. if (parseInt(border[borderDirection][JV.PROP_LINE_WEIGHT]) > 2) bW = 'thick';
  194. rst.push('<' + borderDirection.toLowerCase() + ' style="' + bW + '"><color indexed="64"/></' + borderDirection.toLowerCase() + '>');
  195. }
  196. };
  197. for (let i = 0; i < stylesObj.borders.length; i++) {
  198. const border = stylesObj.borders[i];
  199. rst.push('<border>');
  200. private_setBorder(border, JV.PROP_LEFT);
  201. private_setBorder(border, JV.PROP_RIGHT);
  202. private_setBorder(border, JV.PROP_TOP);
  203. private_setBorder(border, JV.PROP_BOTTOM);
  204. rst.push('<diagonal />');
  205. rst.push('</border>');
  206. }
  207. rst.push('</borders>');
  208. // 4. push cellStyleXfs
  209. rst.push('<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"><alignment vertical="center"/></xf></cellStyleXfs>');
  210. // 5. push cellXfs
  211. rst.push('<cellXfs count="' + stylesObj.cellXfs.length + '">');
  212. for (let i = 0; i < stylesObj.cellXfs.length; i++) {
  213. const excelStyle = stylesObj.cellXfs[i];
  214. rst.push('<xf numFmtId="0" fontId="' + excelStyle.fontId + '" fillId="0" borderId="' + excelStyle.borderId + '" xfId="0">');
  215. // pageData[JV.NODE_FONT_COLLECTION] excelStyle.fontId
  216. let alignStr = '<alignment';
  217. let textRotation = 0;
  218. let newHorizontal = excelStyle[JV.CONTROL_PROPS[2]];
  219. let newVertical = excelStyle[JV.CONTROL_PROPS[3]];
  220. if (excelStyle[JV.CONTROL_PROPS[5]] && !strUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_CLOSE_OUTPUT]])) {
  221. newVertical = excelStyle[JV.CONTROL_PROPS[5]];
  222. }
  223. if (parseInt(excelStyle.fontAngle) !== 0) {
  224. let tmpH = newHorizontal;
  225. let tmpV = newVertical;
  226. if (excelStyle.fontAngle > 0) {
  227. textRotation = 180;
  228. if (newHorizontal === 'left') {
  229. tmpV = 'top';
  230. } else if (newHorizontal === 'right') {
  231. tmpV = 'bottom';
  232. } else {
  233. tmpV = 'center';
  234. }
  235. if (newVertical === 'top') {
  236. tmpH = 'right';
  237. } else if (newVertical === 'bottom') {
  238. tmpH = 'left';
  239. } else if (newVertical === 'justify') {
  240. tmpH = 'justify';
  241. } else {
  242. tmpH = 'center';
  243. }
  244. } else {
  245. textRotation = 90;
  246. if (newHorizontal === 'left') {
  247. tmpV = 'bottom';
  248. } else if (newHorizontal === 'right') {
  249. tmpV = 'top';
  250. } else {
  251. tmpV = 'center';
  252. }
  253. if (newVertical === 'top') {
  254. tmpH = 'left';
  255. } else if (newVertical === 'bottom') {
  256. tmpH = 'right';
  257. } else {
  258. tmpH = 'center';
  259. }
  260. }
  261. newHorizontal = tmpH;
  262. newVertical = tmpV;
  263. }
  264. alignStr += ' horizontal="' + newHorizontal + '" vertical="' + newVertical + '"';
  265. if (strUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[0]])) {
  266. alignStr += ' shrinkToFit="1"';
  267. }
  268. if (strUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[4]]) && !strUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[6]])) {
  269. alignStr += ' wrapText="1"';
  270. }
  271. if (textRotation !== 0) {
  272. alignStr += ' textRotation="' + textRotation + '"';
  273. }
  274. alignStr += '/>';
  275. rst.push(alignStr);
  276. rst.push('</xf>');
  277. }
  278. rst.push('</cellXfs>');
  279. // 6. others (xfl style / dxfs / tableStyles)
  280. rst.push('<cellStyles count="1"><cellStyle name="常规" xfId="0" builtinId="0"/></cellStyles>');
  281. rst.push('<dxfs count="0"/>');
  282. rst.push('<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleLight16"/>');
  283. if (hasSignature) {
  284. rst.push('<extLst>');
  285. rst.push('<ext uri="{EB79DEF2-80B8-43e5-95BD-54CBDDF9020C}" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main">');
  286. rst.push('<x14:slicerStyles defaultSlicerStyle="SlicerStyleLight1"/>');
  287. rst.push('</ext>');
  288. rst.push('<ext uri="{9260A510-F301-46a8-8635-F512D64BE5F5}" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main">');
  289. rst.push('<x15:timelineStyles defaultTimelineStyle="TimeSlicerStyleLight1"/>');
  290. rst.push('</ext>');
  291. rst.push('</extLst>');
  292. }
  293. rst.push('</styleSheet>');
  294. return rst;
  295. }
  296. function writeSharedString(sharedStrList) {
  297. const rst = [];
  298. const pri_func_write = function(cellVal) {
  299. if (cellVal !== null) {
  300. if ((typeof cellVal === 'string') && cellVal.indexOf(' ') === 0) {
  301. rst.push('<si><t xml:space="preserve">' + cellVal + '</t></si>');
  302. } else {
  303. rst.push('<si><t>' + cellVal + '</t></si>');
  304. }
  305. }
  306. };
  307. if (sharedStrList && sharedStrList.length > 0) {
  308. rst.push(dftHeadXml + '\r\n');
  309. rst.push('<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="' + sharedStrList.length + '" uniqueCount="' + sharedStrList.length + '">');
  310. const regExp = new RegExp('<', 'gm');
  311. for (let i = 0; i < sharedStrList.length; i++) {
  312. if (typeof sharedStrList[i] === 'string') {
  313. // 转换特殊字符,如 < , 则需要转义一下
  314. sharedStrList[i] = sharedStrList[i].replace(regExp, '&lt;');
  315. if (sharedStrList[i].indexOf('|') >= 0) {
  316. // rst.push('<si><t>' + sharedStrList[i].split('|').join('\r\n') + '</t></si>');
  317. // rst.push('<si><t>' + sharedStrList[i].split('|').join('\n') + '</t></si>');
  318. pri_func_write(sharedStrList[i].split('|').join('\n'));
  319. } else {
  320. // rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
  321. pri_func_write(sharedStrList[i]);
  322. }
  323. // rst.push('<si><t>' + sharedStrList[i].replace('|','\r\n') + '</t></si>');
  324. } else {
  325. // rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
  326. pri_func_write(sharedStrList[i]);
  327. }
  328. }
  329. rst.push('</sst>');
  330. }
  331. return rst;
  332. }
  333. function writeSheets(pageData, paperSize, sharedStrList, stylesObj, isSinglePage, custSheetMergeBands, hasSignature, signSheetIdxArr) {
  334. const rst = [];
  335. const private_pushDftFont = function() {
  336. const font = {};
  337. if (!(stylesObj.fonts)) {
  338. stylesObj.fonts = [];
  339. }
  340. font[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]] = '宋体'; // font name
  341. font.size = 12;
  342. font.charset = 134;
  343. font.colorIdx = '8';
  344. stylesObj.fonts.push(font);
  345. };
  346. const private_buildFirstDftStyle = function() {
  347. stylesObj.cellXfs = [];
  348. stylesObj.borders = [];
  349. const fontId = 0;
  350. const borderId = 0;
  351. const border = {};
  352. border[JV.PROP_LEFT] = {};
  353. border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = 0;
  354. border[JV.PROP_RIGHT] = {};
  355. border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = 0;
  356. border[JV.PROP_TOP] = {};
  357. border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = 0;
  358. border[JV.PROP_BOTTOM] = {};
  359. border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = 0;
  360. stylesObj.borders.push(border);
  361. const cellControl = pageData[JV.NODE_CONTROL_COLLECTION].Default;
  362. const sheetControl = {};
  363. sheetControl.fontId = fontId;
  364. sheetControl.borderId = borderId;
  365. sheetControl.fontAngle = 0;
  366. for (let i = 0; i < JV.CONTROL_PROPS.length; i++) {
  367. sheetControl[JV.CONTROL_PROPS[i]] = cellControl[JV.CONTROL_PROPS[i]];
  368. }
  369. stylesObj.cellXfs.push(sheetControl);
  370. };
  371. private_pushDftFont();
  372. private_buildFirstDftStyle();
  373. let sheetIdx = 0;
  374. if (isSinglePage) {
  375. rst.push(writeSheet(pageData, null, paperSize, sharedStrList, stylesObj, null, hasSignature, sheetIdx, signSheetIdxArr));
  376. } else {
  377. for (let i = 0; i < pageData.items.length; i++) {
  378. let appointedMergeBand = null;
  379. if (custSheetMergeBands && custSheetMergeBands.length > i) {
  380. appointedMergeBand = custSheetMergeBands[i];
  381. }
  382. rst.push(writeSheet(pageData, pageData.items[i], paperSize, sharedStrList, stylesObj, appointedMergeBand, hasSignature, sheetIdx, signSheetIdxArr));
  383. sheetIdx++;
  384. }
  385. }
  386. return rst;
  387. }
  388. function getProperSignatureArea(cell, control) {
  389. // 约定默认长宽比例是2:1,图片分辨率是600*300
  390. const rst = [0, 0, 0, 0]; // left, top, right, bottom
  391. if (cell && cell[JV.PROP_AREA]) {
  392. let width = cell[JV.PROP_AREA][JV.PROP_RIGHT] - cell[JV.PROP_AREA][JV.PROP_LEFT],
  393. height = cell[JV.PROP_AREA][JV.PROP_BOTTOM] - cell[JV.PROP_AREA][JV.PROP_TOP];
  394. if (width > height * 2) {
  395. width = height * 2;
  396. } else {
  397. height = width / 2;
  398. }
  399. switch (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_HORIZON]]) {
  400. case 'left':
  401. rst[0] = cell[JV.PROP_AREA][JV.PROP_LEFT];
  402. rst[1] = cell[JV.PROP_AREA][JV.PROP_TOP];
  403. rst[2] = rst[0] + width;
  404. rst[3] = rst[1] + height;
  405. break;
  406. case 'right':
  407. rst[2] = cell[JV.PROP_AREA][JV.PROP_RIGHT];
  408. rst[3] = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  409. rst[0] = rst[2] - width;
  410. rst[1] = rst[3] - height;
  411. break;
  412. default:
  413. // center
  414. rst[0] = (cell[JV.PROP_AREA][JV.PROP_LEFT] + cell[JV.PROP_AREA][JV.PROP_RIGHT] - width) / 2;
  415. rst[1] = cell[JV.PROP_AREA][JV.PROP_TOP];
  416. rst[2] = rst[0] + width;
  417. rst[3] = rst[1] + height;
  418. break;
  419. }
  420. }
  421. // rst[0] = rst[0] + JpcCanvasOutput.offsetX;
  422. // rst[2] = rst[2] + JpcCanvasOutput.offsetX;
  423. // rst[1] = rst[1] + JpcCanvasOutput.offsetY;
  424. // rst[3] = rst[3] + JpcCanvasOutput.offsetY;
  425. return rst;
  426. }
  427. function preAnalyzePos(pageData, sheetData, xPos, yPos, yMultiPos) {
  428. let cell;
  429. let pos;
  430. const private_array_sort = function(i1, i2) {
  431. return (i1 - i2);
  432. };
  433. const private_set_cell_pos = function(theCell, theXPos, theYPos) {
  434. pos = theCell[JV.PROP_AREA][JV.PROP_LEFT];
  435. if (theXPos.indexOf(pos) < 0) theXPos.push(pos);
  436. pos = theCell[JV.PROP_AREA][JV.PROP_RIGHT];
  437. if (theXPos.indexOf(pos) < 0) theXPos.push(pos);
  438. pos = theCell[JV.PROP_AREA][JV.PROP_TOP];
  439. if (theYPos.indexOf(pos) < 0) theYPos.push(pos);
  440. pos = theCell[JV.PROP_AREA][JV.PROP_BOTTOM];
  441. if (theYPos.indexOf(pos) < 0) theYPos.push(pos);
  442. };
  443. const private_set_cell_pos2 = function(area, theXPos, theYPos) {
  444. pos = area[0];
  445. if (theXPos.indexOf(pos) < 0) theXPos.push(pos);
  446. pos = area[2];
  447. if (theXPos.indexOf(pos) < 0) theXPos.push(pos);
  448. pos = area[1];
  449. if (theYPos.indexOf(pos) < 0) theYPos.push(pos);
  450. pos = area[3];
  451. if (theYPos.indexOf(pos) < 0) theYPos.push(pos);
  452. };
  453. const self_analyze_sheet_pos = function(theShtData, theXPos, theYPos) {
  454. for (let i = 0; i < theShtData[JV.PROP_CELLS].length; i++) {
  455. cell = theShtData[JV.PROP_CELLS][i];
  456. private_set_cell_pos(cell, theXPos, theYPos);
  457. }
  458. for (let i = 0; i < theShtData[JV.PROP_SIGNATURE_CELLS].length; i++) {
  459. cell = theShtData[JV.PROP_SIGNATURE_CELLS][i];
  460. private_set_cell_pos(cell, theXPos, theYPos);
  461. let cellControl;
  462. if (typeof cell[JV.PROP_CONTROL] === 'string') {
  463. cellControl = pageData[JV.NODE_CONTROL_COLLECTION][cell[JV.PROP_CONTROL]];
  464. } else {
  465. cellControl = cell[JV.PROP_CONTROL];
  466. }
  467. const area = getProperSignatureArea(cell, cellControl);
  468. private_set_cell_pos2(area, theXPos, theYPos);
  469. }
  470. for (let i = 0; i < theShtData[JV.PROP_SIGNATURE_DATE_CELLS].length; i++) {
  471. cell = theShtData[JV.PROP_SIGNATURE_DATE_CELLS][i];
  472. private_set_cell_pos(cell, theXPos, theYPos);
  473. }
  474. };
  475. xPos.push(0);
  476. if (sheetData) {
  477. // current sheet data
  478. // console.log('preAnalyzePos not single');
  479. yPos.push(0);
  480. self_analyze_sheet_pos(sheetData, xPos, yPos);
  481. xPos.sort(private_array_sort);
  482. yPos.sort(private_array_sort);
  483. } else {
  484. // total data in one sheet
  485. const marginBottomPos = Math.round((pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1] - parseFloat(pageData[JV.NODE_PAGE_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM]) / 2.54) * DPI);
  486. // console.log('preAnalyzePos isSingle start');
  487. for (const shtItemData of pageData.items) {
  488. const tmpPos = [];
  489. tmpPos.push(0);
  490. self_analyze_sheet_pos(shtItemData, xPos, tmpPos);
  491. tmpPos.sort(private_array_sort);
  492. if (marginBottomPos - tmpPos[tmpPos.length - 1] > 10) {
  493. // 此逻辑是为了防止打印跨页(假设有些报表模板高度设置离底部margin还好远,导出excel后预览时会发现跨页现象(即下一页的某几行数据会挪到前一页来预览))
  494. tmpPos.push(marginBottomPos - 10);
  495. }
  496. // console.log('preAnalyzePos push to yMultiPos');
  497. yMultiPos.push(tmpPos);
  498. }
  499. xPos.sort(private_array_sort);
  500. yPos.splice(0, yPos.length);
  501. for (const p of yMultiPos[0]) {
  502. yPos.push(p);
  503. }
  504. }
  505. }
  506. function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, appointedMergeBand, hasSignature, sheetIdx, signSheetIdxArr) {
  507. const rst = [];
  508. const xPos = [];
  509. const yPos = [];
  510. const yMultiPos = [];
  511. let currentMergeBorder = null;
  512. const headerStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  513. let currentPageMergePos = null; // 在 JV.PAGING_OPTION_INFINITY 场合应用
  514. const private_getCellIdxStr = function(idx) {
  515. let rst = 'A';
  516. if (idx < 26) {
  517. rst = headerStr[idx];
  518. } else if (idx < 26 * 26 + 26) {
  519. const ti = Math.floor(idx / 26);
  520. const tj = idx % 26;
  521. rst = headerStr[ti - 1] + headerStr[tj];
  522. } else if (idx < 26 * 26 * 26 + 26) {
  523. const ti = Math.floor(idx / (26 * 26));
  524. const tj = Math.floor((idx - ti * 26 * 26) / 26);
  525. const tk = idx % 26;
  526. rst = headerStr[ti - 1] + headerStr[tj - 1] + headerStr[tk];
  527. }
  528. return rst;
  529. };
  530. const private_getSharedStrIdx = function(val) {
  531. let strVal = val;
  532. if (val === null || val === undefined || (typeof val === 'number' && isNaN(val))) {
  533. strVal = '';
  534. }
  535. let rst = sharedStrList.indexOf(strVal);
  536. if (rst < 0) {
  537. sharedStrList.push(strVal);
  538. rst = sharedStrList.length - 1;
  539. }
  540. return rst;
  541. };
  542. const private_getFontId = function(cell) {
  543. let rst = 0;
  544. let hasFont = false;
  545. if (!(stylesObj.fonts)) {
  546. stylesObj.fonts = [];
  547. // for (let i = 0; i < sheetData.font_collection)
  548. }
  549. let sheetFont = null;
  550. if (typeof cell[JV.PROP_FONT] === 'string') {
  551. sheetFont = pageData[JV.NODE_FONT_COLLECTION][cell[JV.PROP_FONT]];
  552. } else {
  553. sheetFont = cell[JV.PROP_FONT];
  554. }
  555. for (let i = 0; i < stylesObj.fonts.length; i++) {
  556. const font = stylesObj.fonts[i];
  557. if (sheetFont) {
  558. if (font[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]] === sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]] && font.size === Math.floor(sheetFont[JV.FONT_PROPS[1]] * 3 / 4)
  559. && font[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] === sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] && font[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]] === sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]]
  560. && font[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]] === sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]]) {
  561. hasFont = true;
  562. rst = i;
  563. break;
  564. }
  565. } else {
  566. break;
  567. }
  568. }
  569. if (!hasFont) {
  570. const font = {};
  571. font[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]] = sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]]; // font name
  572. // font.size = Math.round(sheetFont[JV.FONT_PROPS[1]] * 3 / 4);
  573. font.size = Math.floor(sheetFont[JV.FONT_PROPS[1]] * 3 / 4);
  574. font.charset = 134;
  575. font.colorIdx = '8';
  576. font[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] = sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]]; // font bold
  577. font[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]] = sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]]; // font italic
  578. font[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]] = sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]]; // font underline
  579. stylesObj.fonts.push(font);
  580. rst = stylesObj.fonts.length - 1;
  581. }
  582. return rst;
  583. };
  584. const private_chkAndGetMergeLine = function(cell, sheetBorder, borderStr, needFurtherChk) {
  585. let rst = 0;
  586. const mergeBorder = currentMergeBorder;
  587. let mergeBand = pageData[JV.BAND_PROP_MERGE_BAND];
  588. if (appointedMergeBand !== null) {
  589. mergeBand = appointedMergeBand;
  590. }
  591. if (sheetBorder[borderStr] && sheetBorder[borderStr][JV.PROP_LINE_WEIGHT] !== undefined) {
  592. rst = sheetBorder[borderStr][JV.PROP_LINE_WEIGHT];
  593. }
  594. if (currentPageMergePos) {
  595. const side = currentPageMergePos[borderStr];
  596. if (side.indexOf(cell[JV.PROP_AREA][borderStr]) >= 0) {
  597. if (needFurtherChk) {
  598. const topSide = currentPageMergePos[JV.PROP_TOP];
  599. const bottomSide = currentPageMergePos[JV.PROP_BOTTOM];
  600. for (let i = 0; i < topSide.length; i++) {
  601. if (cell[JV.PROP_AREA][JV.PROP_TOP] >= topSide[i]) {
  602. if (cell[JV.PROP_AREA][JV.PROP_BOTTOM] <= bottomSide[i]) {
  603. const destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
  604. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  605. break;
  606. }
  607. } else {
  608. break;
  609. }
  610. }
  611. } else {
  612. const destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
  613. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  614. }
  615. }
  616. } else {
  617. if (cell[JV.PROP_AREA][borderStr] === mergeBorder[borderStr]) {
  618. const destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
  619. if (needFurtherChk) {
  620. if (cell[JV.PROP_AREA][JV.PROP_TOP] >= mergeBorder[JV.PROP_TOP] &&
  621. cell[JV.PROP_AREA][JV.PROP_BOTTOM] <= mergeBorder[JV.PROP_BOTTOM]) {
  622. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  623. }
  624. } else {
  625. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  626. }
  627. }
  628. }
  629. return parseInt(rst);
  630. };
  631. const private_checkBorder = function(cell, border, sheetBorder) {
  632. let rst = true;
  633. const borderLineWidths = [];
  634. const sheetBorderLineWidths = [];
  635. borderLineWidths.push(border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]);
  636. borderLineWidths.push(border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]);
  637. borderLineWidths.push(border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]);
  638. borderLineWidths.push(border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]);
  639. if (sheetBorder[JV.PROP_LEFT] && sheetBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]) {
  640. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_LEFT, true));
  641. } else {
  642. sheetBorderLineWidths.push(0);
  643. }
  644. if (sheetBorder[JV.PROP_RIGHT] && sheetBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]) {
  645. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_RIGHT, true));
  646. } else {
  647. sheetBorderLineWidths.push(0);
  648. }
  649. if (sheetBorder[JV.PROP_TOP] && sheetBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]) {
  650. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_TOP, false));
  651. } else {
  652. sheetBorderLineWidths.push(0);
  653. }
  654. if (sheetBorder[JV.PROP_BOTTOM] && sheetBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]) {
  655. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_BOTTOM, false));
  656. } else {
  657. sheetBorderLineWidths.push(0);
  658. }
  659. for (let i = 0; i < 4; i++) {
  660. if (borderLineWidths[i] != sheetBorderLineWidths[i]) {
  661. rst = false;
  662. break;
  663. }
  664. }
  665. return rst;
  666. };
  667. const private_getIniBorder = function() {
  668. const rst = {};
  669. rst[JV.PROP_LEFT] = {};
  670. rst[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = 0;
  671. rst[JV.PROP_RIGHT] = {};
  672. rst[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = 0;
  673. rst[JV.PROP_TOP] = {};
  674. rst[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = 0;
  675. rst[JV.PROP_BOTTOM] = {};
  676. rst[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = 0;
  677. return rst;
  678. };
  679. const private_getBorderId = function(cell) {
  680. let rst = 0;
  681. let hasBorder = false;
  682. if (!(stylesObj.borders)) {
  683. stylesObj.borders = [];
  684. }
  685. const sheetBorder = pageData[JV.NODE_STYLE_COLLECTION][cell.style];
  686. const mergedBorder = private_getIniBorder();
  687. mergedBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_LEFT, true);
  688. mergedBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_RIGHT, true);
  689. mergedBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_TOP, false);
  690. mergedBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_BOTTOM, false);
  691. for (let i = 0; i < stylesObj.borders.length; i++) {
  692. const border = stylesObj.borders[i];
  693. if (private_checkBorder(cell, border, mergedBorder)) {
  694. hasBorder = true;
  695. rst = i;
  696. break;
  697. }
  698. }
  699. if (!hasBorder) {
  700. stylesObj.borders.push(mergedBorder);
  701. rst = stylesObj.borders.length - 1;
  702. }
  703. return rst;
  704. };
  705. const private_checkControl = function(cellControl, sheetControl) {
  706. let rst = true;
  707. for (let i = 0; i < JV.CONTROL_PROPS.length; i++) {
  708. if (cellControl[JV.CONTROL_PROPS[i]] !== sheetControl[JV.CONTROL_PROPS[i]]) {
  709. rst = false;
  710. break;
  711. }
  712. }
  713. return rst;
  714. };
  715. const private_getStyleId = function(cell) {
  716. let rst = 1;
  717. let hasStyle = false;
  718. if (!(stylesObj.cellXfs)) stylesObj.cellXfs = [];
  719. const fontId = private_getFontId(cell);
  720. let fontAngle = 0;
  721. if (typeof cell[JV.PROP_FONT] === 'string') {
  722. fontAngle = parseInt(pageData[JV.NODE_FONT_COLLECTION][cell[JV.PROP_FONT]].FontAngle);
  723. } else {
  724. fontAngle = parseInt(cell[JV.PROP_FONT].FontAngle);
  725. }
  726. const borderId = private_getBorderId(cell);
  727. let cellControl = null;
  728. if (typeof cell[JV.PROP_CONTROL] === 'string') {
  729. cellControl = pageData[JV.NODE_CONTROL_COLLECTION][cell[JV.PROP_CONTROL]];
  730. } else {
  731. cellControl = cell[JV.PROP_CONTROL];
  732. }
  733. for (let i = 0; i < stylesObj.cellXfs.length; i++) {
  734. const sheetControl = stylesObj.cellXfs[i];
  735. if (sheetControl.fontId === fontId && sheetControl.borderId === borderId) {
  736. if (private_checkControl(cellControl, sheetControl)) {
  737. rst = i;
  738. hasStyle = true;
  739. break;
  740. }
  741. }
  742. }
  743. if (!hasStyle) {
  744. const sheetControl = {};
  745. sheetControl.fontId = fontId;
  746. sheetControl.borderId = borderId;
  747. sheetControl.fontAngle = fontAngle;
  748. for (let i = 0; i < JV.CONTROL_PROPS.length; i++) {
  749. sheetControl[JV.CONTROL_PROPS[i]] = cellControl[JV.CONTROL_PROPS[i]];
  750. }
  751. stylesObj.cellXfs.push(sheetControl);
  752. rst = stylesObj.cellXfs.length - 1;
  753. }
  754. return rst;
  755. };
  756. const private_setCols = function() {
  757. // remark: 1 excel unit width = 2.117 mm
  758. rst.push('<cols>');
  759. let w = 0;
  760. for (let i = 1; i < xPos.length - 1; i++) {
  761. w = (xPos[i + 1] - xPos[i]) / DPI * 25.4 / 2.117;
  762. w = Math.round(w * 1000) / 1000;
  763. rst.push('<col min="' + i + '" max="' + i + '" width="' + w + '" customWidth="1"/>');
  764. }
  765. rst.push('<col min="' + (xPos.length - 1) + '" max="' + (xPos.length - 1) + '" width="' + 10 + '" customWidth="1"/>');
  766. rst.push('</cols>');
  767. };
  768. const private_setMergedCells = function() {
  769. let cnt = 0;
  770. rst.push('<mergeCells count="?">');
  771. const startIdx = rst.length - 1;
  772. const self_setMergedCells = function(theData, theYPos, offsetY) {
  773. for (let i = 0; i < theData.cells.length; i++) {
  774. const cell = theData.cells[i];
  775. const idxR = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_RIGHT]);
  776. const idxL = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_LEFT]);
  777. const idxB = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_BOTTOM]);
  778. const idxT = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_TOP]);
  779. if (idxR - idxL > 1 || idxB - idxT > 1) {
  780. rst.push('<mergeCell ref="' + private_getCellIdxStr(idxL - 1) + (idxT + offsetY) + ':' + private_getCellIdxStr(idxR - 2) + (idxB - 1 + offsetY) + '"/>');
  781. cnt++;
  782. }
  783. }
  784. };
  785. if (sheetData) {
  786. self_setMergedCells(sheetData, yPos, 0);
  787. } else {
  788. let osY = 0;
  789. for (let i = 0; i < pageData.items.length; i++) {
  790. const shtItemData = pageData.items[i];
  791. const tmpPos = yMultiPos[i];
  792. self_setMergedCells(shtItemData, tmpPos, osY);
  793. osY += tmpPos.length - 2;
  794. }
  795. }
  796. rst[startIdx] = '<mergeCells count="' + cnt + '">';
  797. rst.push('</mergeCells>');
  798. };
  799. const private_setSheetData = function() {
  800. // remark: 1 excel unit height = 0.3612 mm
  801. rst.push('<sheetData>');
  802. const spanX = xPos.length - 2;
  803. let cellIdx = 0;
  804. let h = 0;
  805. const self_setDataEx = function(theShtData, theYPos, rowOffset) {
  806. const rows = [];
  807. // 1. build full set of blank rows/cells
  808. for (let i = 1; i < theYPos.length - 1; i++) {
  809. const rowObj = {};
  810. h = (theYPos[i + 1] - theYPos[i]) / DPI * 25.4 / 0.3612;
  811. h = Math.round(h * 1000) / 1000;
  812. rowObj.height = h;
  813. rowObj.r = i + rowOffset;
  814. rowObj.items = [];
  815. rows.push(rowObj);
  816. for (let j = 1; j < xPos.length - 1; j++) {
  817. const colIdxStr = private_getCellIdxStr(j - 1);
  818. const cellObj = {};
  819. cellObj.r = colIdxStr + (i + rowOffset);
  820. cellObj.s = 0;
  821. cellObj.isBlank = true;
  822. rows[i - 1].items.push(cellObj);
  823. }
  824. }
  825. // 2. then fill up the cell style-ids and values
  826. let rowIdx1 = 0;
  827. let colIdx1 = 0;
  828. let rowIdx2 = 0;
  829. let colIdx2 = 0;
  830. // let colIdxStr = '';
  831. for (let cIdx = 0; cIdx < theShtData.cells.length; cIdx++) {
  832. const styleIdx = private_getStyleId(theShtData.cells[cIdx]);
  833. rowIdx1 = theYPos.indexOf(theShtData.cells[cIdx][JV.PROP_AREA][JV.PROP_TOP]);
  834. colIdx1 = xPos.indexOf(theShtData.cells[cIdx][JV.PROP_AREA][JV.PROP_LEFT]);
  835. let cellObj = rows[rowIdx1 - 1].items[colIdx1 - 1];
  836. cellObj.s = styleIdx;
  837. cellObj.isBlank = false;
  838. if (!(strUtil.isEmptyString(theShtData.cells[cIdx][JV.PROP_VALUE]))) {
  839. const valIdx = private_getSharedStrIdx(theShtData.cells[cIdx][JV.PROP_VALUE]);
  840. cellObj.v = valIdx;
  841. }
  842. rowIdx2 = theYPos.indexOf(theShtData.cells[cIdx][JV.PROP_AREA][JV.PROP_BOTTOM]);
  843. colIdx2 = xPos.indexOf(theShtData.cells[cIdx][JV.PROP_AREA][JV.PROP_RIGHT]);
  844. if ((rowIdx2 - rowIdx1 > 1) || (colIdx2 - colIdx1 > 1)) {
  845. for (let i = 0; i < rowIdx2 - rowIdx1; i++) {
  846. for (let j = 0; j < colIdx2 - colIdx1; j++) {
  847. if (i === 0 && j === 0) continue;
  848. cellObj = rows[rowIdx1 - 1 + i].items[colIdx1 - 1 + j];
  849. cellObj.s = styleIdx;
  850. cellObj.isBlank = true;
  851. }
  852. }
  853. }
  854. }
  855. // 3. then fill up rst
  856. for (let i = 0; i < rows.length; i++) {
  857. rst.push('<row r="' + (i + 1 + rowOffset) + '" spans="1:' + spanX + '" ht="' + rows[i].height + '" customHeight="1">');
  858. for (let j = 0; j < rows[i].items.length; j++) {
  859. const cellObj = rows[i].items[j];
  860. if (cellObj.v === undefined) {
  861. rst.push('<c r="' + cellObj.r + '" s="' + cellObj.s + '"/>');
  862. } else {
  863. rst.push('<c r="' + cellObj.r + '" s="' + cellObj.s + '" t="s">');
  864. rst.push('<v>' + cellObj.v + '</v>');
  865. rst.push('</c>');
  866. }
  867. }
  868. rst.push('</row>');
  869. }
  870. // 4. maybe need to dispose the memory
  871. // ...
  872. };
  873. if (sheetData) {
  874. // current sheet data
  875. currentPageMergePos = sheetData[JV.PAGE_SPECIAL_MERGE_POS];
  876. currentMergeBorder = sheetData[JV.PROP_PAGE_MERGE_BORDER];
  877. self_setDataEx(sheetData, yPos, 0);
  878. } else {
  879. // total data in one sheet
  880. let rowOffset = 0;
  881. for (let i = 0; i < pageData.items.length; i++) {
  882. const shtItemData = pageData.items[i];
  883. currentPageMergePos = shtItemData[JV.PAGE_SPECIAL_MERGE_POS];
  884. currentMergeBorder = shtItemData[JV.PROP_PAGE_MERGE_BORDER];
  885. const tmpPos = yMultiPos[i];
  886. cellIdx = 0;
  887. self_setDataEx(shtItemData, tmpPos, rowOffset);
  888. rowOffset += tmpPos.length - 2;
  889. }
  890. }
  891. rst.push('</sheetData>');
  892. };
  893. preAnalyzePos(pageData, sheetData, xPos, yPos, yMultiPos);
  894. // console.log('yMultiPos');
  895. // console.log(yMultiPos);
  896. rst.push(dftHeadXml + '\r\n');
  897. rst.push('<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  898. const colStr = private_getCellIdxStr(xPos.length - 3);
  899. rst.push('<dimension ref="A1:' + colStr + '' + yPos.length + '"/>');
  900. rst.push('<sheetViews><sheetView tabSelected="1" workbookViewId="0">');
  901. // rst.push('<selection sqref="A1:' + colStr + '1"/>');
  902. rst.push('<selection sqref="A1:A1"/>');
  903. rst.push('</sheetView></sheetViews>');
  904. rst.push('<sheetFormatPr defaultRowHeight="13.5"/>');
  905. private_setCols();
  906. private_setSheetData();
  907. private_setMergedCells();
  908. rst.push('<phoneticPr fontId="1" type="noConversion"/>');
  909. // rst.push('<pageMargins left="0.315" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
  910. rst.push('<pageMargins left="' + (parseFloat(pageData[JV.NODE_PAGE_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]) * 0.39375) +
  911. '" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
  912. const paperSizeIdx = JV.PAGES_SIZE_STR.indexOf(paperSize);
  913. let pStr = '';
  914. if (paperSizeIdx >= 0) {
  915. pStr = 'paperSize="' + JV.PAGES_SIZE_IDX[paperSizeIdx] + '"';
  916. }
  917. let orientationStr = (pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][0] > pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1]) ? 'landscape' : 'portrait';
  918. if (currentPageMergePos) {
  919. orientationStr = (currentPageMergePos[JV.NODE_PAGE_SIZE][0] > currentPageMergePos[JV.NODE_PAGE_SIZE][1]) ? 'landscape' : 'portrait';
  920. }
  921. rst.push('<pageSetup ' + pStr + ' fitToWidth="0" fitToHeight="0" orientation="' + orientationStr + '" />');
  922. rst.push('<headerFooter alignWithMargins="0"/>');
  923. if (hasSignature && signSheetIdxArr[sheetIdx]) {
  924. // let rIdx = 1;
  925. // for (let ssIdx = 0; ssIdx < signSheetIdxArr.length; ssIdx++) {
  926. // if (signSheetIdxArr[ssIdx]) {
  927. // if (ssIdx < sheetIdx) rIdx++
  928. // else break;
  929. // }
  930. // }
  931. rst.push('<drawing r:id="rId1"/>');
  932. }
  933. rst.push('</worksheet>');
  934. return rst;
  935. }
  936. function writeWorkSheetRels(signSheetIdx) {
  937. const rst = [];
  938. rst.push('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
  939. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  940. rst.push('<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing" Target="../drawings/drawing' + signSheetIdx + '.xml"/>');
  941. rst.push('</Relationships>');
  942. return rst;
  943. }
  944. function writeImage(path, pic, baseDir) {
  945. let rst = null;
  946. if (pic) {
  947. // 这里pic的数据就是base64的,不能是其他
  948. rst = base64ToBuffer(pic);
  949. // fs.createReadStream
  950. } else if (path) {
  951. const filePath = baseDir + '/app' + path;
  952. // console.log(filePath);
  953. rst = fs.readFileSync(filePath);
  954. // console.log(rst);
  955. }
  956. return rst;
  957. }
  958. function writeDrawings(pageData, signKeyArr, signPathArr, isSinglePage, signSheetIdxArr) {
  959. const rst = [];
  960. // console.log('isSinglePage: ' + isSinglePage);
  961. if (isSinglePage) {
  962. rst.push(writeDrawing(pageData, null, signKeyArr[0]));
  963. } else {
  964. for (let i = 0; i < pageData.items.length; i++) {
  965. if (signSheetIdxArr[i]) {
  966. rst.push(writeDrawing(pageData, pageData.items[i], signKeyArr[i]));
  967. }
  968. }
  969. }
  970. return rst;
  971. }
  972. function writeDrawing(pageData, sheetData, subSignKeyArr) {
  973. const rst = [];
  974. const xPos = [];
  975. const yPos = [];
  976. const yMultiPos = [];
  977. const private_setSheetDrawingCellData = function(signCell, theYPos, startPicIdx, offsetRow) {
  978. let cellControl;
  979. if (typeof signCell[JV.PROP_CONTROL] === 'string') {
  980. cellControl = pageData[JV.NODE_CONTROL_COLLECTION][signCell[JV.PROP_CONTROL]];
  981. } else {
  982. cellControl = signCell[JV.PROP_CONTROL];
  983. }
  984. const area = getProperSignatureArea(signCell, cellControl);
  985. // 1. from
  986. // let colIdx = xPos.indexOf(signCell[JV.PROP_AREA][JV.PROP_LEFT]);
  987. // let rowIdx = theYPos.indexOf(signCell[JV.PROP_AREA][JV.PROP_TOP]) + offsetRow; // 这里要考虑Row的偏移量(多页在同同一个sheet输出)
  988. let colIdx = xPos.indexOf(area[0]);
  989. let rowIdx = theYPos.indexOf(area[1]) + offsetRow; // 这里要考虑Row的偏移量(多页在同同一个sheet输出)
  990. // 因为 xPos, theYPos的首位是0,所以还需要减1
  991. colIdx--;
  992. rowIdx--;
  993. rst.push('<xdr:twoCellAnchor editAs="oneCell">');
  994. rst.push('<xdr:from>');
  995. rst.push('<xdr:col>' + colIdx + '</xdr:col>');
  996. rst.push('<xdr:colOff>0</xdr:colOff>');
  997. rst.push('<xdr:row>' + rowIdx + '</xdr:row>');
  998. rst.push('<xdr:rowOff>0</xdr:rowOff>');
  999. rst.push('</xdr:from>');
  1000. // 2. to
  1001. colIdx = xPos.indexOf(area[2]);
  1002. rowIdx = theYPos.indexOf(area[3]) + offsetRow;
  1003. // 因为 xPos, theYPos的首位是0,所以还需要减1
  1004. colIdx--;
  1005. rowIdx--;
  1006. rst.push('<xdr:to>');
  1007. rst.push('<xdr:col>' + colIdx + '</xdr:col>');
  1008. rst.push('<xdr:colOff>0</xdr:colOff>');
  1009. rst.push('<xdr:row>' + rowIdx + '</xdr:row>');
  1010. rst.push('<xdr:rowOff>0</xdr:rowOff>');
  1011. rst.push('</xdr:to>');
  1012. // 3. pic
  1013. rst.push('<xdr:pic>');
  1014. // 3.1
  1015. rst.push('<xdr:nvPicPr>');
  1016. rst.push('<xdr:cNvPr id="' + startPicIdx + '" name="图片 ' + startPicIdx + '"/>');
  1017. rst.push('<xdr:cNvPicPr><a:picLocks noChangeAspect="1" noChangeArrowheads="1"/></xdr:cNvPicPr>');
  1018. rst.push('</xdr:nvPicPr>');
  1019. // 3.2
  1020. rst.push('<xdr:blipFill>');
  1021. // 重点!!!
  1022. const picIdx = subSignKeyArr.indexOf(signCell.signature_name) + 1;
  1023. // 3.2.1
  1024. rst.push('<a:blip xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:embed="rId' + picIdx + '" cstate="print">');
  1025. rst.push('<a:extLst>');
  1026. rst.push('<a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">'); // 参考excel本身,都是固定的uri
  1027. rst.push('<a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0"/>');
  1028. rst.push('</a:ext>');
  1029. rst.push('</a:extLst>');
  1030. rst.push('</a:blip>');
  1031. // 3.2.2
  1032. rst.push('<a:srcRect/>');
  1033. // 3.2.3
  1034. rst.push('<a:stretch><a:fillRect/></a:stretch>');
  1035. rst.push('</xdr:blipFill>');
  1036. // 3.3
  1037. rst.push('<xdr:spPr bwMode="auto">');
  1038. // 3.3.1
  1039. rst.push('<a:xfrm>');
  1040. // 以下经过Excel及WPS的测试,这数据好像没什么意义,都设为0
  1041. rst.push('<a:off x="0" y="0"/>');
  1042. rst.push('<a:ext cx="0" cy="0"/>');
  1043. rst.push('</a:xfrm>');
  1044. // 3.3.2
  1045. rst.push('<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>');
  1046. // 3.3.3
  1047. rst.push('<a:noFill/>');
  1048. // 3.3.4
  1049. rst.push('<a:extLst>');
  1050. rst.push('<a:ext uri="{909E8E84-426E-40DD-AFC4-6F175D3DCCD1}">');
  1051. rst.push('<a14:hiddenFill xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main">');
  1052. rst.push('<a:solidFill><a:srgbClr val="FFFFFF"/></a:solidFill>');
  1053. rst.push('</a14:hiddenFill>');
  1054. rst.push('</a:ext>');
  1055. rst.push('</a:extLst>');
  1056. // 收尾
  1057. rst.push('</xdr:spPr>');
  1058. rst.push('</xdr:pic>');
  1059. // 4. client Data
  1060. rst.push('<xdr:clientData/>');
  1061. rst.push('</xdr:twoCellAnchor>');
  1062. };
  1063. preAnalyzePos(pageData, sheetData, xPos, yPos, yMultiPos);
  1064. rst.push('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
  1065. rst.push('<xdr:wsDr xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">');
  1066. if (sheetData) {
  1067. let startPicIdx = 2;
  1068. for (const sCell of sheetData[JV.PROP_SIGNATURE_CELLS]) {
  1069. if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1070. private_setSheetDrawingCellData(sCell, yPos, startPicIdx, 0);
  1071. startPicIdx++;
  1072. }
  1073. }
  1074. } else {
  1075. // total data in one sheet
  1076. let rowOffset = 0;
  1077. for (let i = 0; i < pageData.items.length; i++) {
  1078. const shtItemData = pageData.items[i];
  1079. const tmpPos = yMultiPos[i];
  1080. let startPicIdx = 2;
  1081. for (const sCell of shtItemData[JV.PROP_SIGNATURE_CELLS]) {
  1082. if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1083. private_setSheetDrawingCellData(sCell, tmpPos, startPicIdx, rowOffset);
  1084. startPicIdx++;
  1085. }
  1086. }
  1087. rowOffset += tmpPos.length - 2;
  1088. }
  1089. }
  1090. rst.push('</xdr:wsDr>');
  1091. return rst;
  1092. }
  1093. function writeDrawingsRels(amt, startIdx) {
  1094. const rst = [];
  1095. rst.push('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
  1096. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  1097. for (let idx = 0; idx < amt; idx++) {
  1098. rst.push('<Relationship Id="rId' + (idx + 1) + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/image' + (startIdx + idx + 1) + '.png"/>');
  1099. }
  1100. rst.push('</Relationships>');
  1101. return rst;
  1102. }
  1103. function mergeProperties(orgObj, newObj) {
  1104. const orgPropArr = [];
  1105. const newPropArr = [];
  1106. for (const p in orgObj) {
  1107. orgPropArr.push(p);
  1108. }
  1109. for (const p in newObj) {
  1110. newPropArr.push(p);
  1111. }
  1112. for (let i = 0; i < newPropArr.length; i++) {
  1113. if (orgPropArr.indexOf(newPropArr[i]) < 0) {
  1114. orgObj[newPropArr[i]] = newObj[newPropArr[i]];
  1115. }
  1116. }
  1117. }
  1118. function resetDummuySignature(pageData, roleRel) {
  1119. // 备注:计算草图等其他图形需要额外做些处理
  1120. let dummySignIdx = 0;
  1121. for (let pageIdx = 0; pageIdx < pageData.items.length; pageIdx++) {
  1122. const page = pageData.items[pageIdx];
  1123. if (page[JV.PROP_SIGNATURE_CELLS] && page[JV.PROP_SIGNATURE_CELLS].length > 0) {
  1124. for (const signature of page[JV.PROP_SIGNATURE_CELLS]) {
  1125. if (signature.signature_name.indexOf(JV.SIGNATURE_NAME_DUMMY) >= 0) {
  1126. // 表示这是一个其他类型的非原生电子签名图,只是借用signature的处理机制,每个图都是唯一的,所以需要重新给个唯一的新signature_name
  1127. signature.signature_name = signature.signature_name + '_' + pageIdx + '_' + dummySignIdx;
  1128. dummySignIdx++;
  1129. const roleRelItem = { type: '用户', sign_path: signature.path, signature_name: signature.signature_name };
  1130. roleRel.push(roleRelItem);
  1131. }
  1132. }
  1133. }
  1134. }
  1135. }
  1136. function _createDummyCell(dummyOrgCells, parentPage) {
  1137. for (const signature of dummyOrgCells) {
  1138. const dummyCell = {
  1139. font: 'Content',
  1140. control: 'Default',
  1141. style: 'Default_None',
  1142. Value: '',
  1143. area: { Left: 0, Right: 0, Top: 0, Bottom: 0 }
  1144. };
  1145. if (signature.style) dummyCell.style = signature.style;
  1146. dummyCell[JV.PROP_AREA][JV.PROP_LEFT] = signature[JV.PROP_AREA][JV.PROP_LEFT];
  1147. dummyCell[JV.PROP_AREA][JV.PROP_RIGHT] = signature[JV.PROP_AREA][JV.PROP_RIGHT];
  1148. dummyCell[JV.PROP_AREA][JV.PROP_TOP] = signature[JV.PROP_AREA][JV.PROP_TOP];
  1149. dummyCell[JV.PROP_AREA][JV.PROP_BOTTOM] = signature[JV.PROP_AREA][JV.PROP_BOTTOM];
  1150. parentPage[JV.PROP_CELLS].push(dummyCell);
  1151. }
  1152. }
  1153. function createDummySignatureCell(pageData) {
  1154. for (let pageIdx = 0; pageIdx < pageData.items.length; pageIdx++) {
  1155. const page = pageData.items[pageIdx];
  1156. if (page[JV.PROP_SIGNATURE_CELLS] && page[JV.PROP_SIGNATURE_CELLS].length > 0) {
  1157. _createDummyCell(page[JV.PROP_SIGNATURE_CELLS], page);
  1158. // for (const signature of page[JV.PROP_SIGNATURE_CELLS]) {
  1159. // const dummyCell = {
  1160. // font: 'Content',
  1161. // control: 'Default',
  1162. // style: 'Default_None',
  1163. // Value: '',
  1164. // area: { Left: 0, Right: 0, Top: 0, Bottom: 0 }
  1165. // };
  1166. // if (signature.style) dummyCell.style = signature.style;
  1167. // dummyCell[JV.PROP_AREA][JV.PROP_LEFT] = signature[JV.PROP_AREA][JV.PROP_LEFT];
  1168. // dummyCell[JV.PROP_AREA][JV.PROP_RIGHT] = signature[JV.PROP_AREA][JV.PROP_RIGHT];
  1169. // dummyCell[JV.PROP_AREA][JV.PROP_TOP] = signature[JV.PROP_AREA][JV.PROP_TOP];
  1170. // dummyCell[JV.PROP_AREA][JV.PROP_BOTTOM] = signature[JV.PROP_AREA][JV.PROP_BOTTOM];
  1171. // page[JV.PROP_CELLS].push(dummyCell);
  1172. // }
  1173. }
  1174. if (page[JV.PROP_SIGNATURE_DATE_CELLS] && page[JV.PROP_SIGNATURE_DATE_CELLS].length > 0) {
  1175. _createDummyCell(page[JV.PROP_SIGNATURE_DATE_CELLS], page);
  1176. }
  1177. }
  1178. }
  1179. function checkAndSetSignatureCache(pageData, signKeyArr, signPathArr, roleRel, signSheetIdxArr) {
  1180. // 备注:电子签名是以图形的方式处理,一页可以有多个签名,多页的签名基本是引用同样的图片,在这里先处理一下,后期统一引用。
  1181. // 另:以后的图片(在电子签名(signature_cells)以外的图片)会单独处理(如计算草图等)
  1182. let rst = false;
  1183. let chkRoles = [];
  1184. if (roleRel instanceof Array) {
  1185. chkRoles = roleRel;
  1186. } else {
  1187. if (roleRel) chkRoles.push(roleRel);
  1188. }
  1189. // console.log(chkRoles);
  1190. // console.log(signKeyArr);
  1191. // for (const page of pageData.items) {
  1192. for (let pageIdx = 0; pageIdx < pageData.items.length; pageIdx++) {
  1193. const page = pageData.items[pageIdx];
  1194. signKeyArr.push([]);
  1195. signPathArr.push([]);
  1196. signSheetIdxArr[pageIdx] = false;
  1197. // console.log('page index: ' + pageIdx);
  1198. // console.log(page[JV.PROP_SIGNATURE_CELLS]);
  1199. if (page[JV.PROP_SIGNATURE_CELLS] && page[JV.PROP_SIGNATURE_CELLS].length > 0) {
  1200. // let dummySignIdx = 0;
  1201. for (const signature of page[JV.PROP_SIGNATURE_CELLS]) {
  1202. // if (signature.signature_name.indexOf(JV.SIGNATURE_NAME_DUMMY) >= 0) {
  1203. // // 表示这是一个其他类型的非原生电子签名图,只是借用signature的处理机制,每个图都是唯一的,所以需要重新给个唯一的新signature_name
  1204. // signature.signature_name = signature.signature_name + '_' + pageIdx + '_' + dummySignIdx;
  1205. // dummySignIdx++;
  1206. // rst = true;
  1207. // signSheetIdxArr[pageIdx] = true;
  1208. // }
  1209. if (signKeyArr[pageIdx].indexOf(signature.signature_name) < 0) {
  1210. if (signature.pic) {
  1211. const signPath = { path: null, pic: null };
  1212. signPathArr[pageIdx].push(signPath);
  1213. signPath.pic = signature.pic; // 历史报表
  1214. signKeyArr[pageIdx].push(signature.signature_name);
  1215. rst = true;
  1216. signSheetIdxArr[pageIdx] = true;
  1217. } else {
  1218. for (const role of chkRoles) {
  1219. if (signature.signature_name === role.signature_name) {
  1220. // console.log('signature.signature_name: ' + signature.signature_name);
  1221. if (role.sign_pic) {
  1222. const signPath = { path: null, pic: null };
  1223. signPathArr[pageIdx].push(signPath);
  1224. signPath.pic = role.sign_pic;
  1225. signKeyArr[pageIdx].push(signature.signature_name);
  1226. rst = true;
  1227. signSheetIdxArr[pageIdx] = true;
  1228. } else if (role.sign_path) {
  1229. const signPath = { path: null, pic: null };
  1230. signPathArr[pageIdx].push(signPath);
  1231. signPath.path = role.sign_path;
  1232. signKeyArr[pageIdx].push(signature.signature_name);
  1233. rst = true;
  1234. signSheetIdxArr[pageIdx] = true;
  1235. }
  1236. break;
  1237. }
  1238. }
  1239. }
  1240. }
  1241. }
  1242. }
  1243. }
  1244. return rst;
  1245. }
  1246. function base64ToBuffer(dataurl) {
  1247. // 将base64转换为buffer
  1248. const arr = dataurl.split(',');
  1249. // console.log(arr[1]);
  1250. const rst = new Buffer(arr[1], 'base64');
  1251. return rst;
  1252. }
  1253. module.exports = {
  1254. exportExcel: function(pageData, paperSize, fName, options, custSheetNames, custSheetMergeBands, baseDir, roleRel, callback) {
  1255. const rptOptions = ({ singlePage: false, fileName: 'report' });
  1256. if (options === 'true' || options === true) {
  1257. rptOptions.singlePage = true;
  1258. }
  1259. const isSinglePage = rptOptions.singlePage;
  1260. const sheets = [];
  1261. const signKeyArr = [];
  1262. const signPathArr = [];
  1263. const signSheetIdxArr = []; // 确定哪些sheet有签名(在多表导出的时候,有些表可能没有签名,但其他的表有签名)
  1264. // console.log('in exportExcel!');
  1265. // console.log(roleRel);
  1266. // const hasSignature = (roleRel !== null) ? checkAndSetSignatureCache(pageData, signKeyArr, signPathArr, roleRel, signSheetIdxArr) : false;
  1267. let thisRoleRel = [];
  1268. if (roleRel !== null && roleRel !== undefined) {
  1269. thisRoleRel = roleRel;
  1270. }
  1271. resetDummuySignature(pageData, thisRoleRel); // 把草图转换一下roleRel
  1272. // console.log(thisRoleRel);
  1273. const hasSignature = false; // 暂时不支持电子签名、草图导出excel
  1274. // const hasSignature = checkAndSetSignatureCache(pageData, signKeyArr, signPathArr, thisRoleRel, signSheetIdxArr); // 因草图的关系,thisRoleRel是否为null就不是充要的条件
  1275. // console.log('signKeyArr');
  1276. // console.log(signKeyArr);
  1277. // console.log('signPathArr');
  1278. // console.log(signPathArr);
  1279. // console.log('hasSignature: ' + hasSignature);
  1280. if (!hasSignature) {
  1281. createDummySignatureCell(pageData);
  1282. }
  1283. if (isSinglePage) {
  1284. sheets.push({ sheetName: '全部页' });
  1285. } else {
  1286. if (custSheetNames && custSheetNames.length === pageData.items.length) {
  1287. for (let i = 0; i < pageData.items.length; i++) {
  1288. sheets.push({ sheetName: custSheetNames[i] });
  1289. }
  1290. } else {
  1291. for (let i = 0; i < pageData.items.length; i++) {
  1292. sheets.push({ sheetName: '第' + (i + 1) + '页' });
  1293. }
  1294. }
  1295. }
  1296. // 1.
  1297. let file = '[Content_Types].xml';
  1298. let data = writeContentTypes(sheets, isSinglePage, hasSignature);
  1299. const zip = new JSZip();
  1300. zip.file(file, data.join(''), { compression: 'DEFLATE' });
  1301. // console.log('step 1');
  1302. // 2.
  1303. const zip_rels = zip.folder('_rels');
  1304. file = '.rels';
  1305. data = writeRootRels();
  1306. zip_rels.file(file, data.join(''), { compression: 'DEFLATE' });
  1307. // console.log('step 2');
  1308. // 3.
  1309. const zip_docProps = zip.folder('docProps');
  1310. file = 'app.xml';
  1311. data = writeApp(sheets, isSinglePage);
  1312. zip_docProps.file(file, data.join(''), { compression: 'DEFLATE' });
  1313. file = 'core.xml';
  1314. data = writeCore();
  1315. zip_docProps.file(file, data.join(''), { compression: 'DEFLATE' });
  1316. // console.log('step 3');
  1317. // 4.
  1318. const zip_xl = zip.folder('xl');
  1319. file = 'workbook.xml';
  1320. data = writeXlWorkBook(sheets, isSinglePage);
  1321. zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
  1322. const zip_rels2 = zip_xl.folder('_rels');
  1323. file = 'workbook.xml.rels';
  1324. data = writeXlRels(sheets, isSinglePage);
  1325. zip_rels2.file(file, data.join(''), { compression: 'DEFLATE' });
  1326. // console.log('step 4');
  1327. // 5.
  1328. const zip_theme = zip_xl.folder('theme');
  1329. file = 'theme1.xml';
  1330. data = writeTheme();
  1331. zip_theme.file(file, data, { compression: 'DEFLATE' });
  1332. if (hasSignature) {
  1333. // 5.1 写所有的image
  1334. const zip_media = zip_xl.folder('media');
  1335. let picIdx = 0;
  1336. // console.log(signKeyArr);
  1337. // console.log(signPathArr);
  1338. for (let signIdx = 0; signIdx < signPathArr.length; signIdx++) {
  1339. if (signKeyArr[signIdx].length > 0) {
  1340. for (let pathIdx = 0; pathIdx < signPathArr[signIdx].length; pathIdx++) {
  1341. picIdx++;
  1342. data = writeImage(signPathArr[signIdx][pathIdx].path, signPathArr[signIdx][pathIdx].pic, baseDir);
  1343. file = 'image' + picIdx + '.png';
  1344. zip_media.file(file, data, { compression: 'DEFLATE' });
  1345. }
  1346. }
  1347. }
  1348. // 5.2
  1349. const zip_drawings = zip_xl.folder('drawings');
  1350. data = writeDrawings(pageData, signKeyArr, signPathArr, isSinglePage, signSheetIdxArr);
  1351. // console.log('isSinglePage: ' + isSinglePage);
  1352. // console.log(data);
  1353. for (let psIdx = 0; psIdx < data.length; psIdx++) {
  1354. file = 'drawing' + (psIdx + 1) + '.xml';
  1355. // console.log('drawing1' + (psIdx + 1) + '.xml data');
  1356. // console.log(data);
  1357. zip_drawings.file(file, data[psIdx].join(''), { compression: 'DEFLATE' });
  1358. }
  1359. // 5.3
  1360. const zip_drawings_rels = zip_drawings.folder('_rels');
  1361. let relsIdx = 0;
  1362. let relsImgAmt = 0;
  1363. for (let ssIdx = 0; ssIdx < signKeyArr.length; ssIdx++) {
  1364. if (signKeyArr[ssIdx].length > 0) {
  1365. data = writeDrawingsRels(signKeyArr[ssIdx].length, relsImgAmt); // 一个drawingX.xml.rels文件与一个drawingX.xml对应
  1366. relsIdx++;
  1367. relsImgAmt += signKeyArr[ssIdx].length;
  1368. // console.log('drawing1.xml.rels data');
  1369. // console.log(data);
  1370. file = 'drawing' + relsIdx + '.xml.rels';
  1371. zip_drawings_rels.file(file, data.join(''), { compression: 'DEFLATE' });
  1372. }
  1373. }
  1374. }
  1375. // console.log('step 5');
  1376. // 6.
  1377. const zip_xl_worksheets = zip_xl.folder('worksheets');
  1378. const sharedStrList = [];
  1379. const stylesObj = {};
  1380. data = writeSheets(pageData, paperSize, sharedStrList, stylesObj, isSinglePage, custSheetMergeBands, hasSignature, signSheetIdxArr);
  1381. if (isSinglePage) {
  1382. for (let i = 0; i < 1; i++) {
  1383. file = 'sheet' + (i + 1) + '.xml';
  1384. zip_xl_worksheets.file(file, data[i].join(''), { compression: 'DEFLATE' });
  1385. }
  1386. } else {
  1387. for (let i = 0; i < data.length; i++) {
  1388. file = 'sheet' + (i + 1) + '.xml';
  1389. zip_xl_worksheets.file(file, data[i].join(''), { compression: 'DEFLATE' });
  1390. }
  1391. }
  1392. // console.log('step 6');
  1393. // 7.
  1394. if (hasSignature) {
  1395. const zip_xl_worksheets_rels = zip_xl_worksheets.folder('_rels');
  1396. if (isSinglePage) {
  1397. data = writeWorkSheetRels(1);
  1398. file = 'sheet1.xml.rels';
  1399. zip_xl_worksheets_rels.file(file, data.join(''), { compression: 'DEFLATE' });
  1400. } else {
  1401. let ssIdx = 0;
  1402. for (let i = 0; i < data.length; i++) {
  1403. if (signSheetIdxArr[i]) {
  1404. ssIdx++;
  1405. const ssData = writeWorkSheetRels(ssIdx);
  1406. file = 'sheet' + (i + 1) + '.xml.rels';
  1407. zip_xl_worksheets_rels.file(file, ssData.join(''), { compression: 'DEFLATE' });
  1408. }
  1409. }
  1410. }
  1411. }
  1412. file = 'sharedStrings.xml';
  1413. data = writeSharedString(sharedStrList);
  1414. zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
  1415. file = 'styles.xml';
  1416. data = writeStyles(stylesObj, hasSignature);
  1417. zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
  1418. // console.log('step 7');
  1419. if (fName) {
  1420. // let newName = '' + (new Date()).valueOf();
  1421. const newName = uuidV1();
  1422. // console.log('current path: ' + __dirname);
  1423. // const filePath = this.app.baseDir + '/app/public/download/';
  1424. zip.generateNodeStream({ type: 'nodebuffer', streamFiles: true })
  1425. .pipe(fs.createWriteStream(__dirname.slice(0, __dirname.length - 13) + '/public/download/' + newName + '.xlsx'))
  1426. .on('finish', function() {
  1427. // JSZip generates a readable stream with a "end" event,
  1428. // but is piped here in a writable stream which emits a "finish" event.
  1429. console.log(newName + '.xlsx was written.');
  1430. if (callback) callback(null, newName);
  1431. });
  1432. } else {
  1433. // return zip.generateNodeStream({type:'nodebuffer',streamFiles:true});
  1434. return zip;
  1435. }
  1436. },
  1437. exportExcelInOneBook: function(pageDataArray, paperSize, fName, baseDir, roleRelArr, callback) {
  1438. const me = this;
  1439. const newPageData = {};
  1440. // 1. 重新编排一下数据,把一份报表的pageData合并到一起作为一个Sheet输出(需要重新调整数据纵向坐标),多份报表数据就形成多个Sheet
  1441. // -- 简单来说,就是重新包装数据
  1442. try {
  1443. // 1.1 newPageData外围属性
  1444. const newContrl = {};
  1445. const newFont = {};
  1446. const newStyle = {};
  1447. const custMergeBands = [];
  1448. for (let i = 0; i < pageDataArray.length; i++) {
  1449. mergeProperties(newContrl, pageDataArray[i][JV.NODE_CONTROL_COLLECTION]);
  1450. mergeProperties(newFont, pageDataArray[i][JV.NODE_FONT_COLLECTION]);
  1451. mergeProperties(newStyle, pageDataArray[i][JV.NODE_STYLE_COLLECTION]);
  1452. }
  1453. newPageData[JV.NODE_CONTROL_COLLECTION] = newContrl;
  1454. newPageData[JV.NODE_FONT_COLLECTION] = newFont;
  1455. newPageData[JV.NODE_STYLE_COLLECTION] = newStyle;
  1456. newPageData[JV.NODE_PAGE_INFO] = pageDataArray[0][JV.NODE_PAGE_INFO];
  1457. newPageData[JV.BAND_PROP_MERGE_BAND] = pageDataArray[0][JV.BAND_PROP_MERGE_BAND];
  1458. // 1.2 重新设置pageDataArray的各个cell的Top/Bottom坐标
  1459. const sheetNames = [];
  1460. const newPagePos = [];
  1461. for (let i = 0; i < pageDataArray.length; i++) {
  1462. let offsetY = 0;
  1463. const mergeBand = {};
  1464. custMergeBands.push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND]);
  1465. // 备注:不同的报表有可能有不同的边框,如封面表就是无边框的
  1466. mergeBand[JV.PROP_LEFT] = [];
  1467. mergeBand[JV.PROP_RIGHT] = [];
  1468. mergeBand[JV.PROP_TOP] = [];
  1469. mergeBand[JV.PROP_BOTTOM] = [];
  1470. newPagePos.push(mergeBand);
  1471. mergeBand[JV.PROP_LEFT].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_LEFT]);
  1472. mergeBand[JV.PROP_RIGHT].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_RIGHT]);
  1473. sheetNames.push(pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_MAIN_INFO_RPT_NAME]);
  1474. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1475. let maxY = 0;
  1476. let minY = 100000;
  1477. if (pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS]) {
  1478. let pos = pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_TOP][0] + offsetY;
  1479. mergeBand[JV.PROP_TOP].push(pos);
  1480. pos = pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_BOTTOM][0] + offsetY;
  1481. mergeBand[JV.PROP_BOTTOM].push(pos);
  1482. } else if (pageDataArray[i].items[j][JV.PROP_PAGE_MERGE_BORDER]) {
  1483. let pos = pageDataArray[i].items[j][JV.PROP_PAGE_MERGE_BORDER][JV.PROP_TOP] + offsetY;
  1484. mergeBand[JV.PROP_TOP].push(pos);
  1485. pos = pageDataArray[i].items[j][JV.PROP_PAGE_MERGE_BORDER][JV.PROP_BOTTOM] + offsetY;
  1486. mergeBand[JV.PROP_BOTTOM].push(pos);
  1487. } else {
  1488. mergeBand[JV.PROP_TOP].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_TOP] + offsetY);
  1489. mergeBand[JV.PROP_BOTTOM].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_BOTTOM] + offsetY);
  1490. }
  1491. // 1.2.1 重新设置普通cells的Top Bottom坐标
  1492. for (const cell of pageDataArray[i].items[j].cells) {
  1493. // for (let k = 0; k < pageDataArray[i].items[j].cells.length; k++) {
  1494. if (maxY < cell[JV.PROP_AREA][JV.PROP_BOTTOM]) {
  1495. maxY = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  1496. }
  1497. if (minY > cell[JV.PROP_AREA][JV.PROP_TOP]) {
  1498. minY = cell[JV.PROP_AREA][JV.PROP_TOP];
  1499. }
  1500. cell[JV.PROP_AREA][JV.PROP_BOTTOM] += offsetY;
  1501. cell[JV.PROP_AREA][JV.PROP_TOP] += offsetY;
  1502. }
  1503. // 1.2.2 重新设置电子签名cells的Top Bottom坐标
  1504. for (const cell of pageDataArray[i].items[j].signature_cells) {
  1505. if (maxY < cell[JV.PROP_AREA][JV.PROP_BOTTOM]) {
  1506. maxY = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  1507. }
  1508. if (minY > cell[JV.PROP_AREA][JV.PROP_TOP]) {
  1509. minY = cell[JV.PROP_AREA][JV.PROP_TOP];
  1510. }
  1511. // console.log('before');
  1512. // console.log(cell);
  1513. cell[JV.PROP_AREA][JV.PROP_BOTTOM] += offsetY;
  1514. cell[JV.PROP_AREA][JV.PROP_TOP] += offsetY;
  1515. // console.log('after');
  1516. // console.log(cell);
  1517. }
  1518. // 1.2.3 重新设置电子签名日期cells的Top Bottom坐标
  1519. for (const cell of pageDataArray[i].items[j].signature_date_cells) {
  1520. if (maxY < cell[JV.PROP_AREA][JV.PROP_BOTTOM]) {
  1521. maxY = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  1522. }
  1523. if (minY > cell[JV.PROP_AREA][JV.PROP_TOP]) {
  1524. minY = cell[JV.PROP_AREA][JV.PROP_TOP];
  1525. }
  1526. cell[JV.PROP_AREA][JV.PROP_BOTTOM] += offsetY;
  1527. cell[JV.PROP_AREA][JV.PROP_TOP] += offsetY;
  1528. }
  1529. const bottomGap = Math.round((pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1] - parseFloat(pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM]) / 2.54) * DPI) - maxY;
  1530. offsetY += (maxY - minY);
  1531. if (bottomGap > 10) {
  1532. offsetY += (bottomGap - 10);
  1533. }
  1534. }
  1535. }
  1536. // 2. newPageData的items属性
  1537. newPageData.items = [];
  1538. for (let i = 0; i < pageDataArray.length; i++) {
  1539. const pageItem = {};
  1540. pageItem[JV.PROP_PAGE_SEQ] = i + 1;
  1541. pageItem[JV.PROP_CELLS] = [];
  1542. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1543. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_CELLS].length; k++) {
  1544. pageItem[JV.PROP_CELLS].push(pageDataArray[i].items[j][JV.PROP_CELLS][k]);
  1545. }
  1546. }
  1547. pageItem[JV.PROP_SIGNATURE_CELLS] = [];
  1548. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1549. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_SIGNATURE_CELLS].length; k++) {
  1550. pageItem[JV.PROP_SIGNATURE_CELLS].push(pageDataArray[i].items[j][JV.PROP_SIGNATURE_CELLS][k]);
  1551. }
  1552. }
  1553. pageItem[JV.PROP_SIGNATURE_DATE_CELLS] = [];
  1554. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1555. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_SIGNATURE_DATE_CELLS].length; k++) {
  1556. pageItem[JV.PROP_SIGNATURE_DATE_CELLS].push(pageDataArray[i].items[j][JV.PROP_SIGNATURE_DATE_CELLS][k]);
  1557. }
  1558. }
  1559. newPagePos[i][JV.NODE_PAGE_SIZE] = pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE];
  1560. pageItem[JV.PAGE_SPECIAL_MERGE_POS] = newPagePos[i];
  1561. newPageData.items.push(pageItem);
  1562. }
  1563. // 3. everything is ok, then call me
  1564. // let roleRel = null; // 未来调用的时候,这个属性要从外面给!!!
  1565. // roleRelArr
  1566. me.exportExcel(newPageData, paperSize, fName, 'false', sheetNames, custMergeBands, baseDir, roleRelArr, callback);
  1567. // fsUtil.writeObjToFile(newPageData, 'D:/GitHome/ConstructionOperation/tmp/combinedHeader.js');
  1568. } catch (e) {
  1569. console.log(e);
  1570. }
  1571. },
  1572. };