rpt_excel_util.js 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  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));
  1084. } else {
  1085. // console.log('pageData! ');
  1086. for (let i = 0; i < pageData.items.length; i++) {
  1087. if (signSheetIdxArr[i]) {
  1088. // console.log('signKeyArr[' + i + ']');
  1089. // console.log(signKeyArr[i]);
  1090. rst.push(writeDrawing(pageData, pageData.items[i], signKeyArr));
  1091. }
  1092. }
  1093. }
  1094. // */
  1095. return rst;
  1096. }
  1097. function writeDrawing(pageData, sheetData, subSignKeyArr) {
  1098. const rst = [];
  1099. const xPos = [];
  1100. const yPos = [];
  1101. const yMultiPos = [];
  1102. const private_setSheetDrawingCellData = function(signCell, theYPos, startPicIdx, offsetRow) {
  1103. let cellControl;
  1104. if (typeof signCell[JV.PROP_CONTROL] === 'string') {
  1105. cellControl = pageData[JV.NODE_CONTROL_COLLECTION][signCell[JV.PROP_CONTROL]];
  1106. } else {
  1107. cellControl = signCell[JV.PROP_CONTROL];
  1108. }
  1109. const area = getProperSignatureArea(signCell, cellControl);
  1110. // 1. from
  1111. // let colIdx = xPos.indexOf(signCell[JV.PROP_AREA][JV.PROP_LEFT]);
  1112. // let rowIdx = theYPos.indexOf(signCell[JV.PROP_AREA][JV.PROP_TOP]) + offsetRow; // 这里要考虑Row的偏移量(多页在同同一个sheet输出)
  1113. let colIdx = xPos.indexOf(area[0]);
  1114. let rowIdx = theYPos.indexOf(area[1]) + offsetRow; // 这里要考虑Row的偏移量(多页在同同一个sheet输出)
  1115. // 因为 xPos, theYPos的首位是0,所以还需要减1
  1116. colIdx--;
  1117. rowIdx--;
  1118. rst.push('<xdr:twoCellAnchor editAs="oneCell">');
  1119. rst.push('<xdr:from>');
  1120. rst.push('<xdr:col>' + colIdx + '</xdr:col>');
  1121. rst.push('<xdr:colOff>0</xdr:colOff>');
  1122. rst.push('<xdr:row>' + rowIdx + '</xdr:row>');
  1123. rst.push('<xdr:rowOff>0</xdr:rowOff>');
  1124. rst.push('</xdr:from>');
  1125. // 2. to
  1126. colIdx = xPos.indexOf(area[2]);
  1127. rowIdx = theYPos.indexOf(area[3]) + offsetRow;
  1128. // 因为 xPos, theYPos的首位是0,所以还需要减1
  1129. colIdx--;
  1130. rowIdx--;
  1131. rst.push('<xdr:to>');
  1132. rst.push('<xdr:col>' + colIdx + '</xdr:col>');
  1133. rst.push('<xdr:colOff>0</xdr:colOff>');
  1134. rst.push('<xdr:row>' + rowIdx + '</xdr:row>');
  1135. rst.push('<xdr:rowOff>0</xdr:rowOff>');
  1136. rst.push('</xdr:to>');
  1137. // 3. pic
  1138. rst.push('<xdr:pic>');
  1139. // 3.1
  1140. rst.push('<xdr:nvPicPr>');
  1141. rst.push('<xdr:cNvPr id="' + startPicIdx + '" name="图片 ' + startPicIdx + '"/>');
  1142. rst.push('<xdr:cNvPicPr><a:picLocks noChangeAspect="1" noChangeArrowheads="1"/></xdr:cNvPicPr>');
  1143. rst.push('</xdr:nvPicPr>');
  1144. // 3.2
  1145. rst.push('<xdr:blipFill>');
  1146. // 重点!!!
  1147. const picIdx = subSignKeyArr.indexOf(signCell.signature_name) + 1;
  1148. // 3.2.1
  1149. rst.push('<a:blip xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:embed="rId' + picIdx + '" cstate="print">');
  1150. rst.push('<a:extLst>');
  1151. rst.push('<a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">'); // 参考excel本身,都是固定的uri
  1152. rst.push('<a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0"/>');
  1153. rst.push('</a:ext>');
  1154. rst.push('</a:extLst>');
  1155. rst.push('</a:blip>');
  1156. // 3.2.2
  1157. rst.push('<a:srcRect/>');
  1158. // 3.2.3
  1159. rst.push('<a:stretch><a:fillRect/></a:stretch>');
  1160. rst.push('</xdr:blipFill>');
  1161. // 3.3
  1162. rst.push('<xdr:spPr bwMode="auto">');
  1163. // 3.3.1
  1164. rst.push('<a:xfrm>');
  1165. // 以下经过Excel及WPS的测试,这数据好像没什么意义,都设为0
  1166. rst.push('<a:off x="0" y="0"/>');
  1167. rst.push('<a:ext cx="0" cy="0"/>');
  1168. rst.push('</a:xfrm>');
  1169. // 3.3.2
  1170. rst.push('<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>');
  1171. // 3.3.3
  1172. rst.push('<a:noFill/>');
  1173. // 3.3.4
  1174. rst.push('<a:extLst>');
  1175. rst.push('<a:ext uri="{909E8E84-426E-40DD-AFC4-6F175D3DCCD1}">');
  1176. rst.push('<a14:hiddenFill xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main">');
  1177. rst.push('<a:solidFill><a:srgbClr val="FFFFFF"/></a:solidFill>');
  1178. rst.push('</a14:hiddenFill>');
  1179. rst.push('</a:ext>');
  1180. rst.push('</a:extLst>');
  1181. // 收尾
  1182. rst.push('</xdr:spPr>');
  1183. rst.push('</xdr:pic>');
  1184. // 4. client Data
  1185. rst.push('<xdr:clientData/>');
  1186. rst.push('</xdr:twoCellAnchor>');
  1187. };
  1188. preAnalyzePos(pageData, sheetData, xPos, yPos, yMultiPos);
  1189. rst.push('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
  1190. rst.push('<xdr:wsDr xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">');
  1191. // console.log('subSignKeyArr');
  1192. // console.log(subSignKeyArr);
  1193. if (sheetData) {
  1194. // console.log('sheetData');
  1195. let startPicIdx = 2;
  1196. for (const sCell of sheetData[JV.PROP_SIGNATURE_CELLS]) {
  1197. if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1198. private_setSheetDrawingCellData(sCell, yPos, startPicIdx, 0);
  1199. startPicIdx++;
  1200. }
  1201. }
  1202. for (const sCell of sheetData[JV.PROP_WATERMARK_CELLS]) {
  1203. if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1204. private_setSheetDrawingCellData(sCell, yPos, startPicIdx, 0);
  1205. startPicIdx++;
  1206. }
  1207. }
  1208. } else {
  1209. // console.log('pageData.items[0][JV.PROP_SIGNATURE_CELLS]');
  1210. // console.log(pageData.items[1][JV.PROP_SIGNATURE_CELLS]);
  1211. // total data in one sheet
  1212. let rowOffset = 0;
  1213. for (let i = 0; i < pageData.items.length; i++) {
  1214. const shtItemData = pageData.items[i];
  1215. const tmpPos = yMultiPos[i];
  1216. let startPicIdx = 2;
  1217. for (const sCell of shtItemData[JV.PROP_SIGNATURE_CELLS]) {
  1218. if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1219. private_setSheetDrawingCellData(sCell, tmpPos, startPicIdx, rowOffset);
  1220. startPicIdx++;
  1221. // } else {
  1222. // console.log('index < 0 subSignKeyArr: ');
  1223. // console.log(subSignKeyArr);
  1224. // console.log('sCell.signature_name');
  1225. // console.log(sCell.signature_name);
  1226. }
  1227. }
  1228. for (const sCell of shtItemData[JV.PROP_WATERMARK_CELLS]) {
  1229. if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1230. private_setSheetDrawingCellData(sCell, tmpPos, startPicIdx, rowOffset);
  1231. startPicIdx++;
  1232. }
  1233. }
  1234. rowOffset += tmpPos.length - 2;
  1235. }
  1236. }
  1237. rst.push('</xdr:wsDr>');
  1238. return rst;
  1239. }
  1240. function writeDrawingsRels(amt, startIdx) {
  1241. const rst = [];
  1242. rst.push('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
  1243. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  1244. for (let idx = 0; idx < amt; idx++) {
  1245. rst.push('<Relationship Id="rId' + (idx + 1) + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/image' + (startIdx + idx + 1) + '.png"/>');
  1246. }
  1247. rst.push('</Relationships>');
  1248. return rst;
  1249. }
  1250. function mergeProperties(orgObj, newObj) {
  1251. const orgPropArr = [];
  1252. const newPropArr = [];
  1253. for (const p in orgObj) {
  1254. orgPropArr.push(p);
  1255. }
  1256. for (const p in newObj) {
  1257. newPropArr.push(p);
  1258. }
  1259. for (let i = 0; i < newPropArr.length; i++) {
  1260. if (orgPropArr.indexOf(newPropArr[i]) < 0) {
  1261. orgObj[newPropArr[i]] = newObj[newPropArr[i]];
  1262. }
  1263. }
  1264. }
  1265. function resetDummuySignature(pageData, roleRel) {
  1266. // 备注:计算草图等其他图形需要额外做些处理
  1267. let dummySignIdx = 0;
  1268. for (let pageIdx = 0; pageIdx < pageData.items.length; pageIdx++) {
  1269. const page = pageData.items[pageIdx];
  1270. if (page[JV.PROP_SIGNATURE_CELLS] && page[JV.PROP_SIGNATURE_CELLS].length > 0) {
  1271. for (const signature of page[JV.PROP_SIGNATURE_CELLS]) {
  1272. if (signature.signature_name.indexOf(JV.SIGNATURE_NAME_DUMMY) >= 0) {
  1273. // 表示这是一个其他类型的非原生电子签名图,只是借用signature的处理机制,每个图都是唯一的,所以需要重新给个唯一的新signature_name
  1274. signature.signature_name = signature.signature_name + '_' + pageIdx + '_' + dummySignIdx;
  1275. dummySignIdx++;
  1276. const roleRelItem = { type: '用户', sign_path: signature.path, signature_name: signature.signature_name };
  1277. roleRel.push(roleRelItem);
  1278. }
  1279. }
  1280. }
  1281. }
  1282. }
  1283. function _createDummyCell(dummyOrgCells, parentPage) {
  1284. for (const signature of dummyOrgCells) {
  1285. const dummyCell = {
  1286. font: 'Content',
  1287. control: 'Default',
  1288. style: 'Default_None',
  1289. Value: '',
  1290. area: { Left: 0, Right: 0, Top: 0, Bottom: 0 },
  1291. };
  1292. if (signature.style) dummyCell.style = signature.style;
  1293. dummyCell[JV.PROP_AREA][JV.PROP_LEFT] = signature[JV.PROP_AREA][JV.PROP_LEFT];
  1294. dummyCell[JV.PROP_AREA][JV.PROP_RIGHT] = signature[JV.PROP_AREA][JV.PROP_RIGHT];
  1295. dummyCell[JV.PROP_AREA][JV.PROP_TOP] = signature[JV.PROP_AREA][JV.PROP_TOP];
  1296. dummyCell[JV.PROP_AREA][JV.PROP_BOTTOM] = signature[JV.PROP_AREA][JV.PROP_BOTTOM];
  1297. parentPage[JV.PROP_CELLS].push(dummyCell);
  1298. }
  1299. }
  1300. function createDummySignatureCell(pageData) {
  1301. for (let pageIdx = 0; pageIdx < pageData.items.length; pageIdx++) {
  1302. const page = pageData.items[pageIdx];
  1303. if (page[JV.PROP_SIGNATURE_CELLS] && page[JV.PROP_SIGNATURE_CELLS].length > 0) {
  1304. _createDummyCell(page[JV.PROP_SIGNATURE_CELLS], page);
  1305. }
  1306. if (page[JV.PROP_SIGNATURE_DATE_CELLS] && page[JV.PROP_SIGNATURE_DATE_CELLS].length > 0) {
  1307. _createDummyCell(page[JV.PROP_SIGNATURE_DATE_CELLS], page);
  1308. }
  1309. if (page[JV.PROP_SIGNATURE_AUDIT_CELLS] && page[JV.PROP_SIGNATURE_AUDIT_CELLS].length > 0) {
  1310. _createDummyCell(page[JV.PROP_SIGNATURE_AUDIT_CELLS], page);
  1311. }
  1312. }
  1313. }
  1314. function _checkAndSetSignatureCache(pageData, signKeyArr, signPathArr, roleRel, signSheetIdxArr, isSinglePage) {
  1315. // 备注:电子签名是以图形的方式处理,一页可以有多个签名,多页的签名基本是引用同样的图片,在这里先处理一下,后期统一引用。
  1316. // 另:以后的图片(在电子签名(signature_cells)以外的图片)会单独处理(如计算草图、水印等)
  1317. let rst = false;
  1318. let chkRoles = [];
  1319. if (roleRel instanceof Array) {
  1320. chkRoles = roleRel;
  1321. } else {
  1322. if (roleRel) chkRoles.push(roleRel);
  1323. }
  1324. const _getDupPicPath = function(path) {
  1325. let rst = null;
  1326. for (const sp of signPathArr) {
  1327. if (sp.path === path) {
  1328. rst = sp;
  1329. break;
  1330. }
  1331. }
  1332. return rst;
  1333. };
  1334. // console.log(chkRoles);
  1335. // console.log(signKeyArr);
  1336. // for (const page of pageData.items) {
  1337. for (let pageIdx = 0; pageIdx < pageData.items.length; pageIdx++) {
  1338. const page = pageData.items[pageIdx];
  1339. // if (isSinglePage) {
  1340. // if (pageIdx === 0) {
  1341. // signKeyArr.push([]);
  1342. // signPathArr.push([]);
  1343. // }
  1344. // } else {
  1345. // signKeyArr.push([]);
  1346. // signPathArr.push([]);
  1347. // }
  1348. // 优化调整,不再每页都设置一个,重复的签名只需要一个即可(极端case: 13xx多页的签名文档,导出有1.3G大)
  1349. const curSignIdx = signKeyArr.length - 1;
  1350. signSheetIdxArr[pageIdx] = false;
  1351. // console.log('page index: ' + pageIdx);
  1352. // console.log(page[JV.PROP_SIGNATURE_CELLS]);
  1353. if (page[JV.PROP_SIGNATURE_CELLS] && page[JV.PROP_SIGNATURE_CELLS].length > 0) {
  1354. // let dummySignIdx = 0;
  1355. for (const signature of page[JV.PROP_SIGNATURE_CELLS]) {
  1356. // if (signature.signature_name.indexOf(JV.SIGNATURE_NAME_DUMMY) >= 0) {
  1357. // // 表示这是一个其他类型的非原生电子签名图,只是借用signature的处理机制,每个图都是唯一的,所以需要重新给个唯一的新signature_name
  1358. // signature.signature_name = signature.signature_name + '_' + pageIdx + '_' + dummySignIdx;
  1359. // dummySignIdx++;
  1360. // rst = true;
  1361. // signSheetIdxArr[pageIdx] = true;
  1362. // }
  1363. /*
  1364. if (signKeyArr[curSignIdx].indexOf(signature.signature_name) < 0) {
  1365. if (signature.pic) {
  1366. const signPath = { path: null, pic: null };
  1367. signPathArr[curSignIdx].push(signPath);
  1368. signPath.pic = signature.pic; // 历史报表
  1369. signKeyArr[curSignIdx].push(signature.signature_name);
  1370. rst = true;
  1371. signSheetIdxArr[pageIdx] = true;
  1372. } else {
  1373. for (const role of chkRoles) {
  1374. if (signature.signature_name === role.signature_name) {
  1375. // console.log('signature.signature_name: ' + signature.signature_name);
  1376. if (role.sign_pic) {
  1377. const signPath = { path: null, pic: null };
  1378. signPathArr[curSignIdx].push(signPath);
  1379. signPath.pic = role.sign_pic;
  1380. signKeyArr[curSignIdx].push(signature.signature_name);
  1381. rst = true;
  1382. signSheetIdxArr[pageIdx] = true;
  1383. } else if (role.sign_path) {
  1384. const signPath = { path: null, pic: null };
  1385. signPathArr[curSignIdx].push(signPath);
  1386. signPath.path = role.sign_path;
  1387. signKeyArr[curSignIdx].push(signature.signature_name);
  1388. rst = true;
  1389. signSheetIdxArr[pageIdx] = true;
  1390. }
  1391. break;
  1392. }
  1393. }
  1394. }
  1395. }
  1396. /*/
  1397. // let tgKey = signature.signature_name;
  1398. // for (const role of chkRoles) {
  1399. // if (signature.signature_name === role.signature_name) {
  1400. // if (role.sign_pic) {
  1401. // //
  1402. // } else if (role.sign_path) {
  1403. // tgKey = role.sign_path;
  1404. // }
  1405. // break;
  1406. // }
  1407. // }
  1408. // 这里把图片的路径作为key值判断
  1409. if (signKeyArr.indexOf(signature.signature_name) < 0) {
  1410. if (signature.pic) {
  1411. const signPath = { signature_name: signature.signature_name, path: null, pic: null };
  1412. signPathArr.push(signPath);
  1413. signPath.pic = signature.pic; // 历史报表
  1414. signKeyArr.push(signature.signature_name);
  1415. rst = true;
  1416. signSheetIdxArr[pageIdx] = true;
  1417. } else {
  1418. for (const role of chkRoles) {
  1419. if (signature.signature_name === role.signature_name) {
  1420. // console.log('signature.signature_name: ' + signature.signature_name);
  1421. if (role.sign_pic) {
  1422. const signPath = { signature_name: signature.signature_name, path: null, pic: null };
  1423. signPathArr.push(signPath);
  1424. signPath.pic = role.sign_pic;
  1425. signKeyArr.push(signature.signature_name);
  1426. rst = true;
  1427. signSheetIdxArr[pageIdx] = true;
  1428. } else if (role.sign_path) {
  1429. let sPObj = _getDupPicPath(role.sign_path);
  1430. if (sPObj !== null) {
  1431. signature.signature_name = sPObj.signature_name; //重点,如果有相同路径的,修改signature_name!
  1432. rst = true;
  1433. } else {
  1434. const signPath = { signature_name: signature.signature_name, path: null, pic: null };
  1435. signPathArr.push(signPath);
  1436. signPath.path = role.sign_path;
  1437. signKeyArr.push(signature.signature_name);
  1438. rst = true;
  1439. signSheetIdxArr[pageIdx] = true;
  1440. }
  1441. }
  1442. break;
  1443. }
  1444. }
  1445. }
  1446. }
  1447. //*/
  1448. }
  1449. }
  1450. if (page[JV.PROP_WATERMARK_CELLS] && page[JV.PROP_WATERMARK_CELLS].length > 0) {
  1451. // 水印,单独处理
  1452. const waterMarkCell = page[JV.PROP_WATERMARK_CELLS][0];
  1453. /*
  1454. if (signKeyArr[curSignIdx].indexOf(waterMarkCell.signature_name) < 0) {
  1455. // console.log('waterMarkCell');
  1456. // console.log(waterMarkCell);
  1457. const signPath = { path: null, pic: null };
  1458. signPathArr[curSignIdx].push(signPath);
  1459. signPath.pic = waterMarkCell.pic;
  1460. signPath.path = waterMarkCell.path;
  1461. signKeyArr[curSignIdx].push(waterMarkCell.signature_name);
  1462. rst = true;
  1463. signSheetIdxArr[pageIdx] = true;
  1464. }
  1465. /*/
  1466. if (signKeyArr.indexOf(waterMarkCell.signature_name) < 0) {
  1467. const signPath = { path: null, pic: null };
  1468. signPathArr.push(signPath);
  1469. signPath.pic = waterMarkCell.pic;
  1470. signPath.path = waterMarkCell.path;
  1471. signKeyArr.push(waterMarkCell.signature_name);
  1472. rst = true;
  1473. signSheetIdxArr[pageIdx] = true;
  1474. }
  1475. //*/
  1476. }
  1477. }
  1478. return rst;
  1479. }
  1480. function base64ToBuffer(dataurl) {
  1481. // 将base64转换为buffer
  1482. const arr = dataurl.split(',');
  1483. // console.log(arr[1]);
  1484. const rst = new Buffer(arr[1], 'base64');
  1485. return rst;
  1486. }
  1487. module.exports = {
  1488. exportExcel: function(pageData, paperSize, fName, options, custSheetNames, custSheetMergeBands, baseDir, roleRel, callback) {
  1489. const rptOptions = ({ singlePage: false, fileName: 'report' });
  1490. if (options === 'true' || options === true) {
  1491. rptOptions.singlePage = true;
  1492. }
  1493. const isSinglePage = rptOptions.singlePage;
  1494. const sheets = [];
  1495. const signKeyArr = [];
  1496. const signPathArr = [];
  1497. const signSheetIdxArr = []; // 确定哪些sheet有签名(在多表导出的时候,有些表可能没有签名,但其他的表有签名)
  1498. // console.log('in exportExcel!');
  1499. // console.log(roleRel);
  1500. // const hasSignature = (roleRel !== null) ? _checkAndSetSignatureCache(pageData, signKeyArr, signPathArr, roleRel, signSheetIdxArr) : false;
  1501. let thisRoleRel = [];
  1502. if (roleRel !== null && roleRel !== undefined) {
  1503. thisRoleRel = roleRel;
  1504. }
  1505. resetDummuySignature(pageData, thisRoleRel); // 把草图转换一下roleRel
  1506. // console.log(thisRoleRel);
  1507. // const hasSignature = false; // 暂时不支持电子签名、草图导出excel
  1508. const hasSignature = _checkAndSetSignatureCache(pageData, signKeyArr, signPathArr, thisRoleRel, signSheetIdxArr, isSinglePage); // 因草图的关系,thisRoleRel是否为null就不是充要的条件
  1509. // console.log('signKeyArr');
  1510. // console.log(signKeyArr);
  1511. // console.log('signPathArr');
  1512. // console.log(signPathArr);
  1513. // console.log('hasSignature: ' + hasSignature);
  1514. if (!hasSignature) {
  1515. createDummySignatureCell(pageData);
  1516. }
  1517. if (isSinglePage) {
  1518. // 加分页符(分页符要在外部处理好)-------------------
  1519. _setupPgBrks(pageData);
  1520. // 加分页符结束-------------------
  1521. sheets.push({ sheetName: '全部页' });
  1522. } else {
  1523. if (custSheetNames && custSheetNames.length === pageData.items.length) {
  1524. for (let i = 0; i < pageData.items.length; i++) {
  1525. sheets.push({ sheetName: custSheetNames[i] });
  1526. }
  1527. } else {
  1528. for (let i = 0; i < pageData.items.length; i++) {
  1529. sheets.push({ sheetName: '第' + (i + 1) + '页' });
  1530. }
  1531. }
  1532. }
  1533. // 1.
  1534. let file = '[Content_Types].xml';
  1535. let data = writeContentTypes(sheets, isSinglePage, hasSignature);
  1536. const zip = new JSZip();
  1537. zip.file(file, data.join(''), { compression: 'DEFLATE' });
  1538. // console.log('step 1');
  1539. // 2.
  1540. const zip_rels = zip.folder('_rels');
  1541. file = '.rels';
  1542. data = writeRootRels();
  1543. zip_rels.file(file, data.join(''), { compression: 'DEFLATE' });
  1544. // console.log('step 2');
  1545. // 3.
  1546. const zip_docProps = zip.folder('docProps');
  1547. file = 'app.xml';
  1548. data = writeApp(sheets, isSinglePage);
  1549. zip_docProps.file(file, data.join(''), { compression: 'DEFLATE' });
  1550. file = 'core.xml';
  1551. data = writeCore();
  1552. zip_docProps.file(file, data.join(''), { compression: 'DEFLATE' });
  1553. // console.log('step 3');
  1554. // 4.
  1555. const zip_xl = zip.folder('xl');
  1556. file = 'workbook.xml';
  1557. data = writeXlWorkBook(sheets, isSinglePage);
  1558. zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
  1559. const zip_rels2 = zip_xl.folder('_rels');
  1560. file = 'workbook.xml.rels';
  1561. data = writeXlRels(sheets, isSinglePage);
  1562. zip_rels2.file(file, data.join(''), { compression: 'DEFLATE' });
  1563. // console.log('step 4');
  1564. // 5.
  1565. const zip_theme = zip_xl.folder('theme');
  1566. file = 'theme1.xml';
  1567. data = writeTheme();
  1568. zip_theme.file(file, data, { compression: 'DEFLATE' });
  1569. if (hasSignature) {
  1570. // 5.1 写所有的image
  1571. const zip_media = zip_xl.folder('media');
  1572. let picIdx = 0;
  1573. // console.log(signKeyArr);
  1574. // console.log(signPathArr);
  1575. /*
  1576. for (let signIdx = 0; signIdx < signPathArr.length; signIdx++) {
  1577. if (signKeyArr[signIdx].length > 0) {
  1578. for (let pathIdx = 0; pathIdx < signPathArr[signIdx].length; pathIdx++) {
  1579. picIdx++;
  1580. data = writeImage(signPathArr[signIdx][pathIdx].path, signPathArr[signIdx][pathIdx].pic, baseDir);
  1581. file = 'image' + picIdx + '.png';
  1582. zip_media.file(file, data, { compression: 'DEFLATE' });
  1583. }
  1584. }
  1585. }
  1586. /*/
  1587. // 优化调整,不再每页都设置一个,重复的签名只需要一个即可(极端case: 13xx多页的签名文档,导出有1.3G大)
  1588. for (let pathIdx = 0; pathIdx < signPathArr.length; pathIdx++) {
  1589. picIdx++;
  1590. data = writeImage(signPathArr[pathIdx].path, signPathArr[pathIdx].pic, baseDir);
  1591. file = 'image' + picIdx + '.png';
  1592. zip_media.file(file, data, { compression: 'DEFLATE' });
  1593. }
  1594. //*/
  1595. // 5.2
  1596. const zip_drawings = zip_xl.folder('drawings');
  1597. data = writeDrawings(pageData, signKeyArr, signPathArr, isSinglePage, signSheetIdxArr);
  1598. // data = writeDrawings(pageData, signKeyArr, signPathArr, false, signSheetIdxArr); // 备注:这里根本不需要考虑是否single page
  1599. // console.log('isSinglePage: ' + isSinglePage);
  1600. // console.log(data);
  1601. for (let psIdx = 0; psIdx < data.length; psIdx++) {
  1602. file = 'drawing' + (psIdx + 1) + '.xml';
  1603. // console.log('drawing1' + (psIdx + 1) + '.xml data');
  1604. // console.log(data);
  1605. zip_drawings.file(file, data[psIdx].join(''), { compression: 'DEFLATE' });
  1606. }
  1607. // 5.3
  1608. const zip_drawings_rels = zip_drawings.folder('_rels');
  1609. let relsIdx = 0;
  1610. let relsImgAmt = 0;
  1611. for (let ssIdx = 0; ssIdx < signKeyArr.length; ssIdx++) {
  1612. /*
  1613. if (signKeyArr[ssIdx].length > 0) {
  1614. data = writeDrawingsRels(signKeyArr[ssIdx].length, relsImgAmt); // 一个drawingX.xml.rels文件与一个drawingX.xml对应
  1615. relsIdx++;
  1616. relsImgAmt += signKeyArr[ssIdx].length;
  1617. // console.log('drawing1.xml.rels data');
  1618. // console.log(data);
  1619. file = 'drawing' + relsIdx + '.xml.rels';
  1620. zip_drawings_rels.file(file, data.join(''), { compression: 'DEFLATE' });
  1621. }
  1622. /*/
  1623. data = writeDrawingsRels(signKeyArr.length, relsImgAmt); // 一个drawingX.xml.rels文件与一个drawingX.xml对应
  1624. relsIdx++;
  1625. relsImgAmt += signKeyArr.length;
  1626. file = 'drawing' + relsIdx + '.xml.rels';
  1627. zip_drawings_rels.file(file, data.join(''), { compression: 'DEFLATE' });
  1628. // */
  1629. }
  1630. }
  1631. // console.log('step 5');
  1632. // 6.
  1633. const zip_xl_worksheets = zip_xl.folder('worksheets');
  1634. const sharedStrList = [];
  1635. const stylesObj = {};
  1636. data = writeSheets(pageData, paperSize, sharedStrList, stylesObj, isSinglePage, custSheetMergeBands, hasSignature, signSheetIdxArr);
  1637. if (isSinglePage) {
  1638. for (let i = 0; i < 1; i++) {
  1639. file = 'sheet' + (i + 1) + '.xml';
  1640. zip_xl_worksheets.file(file, data[i].join(''), { compression: 'DEFLATE' });
  1641. }
  1642. } else {
  1643. for (let i = 0; i < data.length; i++) {
  1644. file = 'sheet' + (i + 1) + '.xml';
  1645. zip_xl_worksheets.file(file, data[i].join(''), { compression: 'DEFLATE' });
  1646. }
  1647. }
  1648. // console.log('step 6');
  1649. // 7.
  1650. if (hasSignature) {
  1651. const zip_xl_worksheets_rels = zip_xl_worksheets.folder('_rels');
  1652. if (isSinglePage) {
  1653. data = writeWorkSheetRels(1);
  1654. file = 'sheet1.xml.rels';
  1655. zip_xl_worksheets_rels.file(file, data.join(''), { compression: 'DEFLATE' });
  1656. } else {
  1657. let ssIdx = 0;
  1658. for (let i = 0; i < data.length; i++) {
  1659. if (signSheetIdxArr[i]) {
  1660. ssIdx++;
  1661. const ssData = writeWorkSheetRels(ssIdx);
  1662. file = 'sheet' + (i + 1) + '.xml.rels';
  1663. zip_xl_worksheets_rels.file(file, ssData.join(''), { compression: 'DEFLATE' });
  1664. }
  1665. }
  1666. }
  1667. }
  1668. file = 'sharedStrings.xml';
  1669. data = writeSharedString(sharedStrList);
  1670. zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
  1671. file = 'styles.xml';
  1672. data = writeStyles(stylesObj, hasSignature);
  1673. zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
  1674. // console.log('step 7');
  1675. if (fName) {
  1676. // let newName = '' + (new Date()).valueOf();
  1677. const newName = uuidV1();
  1678. // console.log('current path: ' + __dirname);
  1679. // const filePath = this.app.baseDir + '/app/public/download/';
  1680. zip.generateNodeStream({ type: 'nodebuffer', streamFiles: true })
  1681. .pipe(fs.createWriteStream(__dirname.slice(0, __dirname.length - 13) + '/public/download/' + newName + '.xlsx'))
  1682. .on('finish', function() {
  1683. // JSZip generates a readable stream with a "end" event,
  1684. // but is piped here in a writable stream which emits a "finish" event.
  1685. console.log(newName + '.xlsx was written.');
  1686. if (callback) callback(null, newName);
  1687. });
  1688. } else {
  1689. // return zip.generateNodeStream({type:'nodebuffer',streamFiles:true});
  1690. return zip;
  1691. }
  1692. },
  1693. exportExcelInOneBook: function(pageDataArray, paperSize, fName, baseDir, roleRelArr, callback) {
  1694. const me = this;
  1695. const newPageData = {};
  1696. // 1. 重新编排一下数据,把一份报表的pageData合并到一起作为一个Sheet输出(需要重新调整数据纵向坐标),多份报表数据就形成多个Sheet
  1697. // -- 简单来说,就是重新包装数据
  1698. try {
  1699. // 1.1 newPageData外围属性
  1700. const newContrl = {};
  1701. const newFont = {};
  1702. const newStyle = {};
  1703. const custMergeBands = [];
  1704. for (let i = 0; i < pageDataArray.length; i++) {
  1705. mergeProperties(newContrl, pageDataArray[i][JV.NODE_CONTROL_COLLECTION]);
  1706. mergeProperties(newFont, pageDataArray[i][JV.NODE_FONT_COLLECTION]);
  1707. mergeProperties(newStyle, pageDataArray[i][JV.NODE_STYLE_COLLECTION]);
  1708. }
  1709. newPageData[JV.NODE_CONTROL_COLLECTION] = newContrl;
  1710. newPageData[JV.NODE_FONT_COLLECTION] = newFont;
  1711. newPageData[JV.NODE_STYLE_COLLECTION] = newStyle;
  1712. newPageData[JV.NODE_PAGE_INFO] = pageDataArray[0][JV.NODE_PAGE_INFO];
  1713. newPageData[JV.BAND_PROP_MERGE_BAND] = pageDataArray[0][JV.BAND_PROP_MERGE_BAND];
  1714. // 1.2 重新设置pageDataArray的各个cell的Top/Bottom坐标
  1715. const sheetNames = [];
  1716. const newPagePos = [];
  1717. for (let i = 0; i < pageDataArray.length; i++) {
  1718. let offsetY = 0;
  1719. const mergeBand = {};
  1720. custMergeBands.push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND]);
  1721. // 备注:不同的报表有可能有不同的边框,如封面表就是无边框的
  1722. mergeBand[JV.PROP_LEFT] = [];
  1723. mergeBand[JV.PROP_RIGHT] = [];
  1724. mergeBand[JV.PROP_TOP] = [];
  1725. mergeBand[JV.PROP_BOTTOM] = [];
  1726. newPagePos.push(mergeBand);
  1727. mergeBand[JV.PROP_LEFT].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_LEFT]);
  1728. mergeBand[JV.PROP_RIGHT].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_RIGHT]);
  1729. sheetNames.push(pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_MAIN_INFO_RPT_NAME]);
  1730. // 加分页符(分页符要在外部处理好)-------------------
  1731. _setupPgBrks(pageDataArray[i]);
  1732. // 加分页符结束-------------------
  1733. let maxY = 0;
  1734. let minY = 100000;
  1735. const checkAndResetCellYPos = function(cells) {
  1736. if (cells && cells.length > 0) {
  1737. for (const cell of cells) {
  1738. if (maxY < cell[JV.PROP_AREA][JV.PROP_BOTTOM]) {
  1739. maxY = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  1740. }
  1741. if (minY > cell[JV.PROP_AREA][JV.PROP_TOP]) {
  1742. minY = cell[JV.PROP_AREA][JV.PROP_TOP];
  1743. }
  1744. cell[JV.PROP_AREA][JV.PROP_BOTTOM] += offsetY;
  1745. cell[JV.PROP_AREA][JV.PROP_TOP] += offsetY;
  1746. }
  1747. }
  1748. };
  1749. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1750. maxY = 0;
  1751. minY = 100000;
  1752. if (pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS]) {
  1753. let pos = pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_TOP][0] + offsetY;
  1754. mergeBand[JV.PROP_TOP].push(pos);
  1755. pos = pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_BOTTOM][0] + offsetY;
  1756. mergeBand[JV.PROP_BOTTOM].push(pos);
  1757. } else if (pageDataArray[i].items[j][JV.PROP_PAGE_MERGE_BORDER]) {
  1758. let pos = pageDataArray[i].items[j][JV.PROP_PAGE_MERGE_BORDER][JV.PROP_TOP] + offsetY;
  1759. mergeBand[JV.PROP_TOP].push(pos);
  1760. pos = pageDataArray[i].items[j][JV.PROP_PAGE_MERGE_BORDER][JV.PROP_BOTTOM] + offsetY;
  1761. mergeBand[JV.PROP_BOTTOM].push(pos);
  1762. } else {
  1763. mergeBand[JV.PROP_TOP].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_TOP] + offsetY);
  1764. mergeBand[JV.PROP_BOTTOM].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_BOTTOM] + offsetY);
  1765. }
  1766. // 1.2.1 重新设置普通cells的Top Bottom坐标
  1767. checkAndResetCellYPos(pageDataArray[i].items[j][JV.PROP_CELLS]);
  1768. // 1.2.2 重新设置电子签名cells的Top Bottom坐标
  1769. checkAndResetCellYPos(pageDataArray[i].items[j][JV.PROP_SIGNATURE_CELLS]);
  1770. // 1.2.3 重新设置电子签名日期cells的Top Bottom坐标
  1771. checkAndResetCellYPos(pageDataArray[i].items[j][JV.PROP_SIGNATURE_DATE_CELLS]);
  1772. // 1.2.4 重置电子签名审核意见cells的Top Bottom坐标
  1773. checkAndResetCellYPos(pageDataArray[i].items[j][JV.PROP_SIGNATURE_AUDIT_CELLS]);
  1774. // 1.2.5 重置水印的Top Bottom坐标
  1775. checkAndResetCellYPos(pageDataArray[i].items[j][JV.PROP_WATERMARK_CELLS]);
  1776. 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;
  1777. offsetY += (maxY - minY);
  1778. if (bottomGap > 10) {
  1779. offsetY += (bottomGap - 10);
  1780. }
  1781. }
  1782. }
  1783. // 2. newPageData的items属性及相关分页符
  1784. newPageData.items = [];
  1785. newPageData.pageBreaks = [];
  1786. for (let i = 0; i < pageDataArray.length; i++) {
  1787. const pageItem = {};
  1788. pageItem[JV.PROP_PAGE_SEQ] = i + 1;
  1789. pageItem[JV.PROP_CELLS] = [];
  1790. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1791. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_CELLS].length; k++) {
  1792. pageItem[JV.PROP_CELLS].push(pageDataArray[i].items[j][JV.PROP_CELLS][k]);
  1793. }
  1794. }
  1795. pageItem[JV.PROP_SIGNATURE_CELLS] = [];
  1796. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1797. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_SIGNATURE_CELLS].length; k++) {
  1798. pageItem[JV.PROP_SIGNATURE_CELLS].push(pageDataArray[i].items[j][JV.PROP_SIGNATURE_CELLS][k]);
  1799. }
  1800. }
  1801. pageItem[JV.PROP_SIGNATURE_DATE_CELLS] = [];
  1802. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1803. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_SIGNATURE_DATE_CELLS].length; k++) {
  1804. pageItem[JV.PROP_SIGNATURE_DATE_CELLS].push(pageDataArray[i].items[j][JV.PROP_SIGNATURE_DATE_CELLS][k]);
  1805. }
  1806. }
  1807. pageItem[JV.PROP_SIGNATURE_AUDIT_CELLS] = [];
  1808. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1809. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_SIGNATURE_AUDIT_CELLS].length; k++) {
  1810. pageItem[JV.PROP_SIGNATURE_AUDIT_CELLS].push(pageDataArray[i].items[j][JV.PROP_SIGNATURE_AUDIT_CELLS][k]);
  1811. }
  1812. }
  1813. pageItem[JV.PROP_WATERMARK_CELLS] = [];
  1814. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1815. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_WATERMARK_CELLS].length; k++) {
  1816. pageItem[JV.PROP_WATERMARK_CELLS].push(pageDataArray[i].items[j][JV.PROP_WATERMARK_CELLS][k]);
  1817. }
  1818. }
  1819. newPagePos[i][JV.NODE_PAGE_SIZE] = pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE];
  1820. pageItem[JV.PAGE_SPECIAL_MERGE_POS] = newPagePos[i];
  1821. newPageData.items.push(pageItem);
  1822. // 重新分配分页符()------------------
  1823. if (pageDataArray[i].pageBreaks.length === 1) {
  1824. newPageData.pageBreaks.push(pageDataArray[i].pageBreaks[0]);
  1825. } else {
  1826. newPageData.pageBreaks.push([]);
  1827. }
  1828. // 重新分配分页符结束------------------
  1829. }
  1830. // 3. everything is ok, then call me
  1831. // let roleRel = null; // 未来调用的时候,这个属性要从外面给!!!
  1832. // roleRelArr
  1833. me.exportExcel(newPageData, paperSize, fName, 'false', sheetNames, custMergeBands, baseDir, roleRelArr, callback);
  1834. // fsUtil.writeObjToFile(newPageData, 'D:/GitHome/ConstructionOperation/tmp/combinedHeader.js');
  1835. } catch (e) {
  1836. console.log(e);
  1837. }
  1838. },
  1839. };