rpt_excel_util.js 75 KB

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