rpt_excel_util.js 90 KB

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