rpt_jsexcel.js 90 KB

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