rpt_jsexcel.js 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778
  1. /**
  2. * Created by Tony on 2022/2/21.
  3. * 导出excel从后端移到前端处理
  4. */
  5. // const JV = require('../rpt_component/jpc_value_define'); // JV对象默认已有
  6. // const JSZip = require('jszip'); // 已引用
  7. // const strUtil = require('../public/stringUtil'); // stringUtil对象也有
  8. const DPI = 96;
  9. const dftHeadXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
  10. const STD_COMP_STAMP_SIZE_WIDTH = Math.round(4.2 * 96 / 2.54); // 公章标准尺寸(宽4.2厘米)转成像素
  11. const STD_COMP_STAMP_SIZE_HEIGHT = STD_COMP_STAMP_SIZE_WIDTH; // 公章标准尺寸(高4.2厘米)转成像素
  12. function writeContentTypes(sheets, isSinglePage, hasSignature) {
  13. const rst = [];
  14. rst.push(dftHeadXml + '\r\n');
  15. rst.push('<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">');
  16. rst.push('<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>');
  17. rst.push('<Default Extension="xml" ContentType="application/xml"/>');
  18. if (hasSignature) {
  19. rst.push('<Default Extension="png" ContentType="image/png"/>');
  20. }
  21. rst.push('<Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/>');
  22. rst.push('<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>');
  23. rst.push('<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>');
  24. rst.push('<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>');
  25. rst.push('<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>');
  26. if (isSinglePage) {
  27. rst.push('<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>');
  28. if (hasSignature) {
  29. rst.push('<Override PartName="/xl/drawings/drawing1.xml" ContentType="application/vnd.openxmlformats-officedocument.drawing+xml"/>');
  30. }
  31. } else {
  32. for (let i = 0; i < sheets.length; i++) {
  33. rst.push('<Override PartName="/xl/worksheets/sheet' + (i + 1) + '.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>');
  34. if (hasSignature) {
  35. rst.push('<Override PartName="/xl/drawings/drawing' + (i + 1) + '.xml" ContentType="application/vnd.openxmlformats-officedocument.drawing+xml"/>');
  36. }
  37. }
  38. }
  39. rst.push('<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>');
  40. rst.push('</Types>');
  41. return rst;
  42. }
  43. function writeRootRels() {
  44. const rst = [];
  45. rst.push(dftHeadXml + '\r\n');
  46. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  47. rst.push('<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>');
  48. rst.push('<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>');
  49. rst.push('<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>');
  50. rst.push('</Relationships>');
  51. return rst;
  52. }
  53. function writeApp(sheets, isSinglePage) {
  54. const rst = [];
  55. rst.push(dftHeadXml + '\r\n');
  56. rst.push('<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">');
  57. rst.push('<Application>Microsoft Excel</Application>');
  58. rst.push('<DocSecurity>0</DocSecurity>');
  59. rst.push('<ScaleCrop>false</ScaleCrop>');
  60. rst.push('<HeadingPairs>');
  61. rst.push('<vt:vector size="2" baseType="variant">');
  62. rst.push('<vt:variant><vt:lpstr>工作表</vt:lpstr></vt:variant>');
  63. if (isSinglePage) rst.push('<vt:variant><vt:i4>1</vt:i4></vt:variant>');
  64. else rst.push('<vt:variant><vt:i4>' + sheets.length + '</vt:i4></vt:variant>');
  65. rst.push('</vt:vector>');
  66. rst.push('</HeadingPairs>');
  67. rst.push('<TitlesOfParts>');
  68. if (isSinglePage) {
  69. rst.push('<vt:vector size="1" baseType="lpstr">');
  70. rst.push('<vt:lpstr>' + sheets[0].sheetName + '</vt:lpstr>');
  71. } else {
  72. rst.push('<vt:vector size="' + sheets.length + '" baseType="lpstr">');
  73. for (let i = 0; i < sheets.length; i++) {
  74. rst.push('<vt:lpstr>' + sheets[i].sheetName + '</vt:lpstr>');
  75. }
  76. }
  77. rst.push('</vt:vector>');
  78. rst.push('</TitlesOfParts>');
  79. rst.push('<Company>SmartCost</Company>');
  80. rst.push('<LinksUpToDate>false</LinksUpToDate>');
  81. rst.push('<SharedDoc>false</SharedDoc>');
  82. rst.push('<HyperlinksChanged>false</HyperlinksChanged>');
  83. rst.push('<AppVersion>12.0000</AppVersion>');
  84. rst.push('</Properties>');
  85. return rst;
  86. }
  87. function writeCore() {
  88. const rst = [];
  89. const p_fillZero = function(val) {
  90. let rst = val;
  91. if (val < 10) {
  92. rst = '0' + val;
  93. }
  94. return rst;
  95. };
  96. rst.push(dftHeadXml + '\r\n');
  97. 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">');
  98. rst.push('<dc:creator>SmartCost</dc:creator>');
  99. rst.push('<cp:lastModifiedBy>SmartCost</cp:lastModifiedBy>');
  100. const dt = new Date();
  101. dt.setDate(dt.getDate() - 8 / 24); // it's GMT time, so please add the server offset time ( -8 hours )
  102. const dtStr = dt.getFullYear() + '-' + p_fillZero(dt.getMonth() + 1) + '-' + p_fillZero(dt.getDate()) + 'T' +
  103. p_fillZero(dt.getHours()) + ':' + p_fillZero(dt.getMinutes()) + ':' + p_fillZero(dt.getSeconds()) + 'Z';
  104. rst.push('<dcterms:created xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:created>');
  105. rst.push('<dcterms:modified xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:modified>');
  106. rst.push('</cp:coreProperties>');
  107. return rst;
  108. }
  109. function writeXlWorkBook(sheets, isSinglePage) {
  110. const rst = [];
  111. rst.push(dftHeadXml + '\r\n');
  112. rst.push('<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  113. rst.push('<fileVersion appName="xl" lastEdited="4" lowestEdited="4" rupBuild="4505"/>');
  114. rst.push('<workbookPr defaultThemeVersion="124226"/>');
  115. rst.push('<bookViews><workbookView xWindow="360" yWindow="345" windowWidth="14655" windowHeight="4305"/></bookViews>');
  116. rst.push('<sheets>');
  117. if (isSinglePage) {
  118. rst.push('<sheet name="' + sheets[0].sheetName + '" sheetId="1" r:id="rId1"/>');
  119. } else {
  120. for (let i = 0; i < sheets.length; i++) {
  121. rst.push('<sheet name="' + sheets[i].sheetName + '" sheetId="' + (i + 1) + '" r:id="rId' + (i + 1) + '"/>');
  122. }
  123. }
  124. rst.push('</sheets>');
  125. rst.push('<calcPr calcId="124519"/>');
  126. rst.push('</workbook>');
  127. return rst;
  128. }
  129. function writeXlRels(sheets, isSinglePage) {
  130. const rst = [];
  131. let idx = 1;
  132. rst.push(dftHeadXml + '\r\n');
  133. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  134. if (isSinglePage) {
  135. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>');
  136. idx++;
  137. } else {
  138. for (let i = 0; i < sheets.length; i++) {
  139. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet' + (i + 1) + '.xml"/>');
  140. idx++;
  141. }
  142. }
  143. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>');
  144. idx++;
  145. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>');
  146. idx++;
  147. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/>');
  148. rst.push('</Relationships>');
  149. return rst;
  150. }
  151. function writeTheme() {
  152. // const rst = fs.readFileSync(__dirname + '/excel_base_files/theme1.xml', 'utf8', 'r');
  153. const rst = [`<?xml version="1.0"encoding="UTF-8"standalone="yes"?><a:theme xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"name="Office 主题"><a:themeElements><a:clrScheme name="Office"><a:dk1><a:sysClr val="windowText"lastClr="000000"/></a:dk1><a:lt1><a:sysClr val="window"lastClr="FFFFFF"/></a:lt1><a:dk2><a:srgbClr val="1F497D"/></a:dk2><a:lt2><a:srgbClr val="EEECE1"/></a:lt2><a:accent1><a:srgbClr val="4F81BD"/></a:accent1><a:accent2><a:srgbClr val="C0504D"/></a:accent2><a:accent3><a:srgbClr val="9BBB59"/></a:accent3><a:accent4><a:srgbClr val="8064A2"/></a:accent4><a:accent5><a:srgbClr val="4BACC6"/></a:accent5><a:accent6><a:srgbClr val="F79646"/></a:accent6><a:hlink><a:srgbClr val="0000FF"/></a:hlink><a:folHlink><a:srgbClr val="800080"/></a:folHlink></a:clrScheme><a:fontScheme name="Office"><a:majorFont><a:latin typeface="Cambria"/><a:ea typeface=""/><a:cs typeface=""/><a:font script="Jpan"typeface="MS Pゴシック"/><a:font script="Hang"typeface="맑은 고딕"/><a:font script="Hans"typeface="宋体"/><a:font script="Hant"typeface="新細明體"/><a:font script="Arab"typeface="Times New Roman"/><a:font script="Hebr"typeface="Times New Roman"/><a:font script="Thai"typeface="Tahoma"/><a:font script="Ethi"typeface="Nyala"/><a:font script="Beng"typeface="Vrinda"/><a:font script="Gujr"typeface="Shruti"/><a:font script="Khmr"typeface="MoolBoran"/><a:font script="Knda"typeface="Tunga"/><a:font script="Guru"typeface="Raavi"/><a:font script="Cans"typeface="Euphemia"/><a:font script="Cher"typeface="Plantagenet Cherokee"/><a:font script="Yiii"typeface="Microsoft Yi Baiti"/><a:font script="Tibt"typeface="Microsoft Himalaya"/><a:font script="Thaa"typeface="MV Boli"/><a:font script="Deva"typeface="Mangal"/><a:font script="Telu"typeface="Gautami"/><a:font script="Taml"typeface="Latha"/><a:font script="Syrc"typeface="Estrangelo Edessa"/><a:font script="Orya"typeface="Kalinga"/><a:font script="Mlym"typeface="Kartika"/><a:font script="Laoo"typeface="DokChampa"/><a:font script="Sinh"typeface="Iskoola Pota"/><a:font script="Mong"typeface="Mongolian Baiti"/><a:font script="Viet"typeface="Times New Roman"/><a:font script="Uigh"typeface="Microsoft Uighur"/></a:majorFont><a:minorFont><a:latin typeface="Calibri"/><a:ea typeface=""/><a:cs typeface=""/><a:font script="Jpan"typeface="MS Pゴシック"/><a:font script="Hang"typeface="맑은 고딕"/><a:font script="Hans"typeface="宋体"/><a:font script="Hant"typeface="新細明體"/><a:font script="Arab"typeface="Arial"/><a:font script="Hebr"typeface="Arial"/><a:font script="Thai"typeface="Tahoma"/><a:font script="Ethi"typeface="Nyala"/><a:font script="Beng"typeface="Vrinda"/><a:font script="Gujr"typeface="Shruti"/><a:font script="Khmr"typeface="DaunPenh"/><a:font script="Knda"typeface="Tunga"/><a:font script="Guru"typeface="Raavi"/><a:font script="Cans"typeface="Euphemia"/><a:font script="Cher"typeface="Plantagenet Cherokee"/><a:font script="Yiii"typeface="Microsoft Yi Baiti"/><a:font script="Tibt"typeface="Microsoft Himalaya"/><a:font script="Thaa"typeface="MV Boli"/><a:font script="Deva"typeface="Mangal"/><a:font script="Telu"typeface="Gautami"/><a:font script="Taml"typeface="Latha"/><a:font script="Syrc"typeface="Estrangelo Edessa"/><a:font script="Orya"typeface="Kalinga"/><a:font script="Mlym"typeface="Kartika"/><a:font script="Laoo"typeface="DokChampa"/><a:font script="Sinh"typeface="Iskoola Pota"/><a:font script="Mong"typeface="Mongolian Baiti"/><a:font script="Viet"typeface="Arial"/><a:font script="Uigh"typeface="Microsoft Uighur"/></a:minorFont></a:fontScheme><a:fmtScheme name="Office"><a:fillStyleLst><a:solidFill><a:schemeClr val="phClr"/></a:solidFill><a:gradFill rotWithShape="1"><a:gsLst><a:gs pos="0"><a:schemeClr val="phClr"><a:tint val="50000"/><a:satMod val="300000"/></a:schemeClr></a:gs><a:gs pos="35000"><a:schemeClr val="phClr"><a:tint val="37000"/><a:satMod val="300000"/></a:schemeClr></a:gs><a:gs pos="100000"><a:schemeClr val="phClr"><a:tint val="15000"/><a:satMod val="350000"/></a:schemeClr></a:gs></a:gsLst><a:lin ang="16200000"scaled="1"/></a:gradFill><a:gradFill rotWithShape="1"><a:gsLst><a:gs pos="0"><a:schemeClr val="phClr"><a:shade val="51000"/><a:satMod val="130000"/></a:schemeClr></a:gs><a:gs pos="80000"><a:schemeClr val="phClr"><a:shade val="93000"/><a:satMod val="130000"/></a:schemeClr></a:gs><a:gs pos="100000"><a:schemeClr val="phClr"><a:shade val="94000"/><a:satMod val="135000"/></a:schemeClr></a:gs></a:gsLst><a:lin ang="16200000"scaled="0"/></a:gradFill></a:fillStyleLst><a:lnStyleLst><a:ln w="9525"cap="flat"cmpd="sng"algn="ctr"><a:solidFill><a:schemeClr val="phClr"><a:shade val="95000"/><a:satMod val="105000"/></a:schemeClr></a:solidFill><a:prstDash val="solid"/></a:ln><a:ln w="25400"cap="flat"cmpd="sng"algn="ctr"><a:solidFill><a:schemeClr val="phClr"/></a:solidFill><a:prstDash val="solid"/></a:ln><a:ln w="38100"cap="flat"cmpd="sng"algn="ctr"><a:solidFill><a:schemeClr val="phClr"/></a:solidFill><a:prstDash val="solid"/></a:ln></a:lnStyleLst><a:effectStyleLst><a:effectStyle><a:effectLst><a:outerShdw blurRad="40000"dist="20000"dir="5400000"rotWithShape="0"><a:srgbClr val="000000"><a:alpha val="38000"/></a:srgbClr></a:outerShdw></a:effectLst></a:effectStyle><a:effectStyle><a:effectLst><a:outerShdw blurRad="40000"dist="23000"dir="5400000"rotWithShape="0"><a:srgbClr val="000000"><a:alpha val="35000"/></a:srgbClr></a:outerShdw></a:effectLst></a:effectStyle><a:effectStyle><a:effectLst><a:outerShdw blurRad="40000"dist="23000"dir="5400000"rotWithShape="0"><a:srgbClr val="000000"><a:alpha val="35000"/></a:srgbClr></a:outerShdw></a:effectLst><a:scene3d><a:camera prst="orthographicFront"><a:rot lat="0"lon="0"rev="0"/></a:camera><a:lightRig rig="threePt"dir="t"><a:rot lat="0"lon="0"rev="1200000"/></a:lightRig></a:scene3d><a:sp3d><a:bevelT w="63500"h="25400"/></a:sp3d></a:effectStyle></a:effectStyleLst><a:bgFillStyleLst><a:solidFill><a:schemeClr val="phClr"/></a:solidFill><a:gradFill rotWithShape="1"><a:gsLst><a:gs pos="0"><a:schemeClr val="phClr"><a:tint val="40000"/><a:satMod val="350000"/></a:schemeClr></a:gs><a:gs pos="40000"><a:schemeClr val="phClr"><a:tint val="45000"/><a:shade val="99000"/><a:satMod val="350000"/></a:schemeClr></a:gs><a:gs pos="100000"><a:schemeClr val="phClr"><a:shade val="20000"/><a:satMod val="255000"/></a:schemeClr></a:gs></a:gsLst><a:path path="circle"><a:fillToRect l="50000"t="-80000"r="50000"b="180000"/></a:path></a:gradFill><a:gradFill rotWithShape="1"><a:gsLst><a:gs pos="0"><a:schemeClr val="phClr"><a:tint val="80000"/><a:satMod val="300000"/></a:schemeClr></a:gs><a:gs pos="100000"><a:schemeClr val="phClr"><a:shade val="30000"/><a:satMod val="200000"/></a:schemeClr></a:gs></a:gsLst><a:path path="circle"><a:fillToRect l="50000"t="50000"r="50000"b="50000"/></a:path></a:gradFill></a:bgFillStyleLst></a:fmtScheme></a:themeElements><a:objectDefaults/><a:extraClrSchemeLst/></a:theme>`];
  154. // 前端没有这个文件,需要想想怎么给(说白了,也就是一个一个xml文件而已)
  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 (stringUtil.convertStrToBoolean(font[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]])) {
  167. rst.push('<b/>');
  168. }
  169. if (stringUtil.convertStrToBoolean(font[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]])) {
  170. rst.push('<i/>');
  171. }
  172. if (stringUtil.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]] && !stringUtil.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 (stringUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[0]])) {
  265. alignStr += ' shrinkToFit="1"';
  266. }
  267. if (stringUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[4]]) && !stringUtil.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 || pCells.isStamp) {
  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, true);
  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. if (cell.signature_name.indexOf(JV.SIGNATURE_NAME_DUMMY) >= 0) {
  511. area[JV.IDX_LEFT] = cell[JV.PROP_AREA][JV.PROP_LEFT];
  512. area[JV.IDX_TOP] = cell[JV.PROP_AREA][JV.PROP_TOP];
  513. area[JV.IDX_RIGHT] = cell[JV.PROP_AREA][JV.PROP_RIGHT];
  514. area[JV.IDX_BOTTOM] = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  515. }
  516. private_set_cell_pos2(area, theXPos, theYPos);
  517. }
  518. for (let i = 0; i < theShtData[JV.PROP_SIGNATURE_DATE_CELLS].length; i++) {
  519. cell = theShtData[JV.PROP_SIGNATURE_DATE_CELLS][i];
  520. private_set_cell_pos(cell, theXPos, theYPos);
  521. }
  522. for (let i = 0; i < theShtData[JV.PROP_SIGNATURE_AUDIT_CELLS].length; i++) {
  523. cell = theShtData[JV.PROP_SIGNATURE_AUDIT_CELLS][i];
  524. private_set_cell_pos(cell, theXPos, theYPos);
  525. }
  526. for (let i = 0; i < theShtData[JV.PROP_WATERMARK_CELLS].length; i++) {
  527. cell = theShtData[JV.PROP_WATERMARK_CELLS][i];
  528. // private_set_cell_pos(cell, theXPos, theYPos);
  529. let cellControl;
  530. if (typeof cell[JV.PROP_CONTROL] === 'string') {
  531. cellControl = pageData[JV.NODE_CONTROL_COLLECTION][cell[JV.PROP_CONTROL]];
  532. } else {
  533. cellControl = cell[JV.PROP_CONTROL];
  534. }
  535. const area = getProperSignatureArea(cell, cellControl);
  536. if (cell.signature_name.indexOf(JV.SIGNATURE_NAME_DUMMY) >= 0) {
  537. area[JV.IDX_LEFT] = cell[JV.PROP_AREA][JV.PROP_LEFT];
  538. area[JV.IDX_TOP] = cell[JV.PROP_AREA][JV.PROP_TOP];
  539. area[JV.IDX_RIGHT] = cell[JV.PROP_AREA][JV.PROP_RIGHT];
  540. area[JV.IDX_BOTTOM] = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  541. }
  542. private_set_cell_pos2(area, theXPos, theYPos);
  543. }
  544. };
  545. xPos.push(0);
  546. if (sheetData) {
  547. // current sheet data
  548. // console.log('preAnalyzePos not single');
  549. yPos.push(0);
  550. self_analyze_sheet_pos(sheetData, xPos, yPos);
  551. xPos.sort(private_array_sort);
  552. yPos.sort(private_array_sort);
  553. } else {
  554. // total data in one sheet
  555. 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);
  556. // console.log('preAnalyzePos isSingle start');
  557. for (const shtItemData of pageData.items) {
  558. const tmpPos = [];
  559. tmpPos.push(0);
  560. self_analyze_sheet_pos(shtItemData, xPos, tmpPos);
  561. tmpPos.sort(private_array_sort);
  562. if (marginBottomPos - tmpPos[tmpPos.length - 1] > 10) {
  563. // 此逻辑是为了防止打印跨页(假设有些报表模板高度设置离底部margin还好远,导出excel后预览时会发现跨页现象(即下一页的某几行数据会挪到前一页来预览))
  564. tmpPos.push(marginBottomPos - 10);
  565. }
  566. // console.log('preAnalyzePos push to yMultiPos');
  567. yMultiPos.push(tmpPos);
  568. }
  569. xPos.sort(private_array_sort);
  570. yPos.splice(0, yPos.length);
  571. for (const p of yMultiPos[0]) {
  572. yPos.push(p);
  573. }
  574. }
  575. }
  576. function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, appointedMergeBand, hasSignature, sheetIdx, signSheetIdxArr, isFirstSheet, pgBrkIdx) {
  577. const rst = [];
  578. const xPos = [];
  579. const yPos = [];
  580. const yMultiPos = [];
  581. let currentMergeBorder = null;
  582. const headerStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  583. let currentPageMergePos = null; // 在 JV.PAGING_OPTION_INFINITY 场合应用
  584. const private_getCellIdxStr = function(idx) {
  585. let rst = 'A';
  586. if (idx < 26) {
  587. rst = headerStr[idx];
  588. } else if (idx < 26 * 26 + 26) {
  589. const ti = Math.floor(idx / 26);
  590. const tj = idx % 26;
  591. rst = headerStr[ti - 1] + headerStr[tj];
  592. } else if (idx < 26 * 26 * 26 + 26) {
  593. const ti = Math.floor(idx / (26 * 26));
  594. const tj = Math.floor((idx - ti * 26 * 26) / 26);
  595. const tk = idx % 26;
  596. rst = headerStr[ti - 1] + headerStr[tj - 1] + headerStr[tk];
  597. }
  598. return rst;
  599. };
  600. const private_getSharedStrIdx = function(val) {
  601. let strVal = val;
  602. if (val === null || val === undefined || (typeof val === 'number' && isNaN(val))) {
  603. strVal = '';
  604. }
  605. let rst = sharedStrList.indexOf(strVal);
  606. if (rst < 0) {
  607. sharedStrList.push(strVal);
  608. rst = sharedStrList.length - 1;
  609. }
  610. return rst;
  611. };
  612. const private_getFontId = function(cell) {
  613. let rst = 0;
  614. let hasFont = false;
  615. if (!(stylesObj.fonts)) {
  616. stylesObj.fonts = [];
  617. // for (let i = 0; i < sheetData.font_collection)
  618. }
  619. let sheetFont = null;
  620. if (typeof cell[JV.PROP_FONT] === 'string') {
  621. sheetFont = pageData[JV.NODE_FONT_COLLECTION][cell[JV.PROP_FONT]];
  622. } else {
  623. sheetFont = cell[JV.PROP_FONT];
  624. }
  625. for (let i = 0; i < stylesObj.fonts.length; i++) {
  626. const font = stylesObj.fonts[i];
  627. if (sheetFont) {
  628. 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)
  629. && 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]]
  630. && font[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]] === sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]]) {
  631. hasFont = true;
  632. rst = i;
  633. break;
  634. }
  635. } else {
  636. break;
  637. }
  638. }
  639. if (!hasFont) {
  640. const font = {};
  641. font[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]] = sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]]; // font name
  642. // font.size = Math.round(sheetFont[JV.FONT_PROPS[1]] * 3 / 4);
  643. font.size = Math.floor(sheetFont[JV.FONT_PROPS[1]] * 3 / 4);
  644. font.charset = 134;
  645. font.colorIdx = '8';
  646. font[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] = sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]]; // font bold
  647. font[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]] = sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]]; // font italic
  648. font[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]] = sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]]; // font underline
  649. stylesObj.fonts.push(font);
  650. rst = stylesObj.fonts.length - 1;
  651. }
  652. return rst;
  653. };
  654. const private_chkAndGetMergeLine = function(cell, sheetBorder, borderStr, needFurtherChk) {
  655. let rst = 0;
  656. const mergeBorder = currentMergeBorder;
  657. let mergeBand = pageData[JV.BAND_PROP_MERGE_BAND];
  658. if (appointedMergeBand !== null) {
  659. mergeBand = appointedMergeBand;
  660. }
  661. if (sheetBorder[borderStr] && sheetBorder[borderStr][JV.PROP_LINE_WEIGHT] !== undefined) {
  662. rst = sheetBorder[borderStr][JV.PROP_LINE_WEIGHT];
  663. }
  664. if (currentPageMergePos) {
  665. const side = currentPageMergePos[borderStr];
  666. if (side.indexOf(cell[JV.PROP_AREA][borderStr]) >= 0) {
  667. if (needFurtherChk) {
  668. const topSide = currentPageMergePos[JV.PROP_TOP];
  669. const bottomSide = currentPageMergePos[JV.PROP_BOTTOM];
  670. for (let i = 0; i < topSide.length; i++) {
  671. if (cell[JV.PROP_AREA][JV.PROP_TOP] >= topSide[i]) {
  672. if (cell[JV.PROP_AREA][JV.PROP_BOTTOM] <= bottomSide[i]) {
  673. const destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
  674. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  675. break;
  676. }
  677. } else {
  678. break;
  679. }
  680. }
  681. } else {
  682. const destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
  683. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  684. }
  685. }
  686. } else {
  687. if (mergeBorder && mergeBorder[borderStr] && cell[JV.PROP_AREA][borderStr] === mergeBorder[borderStr]) {
  688. const destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
  689. if (needFurtherChk) {
  690. if (cell[JV.PROP_AREA][JV.PROP_TOP] >= mergeBorder[JV.PROP_TOP] &&
  691. cell[JV.PROP_AREA][JV.PROP_BOTTOM] <= mergeBorder[JV.PROP_BOTTOM]) {
  692. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  693. }
  694. } else {
  695. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  696. }
  697. }
  698. }
  699. return parseInt(rst);
  700. };
  701. const private_checkBorder = function(cell, border, sheetBorder) {
  702. let rst = true;
  703. const borderLineWidths = [];
  704. const sheetBorderLineWidths = [];
  705. borderLineWidths.push(border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]);
  706. borderLineWidths.push(border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]);
  707. borderLineWidths.push(border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]);
  708. borderLineWidths.push(border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]);
  709. if (sheetBorder[JV.PROP_LEFT] && sheetBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]) {
  710. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_LEFT, true));
  711. } else {
  712. sheetBorderLineWidths.push(0);
  713. }
  714. if (sheetBorder[JV.PROP_RIGHT] && sheetBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]) {
  715. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_RIGHT, true));
  716. } else {
  717. sheetBorderLineWidths.push(0);
  718. }
  719. if (sheetBorder[JV.PROP_TOP] && sheetBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]) {
  720. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_TOP, false));
  721. } else {
  722. sheetBorderLineWidths.push(0);
  723. }
  724. if (sheetBorder[JV.PROP_BOTTOM] && sheetBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]) {
  725. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_BOTTOM, false));
  726. } else {
  727. sheetBorderLineWidths.push(0);
  728. }
  729. for (let i = 0; i < 4; i++) {
  730. if (borderLineWidths[i] != sheetBorderLineWidths[i]) {
  731. rst = false;
  732. break;
  733. }
  734. }
  735. return rst;
  736. };
  737. const private_getIniBorder = function() {
  738. const rst = {};
  739. rst[JV.PROP_LEFT] = {};
  740. rst[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = 0;
  741. rst[JV.PROP_RIGHT] = {};
  742. rst[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = 0;
  743. rst[JV.PROP_TOP] = {};
  744. rst[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = 0;
  745. rst[JV.PROP_BOTTOM] = {};
  746. rst[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = 0;
  747. return rst;
  748. };
  749. const private_getBorderId = function(cell) {
  750. let rst = 0;
  751. let hasBorder = false;
  752. if (!(stylesObj.borders)) {
  753. stylesObj.borders = [];
  754. }
  755. const sheetBorder = pageData[JV.NODE_STYLE_COLLECTION][cell.style];
  756. const mergedBorder = private_getIniBorder();
  757. mergedBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_LEFT, true);
  758. mergedBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_RIGHT, true);
  759. mergedBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_TOP, false);
  760. mergedBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_BOTTOM, false);
  761. for (let i = 0; i < stylesObj.borders.length; i++) {
  762. const border = stylesObj.borders[i];
  763. if (private_checkBorder(cell, border, mergedBorder)) {
  764. hasBorder = true;
  765. rst = i;
  766. break;
  767. }
  768. }
  769. if (!hasBorder) {
  770. stylesObj.borders.push(mergedBorder);
  771. rst = stylesObj.borders.length - 1;
  772. }
  773. return rst;
  774. };
  775. const private_checkControl = function(cellControl, sheetControl) {
  776. let rst = true;
  777. for (let i = 0; i < JV.CONTROL_PROPS.length; i++) {
  778. if (cellControl[JV.CONTROL_PROPS[i]] !== sheetControl[JV.CONTROL_PROPS[i]]) {
  779. rst = false;
  780. break;
  781. }
  782. }
  783. return rst;
  784. };
  785. const private_getStyleId = function(cell) {
  786. let rst = 1;
  787. let hasStyle = false;
  788. if (!(stylesObj.cellXfs)) stylesObj.cellXfs = [];
  789. const fontId = private_getFontId(cell);
  790. let fontAngle = 0;
  791. if (typeof cell[JV.PROP_FONT] === 'string') {
  792. fontAngle = parseInt(pageData[JV.NODE_FONT_COLLECTION][cell[JV.PROP_FONT]].FontAngle);
  793. } else {
  794. fontAngle = parseInt(cell[JV.PROP_FONT].FontAngle);
  795. }
  796. const borderId = private_getBorderId(cell);
  797. let cellControl = null;
  798. if (typeof cell[JV.PROP_CONTROL] === 'string') {
  799. cellControl = pageData[JV.NODE_CONTROL_COLLECTION][cell[JV.PROP_CONTROL]];
  800. } else {
  801. cellControl = cell[JV.PROP_CONTROL];
  802. }
  803. for (let i = 0; i < stylesObj.cellXfs.length; i++) {
  804. const sheetControl = stylesObj.cellXfs[i];
  805. if (sheetControl.fontId === fontId && sheetControl.borderId === borderId) {
  806. if (private_checkControl(cellControl, sheetControl)) {
  807. rst = i;
  808. hasStyle = true;
  809. break;
  810. }
  811. }
  812. }
  813. if (!hasStyle) {
  814. const sheetControl = {};
  815. sheetControl.fontId = fontId;
  816. sheetControl.borderId = borderId;
  817. sheetControl.fontAngle = fontAngle;
  818. for (let i = 0; i < JV.CONTROL_PROPS.length; i++) {
  819. sheetControl[JV.CONTROL_PROPS[i]] = cellControl[JV.CONTROL_PROPS[i]];
  820. }
  821. stylesObj.cellXfs.push(sheetControl);
  822. rst = stylesObj.cellXfs.length - 1;
  823. }
  824. return rst;
  825. };
  826. const private_setCols = function() {
  827. // remark: 1 excel unit width = 2.117 mm
  828. rst.push('<cols>');
  829. let w = 0;
  830. for (let i = 1; i < xPos.length - 1; i++) {
  831. w = (xPos[i + 1] - xPos[i]) / DPI * 25.4 / 2.117;
  832. w = Math.round(w * 1000) / 1000;
  833. rst.push('<col min="' + i + '" max="' + i + '" width="' + w + '" customWidth="1"/>');
  834. }
  835. rst.push('<col min="' + (xPos.length - 1) + '" max="' + (xPos.length - 1) + '" width="' + 10 + '" customWidth="1"/>');
  836. rst.push('</cols>');
  837. };
  838. const private_setMergedCells = function() {
  839. let cnt = 0;
  840. rst.push('<mergeCells count="?">');
  841. const startIdx = rst.length - 1;
  842. const self_setMergedCells = function(cells, theYPos, offsetY) {
  843. for (let i = 0; i < cells.length; i++) {
  844. const cell = cells[i];
  845. if (!cell.isStamp) {
  846. const idxR = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_RIGHT]);
  847. const idxL = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_LEFT]);
  848. const idxB = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_BOTTOM]);
  849. const idxT = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_TOP]);
  850. if (idxR - idxL > 1 || idxB - idxT > 1) {
  851. rst.push('<mergeCell ref="' + private_getCellIdxStr(idxL - 1) + (idxT + offsetY) + ':' + private_getCellIdxStr(idxR - 2) + (idxB - 1 + offsetY) + '"/>');
  852. cnt++;
  853. }
  854. }
  855. }
  856. };
  857. if (sheetData) {
  858. // self_setMergedCells(sheetData, yPos, 0);
  859. self_setMergedCells(sheetData[JV.PROP_CELLS], yPos, 0);
  860. if (sheetData[JV.PROP_SIGNATURE_DATE_CELLS]) self_setMergedCells(sheetData[JV.PROP_SIGNATURE_DATE_CELLS], yPos, 0);
  861. if (sheetData[JV.PROP_SIGNATURE_AUDIT_CELLS]) self_setMergedCells(sheetData[JV.PROP_SIGNATURE_AUDIT_CELLS], yPos, 0);
  862. } else {
  863. let osY = 0;
  864. for (let i = 0; i < pageData.items.length; i++) {
  865. const shtItemData = pageData.items[i];
  866. const tmpPos = yMultiPos[i];
  867. // self_setMergedCells(shtItemData, tmpPos, osY);
  868. self_setMergedCells(shtItemData[JV.PROP_CELLS], tmpPos, osY);
  869. if (shtItemData[JV.PROP_SIGNATURE_DATE_CELLS]) self_setMergedCells(shtItemData[JV.PROP_SIGNATURE_DATE_CELLS], tmpPos, osY);
  870. if (shtItemData[JV.PROP_SIGNATURE_AUDIT_CELLS]) self_setMergedCells(shtItemData[JV.PROP_SIGNATURE_AUDIT_CELLS], tmpPos, osY);
  871. osY += tmpPos.length - 2;
  872. }
  873. }
  874. rst[startIdx] = '<mergeCells count="' + cnt + '">';
  875. rst.push('</mergeCells>');
  876. };
  877. const private_setSheetData = function() {
  878. // remark: 1 excel unit height = 0.3612 mm
  879. rst.push('<sheetData>');
  880. const spanX = xPos.length - 2;
  881. let cellIdx = 0;
  882. let h = 0;
  883. const self_create_cell = function(cells, rows, theYPos) {
  884. let rowIdx1 = 0;
  885. let colIdx1 = 0;
  886. let rowIdx2 = 0;
  887. let colIdx2 = 0;
  888. for (let cIdx = 0; cIdx < cells.length; cIdx++) {
  889. const styleIdx = private_getStyleId(cells[cIdx]);
  890. rowIdx1 = theYPos.indexOf(cells[cIdx][JV.PROP_AREA][JV.PROP_TOP]);
  891. colIdx1 = xPos.indexOf(cells[cIdx][JV.PROP_AREA][JV.PROP_LEFT]);
  892. if (rowIdx1 <= rows.length) {
  893. let cellObj = rows[rowIdx1 - 1].items[colIdx1 - 1];
  894. cellObj.s = styleIdx;
  895. cellObj.isBlank = false;
  896. if (!(stringUtil.isEmptyString(cells[cIdx][JV.PROP_VALUE]))) {
  897. const valIdx = private_getSharedStrIdx(cells[cIdx][JV.PROP_VALUE]);
  898. cellObj.v = valIdx;
  899. }
  900. rowIdx2 = theYPos.indexOf(cells[cIdx][JV.PROP_AREA][JV.PROP_BOTTOM]);
  901. colIdx2 = xPos.indexOf(cells[cIdx][JV.PROP_AREA][JV.PROP_RIGHT]);
  902. if ((rowIdx2 - rowIdx1 > 1) || (colIdx2 - colIdx1 > 1)) {
  903. for (let i = 0; i < rowIdx2 - rowIdx1; i++) {
  904. for (let j = 0; j < colIdx2 - colIdx1; j++) {
  905. if (i === 0 && j === 0) continue;
  906. cellObj = rows[rowIdx1 - 1 + i].items[colIdx1 - 1 + j];
  907. cellObj.s = styleIdx;
  908. cellObj.isBlank = true;
  909. }
  910. }
  911. }
  912. }
  913. }
  914. };
  915. const self_setDataEx = function(theShtData, theYPos, rowOffset) {
  916. const rows = [];
  917. // 1. build full set of blank rows/cells
  918. for (let i = 1; i < theYPos.length - 1; i++) {
  919. const rowObj = {};
  920. h = (theYPos[i + 1] - theYPos[i]) / DPI * 25.4 / 0.3612;
  921. h = Math.round(h * 1000) / 1000;
  922. rowObj.height = h;
  923. rowObj.r = i + rowOffset;
  924. rowObj.items = [];
  925. rows.push(rowObj);
  926. for (let j = 1; j < xPos.length - 1; j++) {
  927. const colIdxStr = private_getCellIdxStr(j - 1);
  928. const cellObj = {};
  929. cellObj.r = colIdxStr + (i + rowOffset);
  930. cellObj.s = 0;
  931. cellObj.isBlank = true;
  932. rows[i - 1].items.push(cellObj);
  933. }
  934. }
  935. // 2. then fill up the cell style-ids and values
  936. self_create_cell(theShtData[JV.PROP_CELLS], rows, theYPos);
  937. self_create_cell(theShtData[JV.PROP_SIGNATURE_DATE_CELLS], rows, theYPos);
  938. self_create_cell(theShtData[JV.PROP_SIGNATURE_AUDIT_CELLS], rows, theYPos);
  939. // fsUtil.writeObjToFile(newPageData, 'D:/GitHome/ConstructionOperation/tmp/combinedHeader.js');
  940. // console.log('theShtData[JV.PROP_SIGNATURE_AUDIT_CELLS]: ');
  941. // console.log(theShtData[JV.PROP_SIGNATURE_AUDIT_CELLS]);
  942. // 3. then fill up rst
  943. for (let i = 0; i < rows.length; i++) {
  944. rst.push('<row r="' + (i + 1 + rowOffset) + '" spans="1:' + spanX + '" ht="' + rows[i].height + '" customHeight="1">');
  945. for (let j = 0; j < rows[i].items.length; j++) {
  946. const cellObj = rows[i].items[j];
  947. if (cellObj.v === undefined) {
  948. rst.push('<c r="' + cellObj.r + '" s="' + cellObj.s + '"/>');
  949. } else {
  950. rst.push('<c r="' + cellObj.r + '" s="' + cellObj.s + '" t="s">');
  951. rst.push('<v>' + cellObj.v + '</v>');
  952. rst.push('</c>');
  953. }
  954. }
  955. rst.push('</row>');
  956. }
  957. // 4. maybe need to dispose the memory
  958. // ...
  959. };
  960. if (sheetData) {
  961. // current sheet data
  962. currentPageMergePos = sheetData[JV.PAGE_SPECIAL_MERGE_POS];
  963. currentMergeBorder = sheetData[JV.PROP_PAGE_MERGE_BORDER];
  964. if (hasSignature && sheetData[JV.PROP_SIGNATURE_CELLS] && sheetData[JV.PROP_SIGNATURE_CELLS].length > 0) {
  965. // 有签名情况下,还是有必要创建一个dummy cell的(画框用)
  966. _createDummyCell(sheetData[JV.PROP_SIGNATURE_CELLS], sheetData);
  967. }
  968. self_setDataEx(sheetData, yPos, 0);
  969. } else {
  970. // total data in one sheet
  971. let rowOffset = 0;
  972. for (let i = 0; i < pageData.items.length; i++) {
  973. const shtItemData = pageData.items[i];
  974. currentPageMergePos = shtItemData[JV.PAGE_SPECIAL_MERGE_POS];
  975. currentMergeBorder = shtItemData[JV.PROP_PAGE_MERGE_BORDER];
  976. const tmpPos = yMultiPos[i];
  977. // console.log(yMultiPos[i]);
  978. cellIdx = 0;
  979. if (hasSignature && shtItemData[JV.PROP_SIGNATURE_CELLS] && shtItemData[JV.PROP_SIGNATURE_CELLS].length > 0) {
  980. // 有签名情况下,还是有必要创建一个dummy cell的(画框用)
  981. _createDummyCell(shtItemData[JV.PROP_SIGNATURE_CELLS], shtItemData);
  982. }
  983. self_setDataEx(shtItemData, tmpPos, rowOffset);
  984. rowOffset += tmpPos.length - 2;
  985. }
  986. }
  987. rst.push('</sheetData>');
  988. };
  989. preAnalyzePos(pageData, sheetData, xPos, yPos, yMultiPos);
  990. rst.push(dftHeadXml + '\r\n');
  991. rst.push('<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  992. const colStr = private_getCellIdxStr(xPos.length - 3);
  993. rst.push('<dimension ref="A1:' + colStr + '' + yPos.length + '"/>');
  994. if (isFirstSheet) {
  995. rst.push('<sheetViews><sheetView showGridLines="1" tabSelected="1" workbookViewId="0">');
  996. } else {
  997. rst.push('<sheetViews><sheetView showGridLines="1" workbookViewId="0">');
  998. }
  999. // rst.push('<selection sqref="A1:' + colStr + '1"/>');
  1000. rst.push('<selection sqref="A1:A1"/>');
  1001. rst.push('</sheetView></sheetViews>');
  1002. rst.push('<sheetFormatPr defaultRowHeight="13.5"/>');
  1003. private_setCols();
  1004. private_setSheetData();
  1005. private_setMergedCells();
  1006. rst.push('<phoneticPr fontId="1" type="noConversion"/>');
  1007. // rst.push('<pageMargins left="0.315" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
  1008. rst.push('<pageMargins left="' + (parseFloat(pageData[JV.NODE_PAGE_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]) * 0.39375) +
  1009. '" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
  1010. const paperSizeIdx = JV.PAGES_SIZE_STR.indexOf(paperSize);
  1011. let pStr = '';
  1012. if (paperSizeIdx >= 0) {
  1013. pStr = 'paperSize="' + JV.PAGES_SIZE_IDX[paperSizeIdx] + '"';
  1014. }
  1015. let orientationStr = (pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][0] > pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1]) ? 'landscape' : 'portrait';
  1016. if (currentPageMergePos) {
  1017. orientationStr = (currentPageMergePos[JV.NODE_PAGE_SIZE][0] > currentPageMergePos[JV.NODE_PAGE_SIZE][1]) ? 'landscape' : 'portrait';
  1018. }
  1019. rst.push('<pageSetup ' + pStr + ' fitToWidth="0" fitToHeight="0" orientation="' + orientationStr + '" />');
  1020. rst.push('<headerFooter alignWithMargins="0"/>');
  1021. // 插入分页符---------------------------
  1022. if (pageData.pageBreaks) {
  1023. let pgBrks = [];
  1024. if (sheetData !== null) {
  1025. pgBrks = pageData.pageBreaks[pgBrkIdx];
  1026. } else {
  1027. pgBrks = pageData.pageBreaks[0];
  1028. }
  1029. if (pgBrks.length > 1) {
  1030. rst.push('<rowBreaks count="' + pgBrks.length + '" manualBreakCount="' + pgBrks.length + '" >');
  1031. for (let pbi = 0; pbi < pgBrks.length; pbi++) {
  1032. rst.push('<brk id="' + pgBrks[pbi] + '" max="16383" man="1" />');
  1033. }
  1034. rst.push('</rowBreaks>');
  1035. }
  1036. }
  1037. // 插入分页符结束---------------------------
  1038. // 图形要放在最后,否则excel会报错(但WPS不会)
  1039. if (hasSignature && (sheetData === null || signSheetIdxArr[sheetIdx])) {
  1040. rst.push('<drawing r:id="rId1"/>');
  1041. }
  1042. rst.push('</worksheet>');
  1043. return rst;
  1044. }
  1045. function writeWorkSheetRels(signSheetIdx) {
  1046. const rst = [];
  1047. rst.push('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
  1048. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  1049. rst.push('<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing" Target="../drawings/drawing' + signSheetIdx + '.xml"/>');
  1050. rst.push('</Relationships>');
  1051. return rst;
  1052. }
  1053. async function writeImage(path, pic) {
  1054. let rst = null;
  1055. if (path) {
  1056. // 前端方式处理不同,不用考虑哪里(水印也是),直接都一股脑地申请下载即可(重复的浏览器会找cache!)
  1057. let picpath = path; // 前端的path已经是完整的了
  1058. rst = await getBlob(picpath); //这里通过getBlob函数得到的结果已经是所需要的流数据,无需转换
  1059. }
  1060. return rst;
  1061. }
  1062. function writeDrawings(pageData, signKeyArr, signPathArr, isSinglePage, signSheetIdxArr) {
  1063. const rst = [];
  1064. // 优化调整,不再每页都设置一个,重复的签名只需要一个即可(极端case: 13xx多页的签名文档,导出有1.3G大)
  1065. if (isSinglePage) {
  1066. rst.push(writeDrawing(pageData, null, signKeyArr, 0));
  1067. } else {
  1068. for (let i = 0; i < pageData.items.length; i++) {
  1069. if (signSheetIdxArr[i]) {
  1070. rst.push(writeDrawing(pageData, pageData.items[i], signKeyArr, i));
  1071. }
  1072. }
  1073. }
  1074. // */
  1075. return rst;
  1076. }
  1077. function writeDrawing(pageData, sheetData, subSignKeyArr, sheetIdx) {
  1078. const rst = [];
  1079. const xPos = [];
  1080. const yPos = [];
  1081. const yMultiPos = [];
  1082. const private_setSheetDrawingCellData = function(signCell, theYPos, startPicIdx, offsetRow) {
  1083. let cellControl;
  1084. if (typeof signCell[JV.PROP_CONTROL] === 'string') {
  1085. cellControl = pageData[JV.NODE_CONTROL_COLLECTION][signCell[JV.PROP_CONTROL]];
  1086. } else {
  1087. cellControl = signCell[JV.PROP_CONTROL];
  1088. }
  1089. const area = getProperSignatureArea(signCell, cellControl);
  1090. if (signCell.signature_name.indexOf(JV.SIGNATURE_NAME_DUMMY) >= 0) {
  1091. area[JV.IDX_LEFT] = signCell[JV.PROP_AREA][JV.PROP_LEFT];
  1092. area[JV.IDX_TOP] = signCell[JV.PROP_AREA][JV.PROP_TOP];
  1093. area[JV.IDX_RIGHT] = signCell[JV.PROP_AREA][JV.PROP_RIGHT];
  1094. area[JV.IDX_BOTTOM] = signCell[JV.PROP_AREA][JV.PROP_BOTTOM];
  1095. }
  1096. // 1. from
  1097. // let colIdx = xPos.indexOf(signCell[JV.PROP_AREA][JV.PROP_LEFT]);
  1098. // let rowIdx = theYPos.indexOf(signCell[JV.PROP_AREA][JV.PROP_TOP]) + offsetRow; // 这里要考虑Row的偏移量(多页在同同一个sheet输出)
  1099. let colIdx = xPos.indexOf(area[0]);
  1100. let rowIdx = theYPos.indexOf(area[1]) + offsetRow; // 这里要考虑Row的偏移量(多页在同同一个sheet输出)
  1101. // 因为 xPos, theYPos的首位是0,所以还需要减1
  1102. colIdx--;
  1103. rowIdx--;
  1104. rst.push('<xdr:twoCellAnchor editAs="oneCell">');
  1105. rst.push('<xdr:from>');
  1106. rst.push('<xdr:col>' + colIdx + '</xdr:col>');
  1107. rst.push('<xdr:colOff>0</xdr:colOff>');
  1108. rst.push('<xdr:row>' + rowIdx + '</xdr:row>');
  1109. rst.push('<xdr:rowOff>0</xdr:rowOff>');
  1110. rst.push('</xdr:from>');
  1111. // 2. to
  1112. colIdx = xPos.indexOf(area[2]);
  1113. rowIdx = theYPos.indexOf(area[3]) + offsetRow;
  1114. // 因为 xPos, theYPos的首位是0,所以还需要减1
  1115. colIdx--;
  1116. rowIdx--;
  1117. rst.push('<xdr:to>');
  1118. rst.push('<xdr:col>' + colIdx + '</xdr:col>');
  1119. rst.push('<xdr:colOff>0</xdr:colOff>');
  1120. rst.push('<xdr:row>' + rowIdx + '</xdr:row>');
  1121. rst.push('<xdr:rowOff>0</xdr:rowOff>');
  1122. rst.push('</xdr:to>');
  1123. // 3. pic
  1124. rst.push('<xdr:pic>');
  1125. // 3.1
  1126. rst.push('<xdr:nvPicPr>');
  1127. rst.push('<xdr:cNvPr id="' + startPicIdx + '" name="图片 ' + startPicIdx + '"/>');
  1128. rst.push('<xdr:cNvPicPr><a:picLocks noChangeAspect="1" noChangeArrowheads="1"/></xdr:cNvPicPr>');
  1129. rst.push('</xdr:nvPicPr>');
  1130. // 3.2
  1131. rst.push('<xdr:blipFill>');
  1132. // 重点!!!
  1133. const picIdx = subSignKeyArr.indexOf(signCell.signature_name) + 1;
  1134. // 3.2.1
  1135. rst.push('<a:blip xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:embed="rId' + picIdx + '" cstate="print">');
  1136. rst.push('<a:extLst>');
  1137. rst.push('<a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">'); // 参考excel本身,都是固定的uri
  1138. rst.push('<a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0"/>');
  1139. rst.push('</a:ext>');
  1140. rst.push('</a:extLst>');
  1141. rst.push('</a:blip>');
  1142. // 3.2.2
  1143. rst.push('<a:srcRect/>');
  1144. // 3.2.3
  1145. rst.push('<a:stretch><a:fillRect/></a:stretch>');
  1146. rst.push('</xdr:blipFill>');
  1147. // 3.3
  1148. rst.push('<xdr:spPr bwMode="auto">');
  1149. // 3.3.1
  1150. rst.push('<a:xfrm>');
  1151. // 以下经过Excel及WPS的测试,这数据好像没什么意义,都设为0
  1152. rst.push('<a:off x="0" y="0"/>');
  1153. rst.push('<a:ext cx="0" cy="0"/>');
  1154. rst.push('</a:xfrm>');
  1155. // 3.3.2
  1156. rst.push('<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>');
  1157. // 3.3.3
  1158. rst.push('<a:noFill/>');
  1159. // 3.3.4
  1160. rst.push('<a:extLst>');
  1161. rst.push('<a:ext uri="{909E8E84-426E-40DD-AFC4-6F175D3DCCD1}">');
  1162. rst.push('<a14:hiddenFill xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main">');
  1163. rst.push('<a:solidFill><a:srgbClr val="FFFFFF"/></a:solidFill>');
  1164. rst.push('</a14:hiddenFill>');
  1165. rst.push('</a:ext>');
  1166. rst.push('</a:extLst>');
  1167. // 收尾
  1168. rst.push('</xdr:spPr>');
  1169. rst.push('</xdr:pic>');
  1170. // 4. client Data
  1171. rst.push('<xdr:clientData/>');
  1172. rst.push('</xdr:twoCellAnchor>');
  1173. };
  1174. preAnalyzePos(pageData, sheetData, xPos, yPos, yMultiPos);
  1175. rst.push('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
  1176. rst.push('<xdr:wsDr xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">');
  1177. // console.log('subSignKeyArr');
  1178. // console.log(subSignKeyArr);
  1179. if (sheetData) {
  1180. // console.log('sheetData');
  1181. let startPicIdx = 2 + sheetIdx * 1000;
  1182. for (const sCell of sheetData[JV.PROP_SIGNATURE_CELLS]) {
  1183. if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1184. private_setSheetDrawingCellData(sCell, yPos, startPicIdx, 0);
  1185. startPicIdx++;
  1186. }
  1187. }
  1188. for (const sCell of sheetData[JV.PROP_WATERMARK_CELLS]) {
  1189. if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1190. private_setSheetDrawingCellData(sCell, yPos, startPicIdx, 0);
  1191. startPicIdx++;
  1192. }
  1193. }
  1194. } else {
  1195. // total data in one sheet
  1196. let rowOffset = 0;
  1197. for (let i = 0; i < pageData.items.length; i++) {
  1198. const shtItemData = pageData.items[i];
  1199. const tmpPos = yMultiPos[i];
  1200. let startPicIdx = 2;
  1201. for (const sCell of shtItemData[JV.PROP_SIGNATURE_CELLS]) {
  1202. if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1203. private_setSheetDrawingCellData(sCell, tmpPos, startPicIdx, rowOffset);
  1204. startPicIdx++;
  1205. }
  1206. }
  1207. for (const sCell of shtItemData[JV.PROP_WATERMARK_CELLS]) {
  1208. if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1209. private_setSheetDrawingCellData(sCell, tmpPos, startPicIdx, rowOffset);
  1210. startPicIdx++;
  1211. }
  1212. }
  1213. rowOffset += tmpPos.length - 2;
  1214. }
  1215. }
  1216. rst.push('</xdr:wsDr>');
  1217. return rst;
  1218. }
  1219. function writeDrawingsRels(amt, startIdx) {
  1220. const rst = [];
  1221. rst.push('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
  1222. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  1223. for (let idx = 0; idx < amt; idx++) {
  1224. rst.push('<Relationship Id="rId' + (idx + 1) + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/image' + (startIdx + idx + 1) + '.png"/>');
  1225. }
  1226. rst.push('</Relationships>');
  1227. return rst;
  1228. }
  1229. function mergeProperties(orgObj, newObj) {
  1230. const orgPropArr = [];
  1231. const newPropArr = [];
  1232. for (const p in orgObj) {
  1233. orgPropArr.push(p);
  1234. }
  1235. for (const p in newObj) {
  1236. newPropArr.push(p);
  1237. }
  1238. for (let i = 0; i < newPropArr.length; i++) {
  1239. if (orgPropArr.indexOf(newPropArr[i]) < 0) {
  1240. orgObj[newPropArr[i]] = newObj[newPropArr[i]];
  1241. }
  1242. }
  1243. }
  1244. async function resetDummySignature(pageData, roleRel) {
  1245. // 备注:计算草图等其他图形需要额外做些处理
  1246. let dummySignIdx = 0;
  1247. const stampPicKeys = [], stampPicFeatures = [];
  1248. const _resetStampArea = function(stampCell, roleRelItem) {
  1249. const ctrl = pageData[JV.NODE_CONTROL_COLLECTION][stampCell[JV.PROP_CONTROL]];
  1250. const rst = { Left: stampCell.area.Left, Right: stampCell.area.Right, Top: stampCell.area.Top, Bottom: stampCell.area.Bottom };
  1251. let pLeft = stampCell.orgArea.Left,
  1252. pTop = stampCell.orgArea.Top;
  1253. let std_stamp_size_width = STD_COMP_STAMP_SIZE_WIDTH, std_stamp_size_height = STD_COMP_STAMP_SIZE_HEIGHT;
  1254. if (roleRelItem.stampFeature) {
  1255. std_stamp_size_width = parseFloat(roleRelItem.stampFeature.ImageWidth.value);
  1256. std_stamp_size_height = parseFloat(roleRelItem.stampFeature.ImageHeight.value);
  1257. }
  1258. switch (ctrl[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_HORIZON]]) {
  1259. case JV.OUTPUT_ALIGN.H[JV.H_ALIGN_IDX_LEFT]:
  1260. pLeft = stampCell.orgArea.Left;
  1261. break;
  1262. case JV.OUTPUT_ALIGN.H[JV.H_ALIGN_IDX_CENTER]:
  1263. pLeft = (stampCell.orgArea.Left + stampCell.orgArea.Right - std_stamp_size_width) / 2;
  1264. break;
  1265. case JV.OUTPUT_ALIGN.H[JV.H_ALIGN_IDX_RIGHT]:
  1266. pLeft = stampCell.orgArea.Right - std_stamp_size_width;
  1267. break;
  1268. default:break;
  1269. }
  1270. switch (ctrl[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]]) {
  1271. case JV.OUTPUT_ALIGN.H[JV.V_ALIGN_IDX_TOP]:
  1272. pTop = stampCell.orgArea.Top;
  1273. break;
  1274. case JV.OUTPUT_ALIGN.H[JV.V_ALIGN_IDX_CENTER]:
  1275. pTop = (stampCell.orgArea.Top + stampCell.orgArea.Bottom - std_stamp_size_height) / 2;
  1276. break;
  1277. case JV.OUTPUT_ALIGN.H[JV.V_ALIGN_IDX_BOTTOM]:
  1278. pTop = stampCell.orgArea.Bottom - std_stamp_size_height;
  1279. break;
  1280. default:break;
  1281. }
  1282. stampCell.area.Left = pLeft;
  1283. stampCell.area.Top = pTop;
  1284. stampCell.area.Right = pLeft + std_stamp_size_width;
  1285. stampCell.area.Bottom = pTop + std_stamp_size_height;
  1286. // 最后一步,如超过报表范围,则要调整坐标
  1287. // const maxRect = stampCell.maxRect;
  1288. // if (rst.Left < maxRect[0]) {
  1289. // const width = maxRect[0] - rst.Left;
  1290. // rst.Left += width;
  1291. // rst.Right += width;
  1292. // }
  1293. // if (rst.Top < maxRect[1]) {
  1294. // const height = maxRect[1] - rst.Top;
  1295. // rst.Top += height;
  1296. // rst.Bottom += height;
  1297. // }
  1298. // if (rst.Right > maxRect[2]) {
  1299. // const width = maxRect[2] - rst.Right; // 负
  1300. // rst.Left += width;
  1301. // rst.Right += width;
  1302. // }
  1303. // if (rst.Bottom > maxRect[3]) {
  1304. // const height = maxRect[3] - rst.Bottom;
  1305. // rst.Top += height;
  1306. // rst.Bottom += height;
  1307. // }
  1308. return rst;
  1309. };
  1310. for (let pageIdx = 0; pageIdx < pageData.items.length; pageIdx++) {
  1311. const page = pageData.items[pageIdx];
  1312. if (page[JV.PROP_SIGNATURE_CELLS] && page[JV.PROP_SIGNATURE_CELLS].length > 0) {
  1313. for (const signatureCell of page[JV.PROP_SIGNATURE_CELLS]) {
  1314. if (signatureCell.signature_name.indexOf(JV.SIGNATURE_NAME_DUMMY) >= 0) {
  1315. // 表示这是一个其他类型的非原生电子签名图,只是借用signature的处理机制,每个图都是唯一的,所以需要重新给个唯一的新signature_name
  1316. // signatureCell.signature_name = signatureCell.signature_name + '_' + pageIdx + '_' + dummySignIdx;
  1317. signatureCell.signature_name = signatureCell.signature_name + '_' + page.page_seq + '_' + dummySignIdx; // page_seq在分页后都不会变动
  1318. dummySignIdx++;
  1319. const roleRelItem = { type: '用户', sign_path: signatureCell.path, signature_name: signatureCell.signature_name };
  1320. if (signatureCell.isStamp) {
  1321. let stmpIdx = stampPicKeys.indexOf(signatureCell.path);
  1322. if (stampPicKeys.indexOf(signatureCell.path) < 0) {
  1323. stampPicKeys.push(signatureCell.path);
  1324. stmpIdx = stampPicKeys.length - 1;
  1325. const picRes = await getHttpBlobText(signatureCell.path + '?x-oss-process=image/info');
  1326. stampPicFeatures.push(picRes);
  1327. }
  1328. // { "FileSize": {"value": "2514"}, "Format": {"value": "png"}, "ImageHeight": {"value": "94"}, "ImageWidth": {"value": "94"} }
  1329. roleRelItem.stampFeature = stampPicFeatures[stmpIdx];
  1330. // 重新处理cell坐标
  1331. _resetStampArea(signatureCell, roleRelItem);
  1332. }
  1333. roleRel.push(roleRelItem);
  1334. }
  1335. }
  1336. }
  1337. }
  1338. }
  1339. function _createDummyCell(dummyOrgCells, parentPage) {
  1340. for (const signature of dummyOrgCells) {
  1341. if (!signature.isStamp) {
  1342. const dummyCell = {
  1343. font: 'Content',
  1344. control: 'Default',
  1345. style: 'Default_None',
  1346. Value: '',
  1347. area: { Left: 0, Right: 0, Top: 0, Bottom: 0 },
  1348. };
  1349. if (signature.style) dummyCell.style = signature.style;
  1350. dummyCell[JV.PROP_AREA][JV.PROP_LEFT] = signature[JV.PROP_AREA][JV.PROP_LEFT];
  1351. dummyCell[JV.PROP_AREA][JV.PROP_RIGHT] = signature[JV.PROP_AREA][JV.PROP_RIGHT];
  1352. dummyCell[JV.PROP_AREA][JV.PROP_TOP] = signature[JV.PROP_AREA][JV.PROP_TOP];
  1353. dummyCell[JV.PROP_AREA][JV.PROP_BOTTOM] = signature[JV.PROP_AREA][JV.PROP_BOTTOM];
  1354. parentPage[JV.PROP_CELLS].push(dummyCell);
  1355. }
  1356. }
  1357. }
  1358. function createDummySignatureCell(pageData) {
  1359. for (let pageIdx = 0; pageIdx < pageData.items.length; pageIdx++) {
  1360. const page = pageData.items[pageIdx];
  1361. if (page[JV.PROP_SIGNATURE_CELLS] && page[JV.PROP_SIGNATURE_CELLS].length > 0) {
  1362. _createDummyCell(page[JV.PROP_SIGNATURE_CELLS], page);
  1363. }
  1364. if (page[JV.PROP_SIGNATURE_DATE_CELLS] && page[JV.PROP_SIGNATURE_DATE_CELLS].length > 0) {
  1365. _createDummyCell(page[JV.PROP_SIGNATURE_DATE_CELLS], page);
  1366. }
  1367. if (page[JV.PROP_SIGNATURE_AUDIT_CELLS] && page[JV.PROP_SIGNATURE_AUDIT_CELLS].length > 0) {
  1368. _createDummyCell(page[JV.PROP_SIGNATURE_AUDIT_CELLS], page);
  1369. }
  1370. }
  1371. }
  1372. function _checkAndSetSignatureCache(pageData, signKeyArr, signPathArr, roleRel, signSheetIdxArr, isSinglePage) {
  1373. // 备注:电子签名是以图形的方式处理,一页可以有多个签名,多页的签名基本是引用同样的图片,在这里先处理一下,后期统一引用。
  1374. // 另:以后的图片(在电子签名(signature_cells)以外的图片)会单独处理(如计算草图、水印等)
  1375. let rst = false;
  1376. let chkRoles = [];
  1377. if (roleRel instanceof Array) {
  1378. chkRoles = roleRel;
  1379. } else {
  1380. if (roleRel) chkRoles.push(roleRel);
  1381. }
  1382. const _getDupPicPath = function(path) {
  1383. let rst = null;
  1384. for (const sp of signPathArr) {
  1385. if (sp.path === path) {
  1386. rst = sp;
  1387. break;
  1388. }
  1389. }
  1390. return rst;
  1391. };
  1392. for (let pageIdx = 0; pageIdx < pageData.items.length; pageIdx++) {
  1393. const page = pageData.items[pageIdx];
  1394. // 优化调整,不再每页都设置一个,重复的签名只需要一个即可(极端case: 13xx多页的签名文档,导出有1.3G大)
  1395. const curSignIdx = signKeyArr.length - 1;
  1396. signSheetIdxArr[pageIdx] = false;
  1397. if (page[JV.PROP_SIGNATURE_CELLS] && page[JV.PROP_SIGNATURE_CELLS].length > 0) {
  1398. for (const signature of page[JV.PROP_SIGNATURE_CELLS]) {
  1399. // 这里把图片的路径作为key值判断
  1400. // if (true || signKeyArr.indexOf(signature.signature_name) < 0) {
  1401. // console.log(`signature: ${signature.signature_name}`);
  1402. if (signKeyArr.indexOf(signature.signature_name) < 0) {
  1403. // console.log(`signature: ${signature.signature_name}`);
  1404. if (signature.pic) {
  1405. const signPath = { signature_name: signature.signature_name, path: null, pic: null };
  1406. signPathArr.push(signPath);
  1407. signPath.pic = signature.pic; // 历史报表
  1408. signKeyArr.push(signature.signature_name);
  1409. rst = true;
  1410. signSheetIdxArr[pageIdx] = true;
  1411. } else {
  1412. for (const role of chkRoles) {
  1413. if (signature.signature_name === role.signature_name) {
  1414. // console.log('signature.signature_name: ' + signature.signature_name);
  1415. if (role.sign_pic) {
  1416. const signPath = { signature_name: signature.signature_name, path: null, pic: null };
  1417. signPathArr.push(signPath);
  1418. signPath.pic = role.sign_pic;
  1419. signKeyArr.push(signature.signature_name);
  1420. rst = true;
  1421. signSheetIdxArr[pageIdx] = true;
  1422. } else if (role.sign_path) {
  1423. let sPObj = _getDupPicPath(role.sign_path);
  1424. if (sPObj !== null && isSinglePage) { // 只有在isSinglePage为true时,才需要优化签名
  1425. signature.signature_name = sPObj.signature_name; // 重点,如果有相同路径的,修改signature_name!
  1426. rst = true;
  1427. } else {
  1428. const signPath = { signature_name: signature.signature_name, path: null, pic: null };
  1429. signPathArr.push(signPath);
  1430. signPath.path = role.sign_path;
  1431. signKeyArr.push(signature.signature_name);
  1432. rst = true;
  1433. signSheetIdxArr[pageIdx] = true;
  1434. }
  1435. }
  1436. break;
  1437. }
  1438. }
  1439. }
  1440. } else {
  1441. // console.log(`duplicate signature: ${signature.signature_name}`);
  1442. // signSheetIdxArr[pageIdx] = true;
  1443. }
  1444. }
  1445. }
  1446. if (page[JV.PROP_WATERMARK_CELLS] && page[JV.PROP_WATERMARK_CELLS].length > 0) {
  1447. // 水印,单独处理
  1448. const waterMarkCell = page[JV.PROP_WATERMARK_CELLS][0];
  1449. if (signKeyArr.indexOf(waterMarkCell.signature_name) < 0) {
  1450. const signPath = { path: null, pic: null };
  1451. signPathArr.push(signPath);
  1452. signPath.pic = waterMarkCell.pic;
  1453. signPath.path = waterMarkCell.path;
  1454. signKeyArr.push(waterMarkCell.signature_name);
  1455. rst = true;
  1456. signSheetIdxArr[pageIdx] = true;
  1457. } else if (!isSinglePage) {
  1458. signSheetIdxArr[pageIdx] = true;
  1459. }
  1460. }
  1461. }
  1462. return rst;
  1463. }
  1464. const excelExportUtil = {
  1465. exportExcel: async function(pageData, paperSize, fName, options, custSheetNames, custSheetMergeBands, roleRel, callback) {
  1466. const rptOptions = ({ singlePage: false, fileName: 'report' });
  1467. if (options === 'true' || options === true) {
  1468. rptOptions.singlePage = true;
  1469. }
  1470. const isSinglePage = rptOptions.singlePage;
  1471. const sheets = [];
  1472. const signKeyArr = [];
  1473. const signPathArr = [];
  1474. const signSheetIdxArr = []; // 确定哪些sheet有签名(在多表导出的时候,有些表可能没有签名,但其他的表有签名)
  1475. let thisRoleRel = [];
  1476. if (roleRel !== null && roleRel !== undefined) {
  1477. thisRoleRel = JSON.parse(JSON.stringify(roleRel));
  1478. }
  1479. await rptSignatureHelper.resetDummySignature(pageData, thisRoleRel, true); // 把草图转换一下roleRel
  1480. // const hasSignature = false; // 暂时不支持电子签名、草图导出excel
  1481. const hasSignature = _checkAndSetSignatureCache(pageData, signKeyArr, signPathArr, thisRoleRel, signSheetIdxArr, isSinglePage); // 因草图的关系,thisRoleRel是否为null就不是充要的条件
  1482. if (!hasSignature) {
  1483. createDummySignatureCell(pageData);
  1484. }
  1485. if (isSinglePage) {
  1486. // 加分页符(分页符要在外部处理好)-------------------
  1487. _setupPgBrks(pageData);
  1488. // 加分页符结束-------------------
  1489. sheets.push({ sheetName: '全部页' });
  1490. } else {
  1491. if (custSheetNames && custSheetNames.length === pageData.items.length) {
  1492. for (let i = 0; i < pageData.items.length; i++) {
  1493. sheets.push({ sheetName: custSheetNames[i] });
  1494. }
  1495. } else {
  1496. for (let i = 0; i < pageData.items.length; i++) {
  1497. sheets.push({ sheetName: '第' + (i + 1) + '页' });
  1498. }
  1499. }
  1500. }
  1501. // 1.
  1502. let file = '[Content_Types].xml';
  1503. let data = writeContentTypes(sheets, isSinglePage, hasSignature);
  1504. const zip = new JSZip();
  1505. zip.file(file, data.join(''), { compression: 'DEFLATE' });
  1506. // 2.
  1507. const zip_rels = zip.folder('_rels');
  1508. file = '.rels';
  1509. data = writeRootRels();
  1510. zip_rels.file(file, data.join(''), { compression: 'DEFLATE' });
  1511. // 3.
  1512. const zip_docProps = zip.folder('docProps');
  1513. file = 'app.xml';
  1514. data = writeApp(sheets, isSinglePage);
  1515. zip_docProps.file(file, data.join(''), { compression: 'DEFLATE' });
  1516. file = 'core.xml';
  1517. data = writeCore();
  1518. zip_docProps.file(file, data.join(''), { compression: 'DEFLATE' });
  1519. // 4.
  1520. const zip_xl = zip.folder('xl');
  1521. file = 'workbook.xml';
  1522. data = writeXlWorkBook(sheets, isSinglePage);
  1523. zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
  1524. const zip_rels2 = zip_xl.folder('_rels');
  1525. file = 'workbook.xml.rels';
  1526. data = writeXlRels(sheets, isSinglePage);
  1527. zip_rels2.file(file, data.join(''), { compression: 'DEFLATE' });
  1528. // 5.
  1529. const zip_theme = zip_xl.folder('theme');
  1530. file = 'theme1.xml';
  1531. data = writeTheme();
  1532. zip_theme.file(file, data, { compression: 'DEFLATE' });
  1533. if (hasSignature) {
  1534. // 5.1 写所有的image
  1535. const zip_media = zip_xl.folder('media');
  1536. let picIdx = 0;
  1537. // 优化调整,不再每页都设置一个,重复的签名只需要一个即可(极端case: 13xx多页的签名文档,导出有1.3G大)
  1538. for (let pathIdx = 0; pathIdx < signPathArr.length; pathIdx++) {
  1539. picIdx++;
  1540. data = await writeImage(signPathArr[pathIdx].path, signPathArr[pathIdx].pic);
  1541. file = 'image' + picIdx + '.png';
  1542. zip_media.file(file, data, { compression: 'DEFLATE' });
  1543. }
  1544. // 5.2
  1545. const zip_drawings = zip_xl.folder('drawings');
  1546. data = writeDrawings(pageData, signKeyArr, signPathArr, isSinglePage, signSheetIdxArr);
  1547. // data = writeDrawings(pageData, signKeyArr, signPathArr, false, signSheetIdxArr); // 备注:这里根本不需要考虑是否single page
  1548. for (let psIdx = 0; psIdx < data.length; psIdx++) {
  1549. file = 'drawing' + (psIdx + 1) + '.xml';
  1550. zip_drawings.file(file, data[psIdx].join(''), { compression: 'DEFLATE' });
  1551. }
  1552. // 5.3
  1553. const zip_drawings_rels = zip_drawings.folder('_rels');
  1554. let relsIdx = 0;
  1555. let relsImgAmt = 0;
  1556. for (let ssIdx = 0; ssIdx < signKeyArr.length; ssIdx++) {
  1557. data = writeDrawingsRels(signKeyArr.length, relsImgAmt); // 一个drawingX.xml.rels文件与一个drawingX.xml对应
  1558. relsIdx++;
  1559. if (isSinglePage) {
  1560. relsImgAmt += signKeyArr.length;
  1561. }
  1562. file = 'drawing' + relsIdx + '.xml.rels';
  1563. zip_drawings_rels.file(file, data.join(''), { compression: 'DEFLATE' });
  1564. }
  1565. }
  1566. // 6.
  1567. const zip_xl_worksheets = zip_xl.folder('worksheets');
  1568. const sharedStrList = [];
  1569. const stylesObj = {};
  1570. data = writeSheets(pageData, paperSize, sharedStrList, stylesObj, isSinglePage, custSheetMergeBands, hasSignature, signSheetIdxArr);
  1571. if (isSinglePage) {
  1572. for (let i = 0; i < 1; i++) {
  1573. file = 'sheet' + (i + 1) + '.xml';
  1574. zip_xl_worksheets.file(file, data[i].join(''), { compression: 'DEFLATE' });
  1575. }
  1576. } else {
  1577. for (let i = 0; i < data.length; i++) {
  1578. file = 'sheet' + (i + 1) + '.xml';
  1579. zip_xl_worksheets.file(file, data[i].join(''), { compression: 'DEFLATE' });
  1580. }
  1581. }
  1582. // 7.
  1583. if (hasSignature) {
  1584. const zip_xl_worksheets_rels = zip_xl_worksheets.folder('_rels');
  1585. if (isSinglePage) {
  1586. data = writeWorkSheetRels(1);
  1587. file = 'sheet1.xml.rels';
  1588. zip_xl_worksheets_rels.file(file, data.join(''), { compression: 'DEFLATE' });
  1589. } else {
  1590. let ssIdx = 0;
  1591. for (let i = 0; i < data.length; i++) {
  1592. if (signSheetIdxArr[i]) {
  1593. ssIdx++;
  1594. const ssData = writeWorkSheetRels(ssIdx);
  1595. file = 'sheet' + (i + 1) + '.xml.rels';
  1596. zip_xl_worksheets_rels.file(file, ssData.join(''), { compression: 'DEFLATE' });
  1597. }
  1598. }
  1599. }
  1600. }
  1601. file = 'sharedStrings.xml';
  1602. data = writeSharedString(sharedStrList);
  1603. zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
  1604. file = 'styles.xml';
  1605. data = writeStyles(stylesObj, hasSignature);
  1606. zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
  1607. zip.generateAsync({type: 'blob'}).then(function(content){
  1608. saveAs(content, `${fName}.xlsx`); // 页面需要引用 FileSaver.js 才有这个方法
  1609. });
  1610. },
  1611. exportExcelInOneBook: async function(pageDataArray, paperSize, fName, roleRelArr, callback) {
  1612. const me = this;
  1613. const newPageData = {};
  1614. // 1. 重新编排一下数据,把一份报表的pageData合并到一起作为一个Sheet输出(需要重新调整数据纵向坐标),多份报表数据就形成多个Sheet
  1615. // -- 简单来说,就是重新包装数据
  1616. try {
  1617. // 1.1 newPageData外围属性
  1618. const newContrl = {};
  1619. const newFont = {};
  1620. const newStyle = {};
  1621. const custMergeBands = [];
  1622. for (let i = 0; i < pageDataArray.length; i++) {
  1623. mergeProperties(newContrl, pageDataArray[i][JV.NODE_CONTROL_COLLECTION]);
  1624. mergeProperties(newFont, pageDataArray[i][JV.NODE_FONT_COLLECTION]);
  1625. mergeProperties(newStyle, pageDataArray[i][JV.NODE_STYLE_COLLECTION]);
  1626. }
  1627. newPageData[JV.NODE_CONTROL_COLLECTION] = newContrl;
  1628. newPageData[JV.NODE_FONT_COLLECTION] = newFont;
  1629. newPageData[JV.NODE_STYLE_COLLECTION] = newStyle;
  1630. newPageData[JV.NODE_PAGE_INFO] = pageDataArray[0][JV.NODE_PAGE_INFO];
  1631. newPageData[JV.BAND_PROP_MERGE_BAND] = pageDataArray[0][JV.BAND_PROP_MERGE_BAND];
  1632. // 1.2 重新设置pageDataArray的各个cell的Top/Bottom坐标
  1633. const sheetNames = [];
  1634. const newPagePos = [];
  1635. for (let i = 0; i < pageDataArray.length; i++) {
  1636. let offsetY = 0;
  1637. const mergeBand = {};
  1638. custMergeBands.push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND]);
  1639. // 备注:不同的报表有可能有不同的边框,如封面表就是无边框的
  1640. mergeBand[JV.PROP_LEFT] = [];
  1641. mergeBand[JV.PROP_RIGHT] = [];
  1642. mergeBand[JV.PROP_TOP] = [];
  1643. mergeBand[JV.PROP_BOTTOM] = [];
  1644. newPagePos.push(mergeBand);
  1645. mergeBand[JV.PROP_LEFT].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_LEFT]);
  1646. mergeBand[JV.PROP_RIGHT].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_RIGHT]);
  1647. sheetNames.push(pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_MAIN_INFO_RPT_NAME]);
  1648. // 加分页符(分页符要在外部处理好)-------------------
  1649. _setupPgBrks(pageDataArray[i]);
  1650. // 加分页符结束-------------------
  1651. let maxY = 0;
  1652. let minY = 100000;
  1653. const checkAndResetCellYPos = function(cells) {
  1654. if (cells && cells.length > 0) {
  1655. for (const cell of cells) {
  1656. if (maxY < cell[JV.PROP_AREA][JV.PROP_BOTTOM]) {
  1657. maxY = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  1658. }
  1659. if (minY > cell[JV.PROP_AREA][JV.PROP_TOP]) {
  1660. minY = cell[JV.PROP_AREA][JV.PROP_TOP];
  1661. }
  1662. cell[JV.PROP_AREA][JV.PROP_BOTTOM] += offsetY;
  1663. cell[JV.PROP_AREA][JV.PROP_TOP] += offsetY;
  1664. }
  1665. }
  1666. };
  1667. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1668. maxY = 0;
  1669. minY = 100000;
  1670. if (pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS]) {
  1671. let pos = pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_TOP][0] + offsetY;
  1672. mergeBand[JV.PROP_TOP].push(pos);
  1673. pos = pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_BOTTOM][0] + offsetY;
  1674. mergeBand[JV.PROP_BOTTOM].push(pos);
  1675. } else if (pageDataArray[i].items[j][JV.PROP_PAGE_MERGE_BORDER]) {
  1676. let pos = pageDataArray[i].items[j][JV.PROP_PAGE_MERGE_BORDER][JV.PROP_TOP] + offsetY;
  1677. mergeBand[JV.PROP_TOP].push(pos);
  1678. pos = pageDataArray[i].items[j][JV.PROP_PAGE_MERGE_BORDER][JV.PROP_BOTTOM] + offsetY;
  1679. mergeBand[JV.PROP_BOTTOM].push(pos);
  1680. } else {
  1681. mergeBand[JV.PROP_TOP].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_TOP] + offsetY);
  1682. mergeBand[JV.PROP_BOTTOM].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_BOTTOM] + offsetY);
  1683. }
  1684. // 1.2.1 重新设置普通cells的Top Bottom坐标
  1685. checkAndResetCellYPos(pageDataArray[i].items[j][JV.PROP_CELLS]);
  1686. // 1.2.2 重新设置电子签名cells的Top Bottom坐标
  1687. checkAndResetCellYPos(pageDataArray[i].items[j][JV.PROP_SIGNATURE_CELLS]);
  1688. // 1.2.3 重新设置电子签名日期cells的Top Bottom坐标
  1689. checkAndResetCellYPos(pageDataArray[i].items[j][JV.PROP_SIGNATURE_DATE_CELLS]);
  1690. // 1.2.4 重置电子签名审核意见cells的Top Bottom坐标
  1691. checkAndResetCellYPos(pageDataArray[i].items[j][JV.PROP_SIGNATURE_AUDIT_CELLS]);
  1692. // 1.2.5 重置水印的Top Bottom坐标
  1693. checkAndResetCellYPos(pageDataArray[i].items[j][JV.PROP_WATERMARK_CELLS]);
  1694. 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;
  1695. offsetY += (maxY - minY);
  1696. if (bottomGap > 10) {
  1697. offsetY += (bottomGap - 10);
  1698. }
  1699. }
  1700. }
  1701. // 2. newPageData的items属性及相关分页符
  1702. newPageData.items = [];
  1703. newPageData.pageBreaks = [];
  1704. for (let i = 0; i < pageDataArray.length; i++) {
  1705. const pageItem = {};
  1706. pageItem[JV.PROP_PAGE_SEQ] = i + 1;
  1707. pageItem[JV.PROP_CELLS] = [];
  1708. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1709. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_CELLS].length; k++) {
  1710. pageItem[JV.PROP_CELLS].push(pageDataArray[i].items[j][JV.PROP_CELLS][k]);
  1711. }
  1712. }
  1713. pageItem[JV.PROP_SIGNATURE_CELLS] = [];
  1714. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1715. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_SIGNATURE_CELLS].length; k++) {
  1716. pageItem[JV.PROP_SIGNATURE_CELLS].push(pageDataArray[i].items[j][JV.PROP_SIGNATURE_CELLS][k]);
  1717. }
  1718. }
  1719. pageItem[JV.PROP_SIGNATURE_DATE_CELLS] = [];
  1720. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1721. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_SIGNATURE_DATE_CELLS].length; k++) {
  1722. pageItem[JV.PROP_SIGNATURE_DATE_CELLS].push(pageDataArray[i].items[j][JV.PROP_SIGNATURE_DATE_CELLS][k]);
  1723. }
  1724. }
  1725. pageItem[JV.PROP_SIGNATURE_AUDIT_CELLS] = [];
  1726. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1727. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_SIGNATURE_AUDIT_CELLS].length; k++) {
  1728. pageItem[JV.PROP_SIGNATURE_AUDIT_CELLS].push(pageDataArray[i].items[j][JV.PROP_SIGNATURE_AUDIT_CELLS][k]);
  1729. }
  1730. }
  1731. pageItem[JV.PROP_WATERMARK_CELLS] = [];
  1732. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1733. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_WATERMARK_CELLS].length; k++) {
  1734. pageItem[JV.PROP_WATERMARK_CELLS].push(pageDataArray[i].items[j][JV.PROP_WATERMARK_CELLS][k]);
  1735. }
  1736. }
  1737. newPagePos[i][JV.NODE_PAGE_SIZE] = pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE];
  1738. pageItem[JV.PAGE_SPECIAL_MERGE_POS] = newPagePos[i];
  1739. newPageData.items.push(pageItem);
  1740. // 重新分配分页符()------------------
  1741. if (pageDataArray[i].pageBreaks.length === 1) {
  1742. newPageData.pageBreaks.push(pageDataArray[i].pageBreaks[0]);
  1743. } else {
  1744. newPageData.pageBreaks.push([]);
  1745. }
  1746. // 重新分配分页符结束------------------
  1747. }
  1748. // 3. everything is ok, then call me
  1749. await me.exportExcel(newPageData, paperSize, fName, 'false', sheetNames, custMergeBands, roleRelArr, callback);
  1750. } catch (e) {
  1751. console.log(e);
  1752. }
  1753. }
  1754. };