rpt_jsexcel.js 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  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. for (let i = 0; i < pageData.items.length; i++) {
  338. const page = pageData.items[i];
  339. const maxBottomArr = [];
  340. preAnalyzePos(pageData, page, [], maxBottomArr, []); // 这里统一分析坐标方法,要错的话,大家都错
  341. pgBrkIdx += maxBottomArr.length - 2; // preAnalyzePos方法会多一个0
  342. pgBrk.push(pgBrkIdx);
  343. }
  344. pageData.pageBreaks.push(pgBrk);
  345. }
  346. function writeSheets(pageData, paperSize, sharedStrList, stylesObj, isSinglePage, custSheetMergeBands, hasSignature, signSheetIdxArr) {
  347. const rst = [];
  348. const private_pushDftFont = function() {
  349. const font = {};
  350. if (!(stylesObj.fonts)) {
  351. stylesObj.fonts = [];
  352. }
  353. font[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]] = '宋体'; // font name
  354. font.size = 12;
  355. font.charset = 134;
  356. font.colorIdx = '8';
  357. stylesObj.fonts.push(font);
  358. };
  359. const private_buildFirstDftStyle = function() {
  360. stylesObj.cellXfs = [];
  361. stylesObj.borders = [];
  362. const fontId = 0;
  363. const borderId = 0;
  364. const border = {};
  365. border[JV.PROP_LEFT] = {};
  366. border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = 0;
  367. border[JV.PROP_RIGHT] = {};
  368. border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = 0;
  369. border[JV.PROP_TOP] = {};
  370. border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = 0;
  371. border[JV.PROP_BOTTOM] = {};
  372. border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = 0;
  373. stylesObj.borders.push(border);
  374. const cellControl = pageData[JV.NODE_CONTROL_COLLECTION].Default;
  375. const sheetControl = {};
  376. sheetControl.fontId = fontId;
  377. sheetControl.borderId = borderId;
  378. sheetControl.fontAngle = 0;
  379. for (let i = 0; i < JV.CONTROL_PROPS.length; i++) {
  380. sheetControl[JV.CONTROL_PROPS[i]] = cellControl[JV.CONTROL_PROPS[i]];
  381. }
  382. stylesObj.cellXfs.push(sheetControl);
  383. };
  384. private_pushDftFont();
  385. private_buildFirstDftStyle();
  386. let sheetIdx = 0;
  387. if (isSinglePage) {
  388. rst.push(writeSheet(pageData, null, paperSize, sharedStrList, stylesObj, null, hasSignature, sheetIdx, signSheetIdxArr, true, -1));
  389. } else {
  390. for (let i = 0; i < pageData.items.length; i++) {
  391. let appointedMergeBand = null;
  392. if (custSheetMergeBands && custSheetMergeBands.length > i) {
  393. appointedMergeBand = custSheetMergeBands[i];
  394. }
  395. rst.push(writeSheet(pageData, pageData.items[i], paperSize, sharedStrList, stylesObj, appointedMergeBand, hasSignature, sheetIdx, signSheetIdxArr, i === 0, i));
  396. sheetIdx++;
  397. }
  398. }
  399. return rst;
  400. }
  401. function getAndSetProperSignatureArea(cell, cellControl) {
  402. const area = getProperSignatureArea(cell, cellControl, 0, 0, JV);
  403. if (!!cell.isStamp) {
  404. area[JV.IDX_LEFT] = cell[JV.PROP_AREA][JV.PROP_LEFT];
  405. area[JV.IDX_TOP] = cell[JV.PROP_AREA][JV.PROP_TOP];
  406. area[JV.IDX_RIGHT] = cell[JV.PROP_AREA][JV.PROP_RIGHT];
  407. area[JV.IDX_BOTTOM] = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  408. }
  409. return area;
  410. }
  411. function preAnalyzePos(pageData, sheetData, xPos, yPos, yMultiPos) {
  412. let cell;
  413. let pos;
  414. const private_array_sort = function(i1, i2) {
  415. return (i1 - i2);
  416. };
  417. const private_set_cell_pos = function(theCell, theXPos, theYPos) {
  418. pos = theCell[JV.PROP_AREA][JV.PROP_LEFT];
  419. if (theXPos.indexOf(pos) < 0) theXPos.push(pos);
  420. pos = theCell[JV.PROP_AREA][JV.PROP_RIGHT];
  421. if (theXPos.indexOf(pos) < 0) theXPos.push(pos);
  422. pos = theCell[JV.PROP_AREA][JV.PROP_TOP];
  423. if (theYPos.indexOf(pos) < 0) theYPos.push(pos);
  424. pos = theCell[JV.PROP_AREA][JV.PROP_BOTTOM];
  425. if (theYPos.indexOf(pos) < 0) theYPos.push(pos);
  426. };
  427. const private_set_cell_pos2 = function(area, theXPos, theYPos) {
  428. pos = area[0];
  429. if (theXPos.indexOf(pos) < 0) theXPos.push(pos);
  430. pos = area[2];
  431. if (theXPos.indexOf(pos) < 0) theXPos.push(pos);
  432. pos = area[1];
  433. if (theYPos.indexOf(pos) < 0) theYPos.push(pos);
  434. pos = area[3];
  435. if (theYPos.indexOf(pos) < 0) theYPos.push(pos);
  436. };
  437. const self_analyze_sheet_pos = function(theShtData, theXPos, theYPos) {
  438. for (let i = 0; i < theShtData[JV.PROP_CELLS].length; i++) {
  439. cell = theShtData[JV.PROP_CELLS][i];
  440. private_set_cell_pos(cell, theXPos, theYPos);
  441. }
  442. for (let i = 0; i < theShtData[JV.PROP_INTERACT_CELLS].length; i++) {
  443. cell = theShtData[JV.PROP_INTERACT_CELLS][i];
  444. private_set_cell_pos(cell, theXPos, theYPos);
  445. }
  446. for (let i = 0; i < theShtData[JV.PROP_SIGNATURE_CELLS].length; i++) {
  447. cell = theShtData[JV.PROP_SIGNATURE_CELLS][i];
  448. private_set_cell_pos(cell, theXPos, theYPos);
  449. let cellControl;
  450. if (typeof cell[JV.PROP_CONTROL] === 'string') {
  451. cellControl = pageData[JV.NODE_CONTROL_COLLECTION][cell[JV.PROP_CONTROL]];
  452. } else {
  453. cellControl = cell[JV.PROP_CONTROL];
  454. }
  455. const area = getAndSetProperSignatureArea(cell, cellControl);
  456. private_set_cell_pos2(area, theXPos, theYPos);
  457. }
  458. for (let i = 0; i < theShtData[JV.PROP_SIGNATURE_DATE_CELLS].length; i++) {
  459. cell = theShtData[JV.PROP_SIGNATURE_DATE_CELLS][i];
  460. private_set_cell_pos(cell, theXPos, theYPos);
  461. }
  462. for (let i = 0; i < theShtData[JV.PROP_SIGNATURE_AUDIT_CELLS].length; i++) {
  463. cell = theShtData[JV.PROP_SIGNATURE_AUDIT_CELLS][i];
  464. private_set_cell_pos(cell, theXPos, theYPos);
  465. }
  466. for (let i = 0; i < theShtData[JV.PROP_WATERMARK_CELLS].length; i++) {
  467. cell = theShtData[JV.PROP_WATERMARK_CELLS][i];
  468. // private_set_cell_pos(cell, theXPos, theYPos);
  469. let cellControl;
  470. if (typeof cell[JV.PROP_CONTROL] === 'string') {
  471. cellControl = pageData[JV.NODE_CONTROL_COLLECTION][cell[JV.PROP_CONTROL]];
  472. } else {
  473. cellControl = cell[JV.PROP_CONTROL];
  474. }
  475. const area = getAndSetProperSignatureArea(cell, cellControl);
  476. private_set_cell_pos2(area, theXPos, theYPos);
  477. }
  478. };
  479. xPos.push(0);
  480. if (sheetData) {
  481. // current sheet data
  482. // console.log('preAnalyzePos not single');
  483. yPos.push(0);
  484. self_analyze_sheet_pos(sheetData, xPos, yPos);
  485. xPos.sort(private_array_sort);
  486. yPos.sort(private_array_sort);
  487. } else {
  488. // total data in one sheet
  489. 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);
  490. // console.log('preAnalyzePos isSingle start');
  491. for (const shtItemData of pageData.items) {
  492. const tmpPos = [];
  493. tmpPos.push(0);
  494. self_analyze_sheet_pos(shtItemData, xPos, tmpPos);
  495. tmpPos.sort(private_array_sort);
  496. if (marginBottomPos - tmpPos[tmpPos.length - 1] > 10) {
  497. // 此逻辑是为了防止打印跨页(假设有些报表模板高度设置离底部margin还好远,导出excel后预览时会发现跨页现象(即下一页的某几行数据会挪到前一页来预览))
  498. tmpPos.push(marginBottomPos - 10);
  499. }
  500. // console.log('preAnalyzePos push to yMultiPos');
  501. yMultiPos.push(tmpPos);
  502. }
  503. xPos.sort(private_array_sort);
  504. yPos.splice(0, yPos.length);
  505. for (const p of yMultiPos[0]) {
  506. yPos.push(p);
  507. }
  508. }
  509. }
  510. function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, appointedMergeBand, hasSignature, sheetIdx, signSheetIdxArr, isFirstSheet, pgBrkIdx) {
  511. const rst = [];
  512. const xPos = [];
  513. const yPos = [];
  514. const yMultiPos = [];
  515. let currentMergeBorder = null;
  516. const headerStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  517. let currentPageMergePos = null; // 在 JV.PAGING_OPTION_INFINITY 场合应用
  518. const private_getCellIdxStr = function(idx) {
  519. let rst = 'A';
  520. if (idx < 26) {
  521. rst = headerStr[idx];
  522. } else if (idx < 26 * 26 + 26) {
  523. const ti = Math.floor(idx / 26);
  524. const tj = idx % 26;
  525. rst = headerStr[ti - 1] + headerStr[tj];
  526. } else if (idx < 26 * 26 * 26 + 26) {
  527. const ti = Math.floor(idx / (26 * 26));
  528. const tj = Math.floor((idx - ti * 26 * 26) / 26);
  529. const tk = idx % 26;
  530. rst = headerStr[ti - 1] + headerStr[tj - 1] + headerStr[tk];
  531. }
  532. return rst;
  533. };
  534. const private_getSharedStrIdx = function(val) {
  535. let strVal = val;
  536. if (val === null || val === undefined || (typeof val === 'number' && isNaN(val))) {
  537. strVal = '';
  538. }
  539. let rst = sharedStrList.indexOf(strVal);
  540. if (rst < 0) {
  541. sharedStrList.push(strVal);
  542. rst = sharedStrList.length - 1;
  543. }
  544. return rst;
  545. };
  546. const private_getFontId = function(cell) {
  547. let rst = 0;
  548. let hasFont = false;
  549. if (!(stylesObj.fonts)) {
  550. stylesObj.fonts = [];
  551. // for (let i = 0; i < sheetData.font_collection)
  552. }
  553. let sheetFont = null;
  554. if (typeof cell[JV.PROP_FONT] === 'string') {
  555. sheetFont = pageData[JV.NODE_FONT_COLLECTION][cell[JV.PROP_FONT]];
  556. } else {
  557. sheetFont = cell[JV.PROP_FONT] || pageData[JV.NODE_FONT_COLLECTION]['Content'];
  558. }
  559. for (let i = 0; i < stylesObj.fonts.length; i++) {
  560. const font = stylesObj.fonts[i];
  561. if (sheetFont) {
  562. 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)
  563. && 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]]
  564. && font[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]] === sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]]) {
  565. hasFont = true;
  566. rst = i;
  567. break;
  568. }
  569. } else {
  570. break;
  571. }
  572. }
  573. if (!hasFont) {
  574. const font = {};
  575. font[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]] = sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]]; // font name
  576. // font.size = Math.round(sheetFont[JV.FONT_PROPS[1]] * 3 / 4);
  577. font.size = Math.floor(sheetFont[JV.FONT_PROPS[1]] * 3 / 4);
  578. font.charset = 134;
  579. font.colorIdx = '8';
  580. font[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] = sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]]; // font bold
  581. font[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]] = sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]]; // font italic
  582. font[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]] = sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]]; // font underline
  583. stylesObj.fonts.push(font);
  584. rst = stylesObj.fonts.length - 1;
  585. }
  586. return rst;
  587. };
  588. const private_chkAndGetMergeLine = function(cell, sheetBorder, borderStr, needFurtherChk) {
  589. let rst = 0;
  590. const mergeBorder = currentMergeBorder;
  591. let mergeBand = pageData[JV.BAND_PROP_MERGE_BAND];
  592. if (appointedMergeBand !== null) {
  593. mergeBand = appointedMergeBand;
  594. }
  595. if (sheetBorder[borderStr] && sheetBorder[borderStr][JV.PROP_LINE_WEIGHT] !== undefined) {
  596. rst = sheetBorder[borderStr][JV.PROP_LINE_WEIGHT];
  597. }
  598. if (currentPageMergePos) {
  599. const side = currentPageMergePos[borderStr];
  600. if (side.indexOf(cell[JV.PROP_AREA][borderStr]) >= 0) {
  601. if (needFurtherChk) {
  602. const topSide = currentPageMergePos[JV.PROP_TOP];
  603. const bottomSide = currentPageMergePos[JV.PROP_BOTTOM];
  604. for (let i = 0; i < topSide.length; i++) {
  605. if (cell[JV.PROP_AREA][JV.PROP_TOP] >= topSide[i]) {
  606. if (cell[JV.PROP_AREA][JV.PROP_BOTTOM] <= bottomSide[i]) {
  607. const destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
  608. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  609. break;
  610. }
  611. } else {
  612. break;
  613. }
  614. }
  615. } else {
  616. const destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
  617. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  618. }
  619. }
  620. } else {
  621. if (mergeBorder && mergeBorder[borderStr] && cell[JV.PROP_AREA][borderStr] === mergeBorder[borderStr]) {
  622. const destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
  623. if (needFurtherChk) {
  624. if (cell[JV.PROP_AREA][JV.PROP_TOP] >= mergeBorder[JV.PROP_TOP] &&
  625. cell[JV.PROP_AREA][JV.PROP_BOTTOM] <= mergeBorder[JV.PROP_BOTTOM]) {
  626. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  627. }
  628. } else {
  629. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  630. }
  631. }
  632. }
  633. return parseInt(rst);
  634. };
  635. const private_checkBorder = function(cell, border, sheetBorder) {
  636. let rst = true;
  637. const borderLineWidths = [];
  638. const sheetBorderLineWidths = [];
  639. borderLineWidths.push(border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]);
  640. borderLineWidths.push(border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]);
  641. borderLineWidths.push(border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]);
  642. borderLineWidths.push(border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]);
  643. if (sheetBorder[JV.PROP_LEFT] && sheetBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]) {
  644. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_LEFT, true));
  645. } else {
  646. sheetBorderLineWidths.push(0);
  647. }
  648. if (sheetBorder[JV.PROP_RIGHT] && sheetBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]) {
  649. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_RIGHT, true));
  650. } else {
  651. sheetBorderLineWidths.push(0);
  652. }
  653. if (sheetBorder[JV.PROP_TOP] && sheetBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]) {
  654. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_TOP, false));
  655. } else {
  656. sheetBorderLineWidths.push(0);
  657. }
  658. if (sheetBorder[JV.PROP_BOTTOM] && sheetBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]) {
  659. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_BOTTOM, false));
  660. } else {
  661. sheetBorderLineWidths.push(0);
  662. }
  663. for (let i = 0; i < 4; i++) {
  664. if (borderLineWidths[i] != sheetBorderLineWidths[i]) {
  665. rst = false;
  666. break;
  667. }
  668. }
  669. return rst;
  670. };
  671. const private_getIniBorder = function() {
  672. const rst = {};
  673. rst[JV.PROP_LEFT] = {};
  674. rst[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = 0;
  675. rst[JV.PROP_RIGHT] = {};
  676. rst[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = 0;
  677. rst[JV.PROP_TOP] = {};
  678. rst[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = 0;
  679. rst[JV.PROP_BOTTOM] = {};
  680. rst[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = 0;
  681. return rst;
  682. };
  683. const private_getBorderId = function(cell) {
  684. let rst = 0;
  685. let hasBorder = false;
  686. if (!(stylesObj.borders)) {
  687. stylesObj.borders = [];
  688. }
  689. const sheetBorder = pageData[JV.NODE_STYLE_COLLECTION][cell.style];
  690. const mergedBorder = private_getIniBorder();
  691. mergedBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_LEFT, true);
  692. mergedBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_RIGHT, true);
  693. mergedBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_TOP, false);
  694. mergedBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_BOTTOM, false);
  695. for (let i = 0; i < stylesObj.borders.length; i++) {
  696. const border = stylesObj.borders[i];
  697. if (private_checkBorder(cell, border, mergedBorder)) {
  698. hasBorder = true;
  699. rst = i;
  700. break;
  701. }
  702. }
  703. if (!hasBorder) {
  704. stylesObj.borders.push(mergedBorder);
  705. rst = stylesObj.borders.length - 1;
  706. }
  707. return rst;
  708. };
  709. const private_checkControl = function(cellControl, sheetControl) {
  710. let rst = true;
  711. for (let i = 0; i < JV.CONTROL_PROPS.length; i++) {
  712. if (cellControl[JV.CONTROL_PROPS[i]] !== sheetControl[JV.CONTROL_PROPS[i]]) {
  713. rst = false;
  714. break;
  715. }
  716. }
  717. return rst;
  718. };
  719. const private_getStyleId = function(cell) {
  720. let rst = 1;
  721. let hasStyle = false;
  722. if (!(stylesObj.cellXfs)) stylesObj.cellXfs = [];
  723. const fontId = private_getFontId(cell);
  724. let fontAngle = 0;
  725. if (typeof cell[JV.PROP_FONT] === 'string') {
  726. fontAngle = parseInt(pageData[JV.NODE_FONT_COLLECTION][cell[JV.PROP_FONT]].FontAngle);
  727. } else {
  728. fontAngle = parseInt(cell[JV.PROP_FONT].FontAngle);
  729. // fontAngle = parseInt(cell[JV.PROP_FONT]?.FontAngle || 0);
  730. }
  731. const borderId = private_getBorderId(cell);
  732. let cellControl = null;
  733. if (typeof cell[JV.PROP_CONTROL] === 'string') {
  734. cellControl = pageData[JV.NODE_CONTROL_COLLECTION][cell[JV.PROP_CONTROL]];
  735. } else {
  736. cellControl = cell[JV.PROP_CONTROL];
  737. }
  738. for (let i = 0; i < stylesObj.cellXfs.length; i++) {
  739. const sheetControl = stylesObj.cellXfs[i];
  740. if (sheetControl.fontId === fontId && sheetControl.borderId === borderId) {
  741. if (private_checkControl(cellControl, sheetControl)) {
  742. rst = i;
  743. hasStyle = true;
  744. break;
  745. }
  746. }
  747. }
  748. if (!hasStyle) {
  749. const sheetControl = {};
  750. sheetControl.fontId = fontId;
  751. sheetControl.borderId = borderId;
  752. sheetControl.fontAngle = fontAngle;
  753. for (let i = 0; i < JV.CONTROL_PROPS.length; i++) {
  754. sheetControl[JV.CONTROL_PROPS[i]] = cellControl[JV.CONTROL_PROPS[i]];
  755. }
  756. stylesObj.cellXfs.push(sheetControl);
  757. rst = stylesObj.cellXfs.length - 1;
  758. }
  759. return rst;
  760. };
  761. const private_setCols = function() {
  762. // remark: 1 excel unit width = 2.117 mm
  763. rst.push('<cols>');
  764. let w = 0;
  765. for (let i = 1; i < xPos.length - 1; i++) {
  766. w = (xPos[i + 1] - xPos[i]) / DPI * 25.4 / 2.117;
  767. w = Math.round(w * 1000) / 1000;
  768. rst.push('<col min="' + i + '" max="' + i + '" width="' + w + '" customWidth="1"/>');
  769. }
  770. rst.push('<col min="' + (xPos.length - 1) + '" max="' + (xPos.length - 1) + '" width="' + 10 + '" customWidth="1"/>');
  771. rst.push('</cols>');
  772. };
  773. const private_setMergedCells = function() {
  774. let cnt = 0;
  775. rst.push('<mergeCells count="?">');
  776. const startIdx = rst.length - 1;
  777. const self_setMergedCells = function(cells, theYPos, offsetY) {
  778. for (let i = 0; i < cells.length; i++) {
  779. const cell = cells[i];
  780. if (!cell.isStamp) {
  781. const idxR = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_RIGHT]);
  782. const idxL = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_LEFT]);
  783. const idxB = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_BOTTOM]);
  784. const idxT = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_TOP]);
  785. if (idxR - idxL > 1 || idxB - idxT > 1) {
  786. rst.push('<mergeCell ref="' + private_getCellIdxStr(idxL - 1) + (idxT + offsetY) + ':' + private_getCellIdxStr(idxR - 2) + (idxB - 1 + offsetY) + '"/>');
  787. cnt++;
  788. }
  789. }
  790. }
  791. };
  792. if (sheetData) {
  793. // self_setMergedCells(sheetData, yPos, 0);
  794. self_setMergedCells(sheetData[JV.PROP_CELLS], yPos, 0);
  795. self_setMergedCells(sheetData[JV.PROP_INTERACT_CELLS], yPos, 0);
  796. if (sheetData[JV.PROP_SIGNATURE_DATE_CELLS]) self_setMergedCells(sheetData[JV.PROP_SIGNATURE_DATE_CELLS], yPos, 0);
  797. if (sheetData[JV.PROP_SIGNATURE_AUDIT_CELLS]) self_setMergedCells(sheetData[JV.PROP_SIGNATURE_AUDIT_CELLS], yPos, 0);
  798. } else {
  799. let osY = 0;
  800. for (let i = 0; i < pageData.items.length; i++) {
  801. const shtItemData = pageData.items[i];
  802. const tmpPos = yMultiPos[i];
  803. // self_setMergedCells(shtItemData, tmpPos, osY);
  804. self_setMergedCells(shtItemData[JV.PROP_CELLS], tmpPos, osY);
  805. self_setMergedCells(shtItemData[JV.PROP_INTERACT_CELLS], tmpPos, osY);
  806. if (shtItemData[JV.PROP_SIGNATURE_DATE_CELLS]) self_setMergedCells(shtItemData[JV.PROP_SIGNATURE_DATE_CELLS], tmpPos, osY);
  807. if (shtItemData[JV.PROP_SIGNATURE_AUDIT_CELLS]) self_setMergedCells(shtItemData[JV.PROP_SIGNATURE_AUDIT_CELLS], tmpPos, osY);
  808. osY += tmpPos.length - 2;
  809. }
  810. }
  811. rst[startIdx] = '<mergeCells count="' + cnt + '">';
  812. rst.push('</mergeCells>');
  813. };
  814. const private_setSheetData = function() {
  815. // remark: 1 excel unit height = 0.3612 mm
  816. rst.push('<sheetData>');
  817. const spanX = xPos.length - 2;
  818. let cellIdx = 0;
  819. let h = 0;
  820. const self_create_cell = function(cells, rows, theYPos) {
  821. let rowIdx1 = 0;
  822. let colIdx1 = 0;
  823. let rowIdx2 = 0;
  824. let colIdx2 = 0;
  825. for (let cIdx = 0; cIdx < cells.length; cIdx++) {
  826. const styleIdx = private_getStyleId(cells[cIdx]);
  827. rowIdx1 = theYPos.indexOf(cells[cIdx][JV.PROP_AREA][JV.PROP_TOP]);
  828. colIdx1 = xPos.indexOf(cells[cIdx][JV.PROP_AREA][JV.PROP_LEFT]);
  829. if (rowIdx1 <= rows.length) {
  830. let cellObj = rows[rowIdx1 - 1].items[colIdx1 - 1];
  831. cellObj.s = styleIdx;
  832. cellObj.isBlank = false;
  833. if (!(stringUtil.isEmptyString(cells[cIdx][JV.PROP_VALUE]))) {
  834. const valIdx = private_getSharedStrIdx(cells[cIdx][JV.PROP_VALUE]);
  835. cellObj.v = valIdx;
  836. }
  837. rowIdx2 = theYPos.indexOf(cells[cIdx][JV.PROP_AREA][JV.PROP_BOTTOM]);
  838. colIdx2 = xPos.indexOf(cells[cIdx][JV.PROP_AREA][JV.PROP_RIGHT]);
  839. if ((rowIdx2 - rowIdx1 > 1) || (colIdx2 - colIdx1 > 1)) {
  840. for (let i = 0; i < rowIdx2 - rowIdx1; i++) {
  841. for (let j = 0; j < colIdx2 - colIdx1; j++) {
  842. if (i === 0 && j === 0) continue;
  843. cellObj = rows[rowIdx1 - 1 + i].items[colIdx1 - 1 + j];
  844. cellObj.s = styleIdx;
  845. cellObj.isBlank = true;
  846. }
  847. }
  848. }
  849. }
  850. }
  851. };
  852. const self_setDataEx = function(theShtData, theYPos, rowOffset) {
  853. const rows = [];
  854. // 1. build full set of blank rows/cells
  855. for (let i = 1; i < theYPos.length - 1; i++) {
  856. const rowObj = {};
  857. h = (theYPos[i + 1] - theYPos[i]) / DPI * 25.4 / 0.3612;
  858. h = Math.round(h * 1000) / 1000;
  859. rowObj.height = h;
  860. rowObj.r = i + rowOffset;
  861. rowObj.items = [];
  862. rows.push(rowObj);
  863. for (let j = 1; j < xPos.length - 1; j++) {
  864. const colIdxStr = private_getCellIdxStr(j - 1);
  865. const cellObj = {};
  866. cellObj.r = colIdxStr + (i + rowOffset);
  867. cellObj.s = 0;
  868. cellObj.isBlank = true;
  869. rows[i - 1].items.push(cellObj);
  870. }
  871. }
  872. // 2. then fill up the cell style-ids and values
  873. self_create_cell(theShtData[JV.PROP_CELLS], rows, theYPos);
  874. self_create_cell(theShtData[JV.PROP_INTERACT_CELLS], rows, theYPos);
  875. self_create_cell(theShtData[JV.PROP_SIGNATURE_DATE_CELLS], rows, theYPos);
  876. self_create_cell(theShtData[JV.PROP_SIGNATURE_AUDIT_CELLS], rows, theYPos);
  877. // fsUtil.writeObjToFile(newPageData, 'D:/GitHome/ConstructionOperation/tmp/combinedHeader.js');
  878. // console.log('theShtData[JV.PROP_SIGNATURE_AUDIT_CELLS]: ');
  879. // console.log(theShtData[JV.PROP_SIGNATURE_AUDIT_CELLS]);
  880. // 3. then fill up rst
  881. for (let i = 0; i < rows.length; i++) {
  882. rst.push('<row r="' + (i + 1 + rowOffset) + '" spans="1:' + spanX + '" ht="' + rows[i].height + '" customHeight="1">');
  883. for (let j = 0; j < rows[i].items.length; j++) {
  884. const cellObj = rows[i].items[j];
  885. if (cellObj.v === undefined) {
  886. rst.push('<c r="' + cellObj.r + '" s="' + cellObj.s + '"/>');
  887. } else {
  888. rst.push('<c r="' + cellObj.r + '" s="' + cellObj.s + '" t="s">');
  889. rst.push('<v>' + cellObj.v + '</v>');
  890. rst.push('</c>');
  891. }
  892. }
  893. rst.push('</row>');
  894. }
  895. // 4. maybe need to dispose the memory
  896. // ...
  897. };
  898. if (sheetData) {
  899. // current sheet data
  900. currentPageMergePos = sheetData[JV.PAGE_SPECIAL_MERGE_POS];
  901. currentMergeBorder = sheetData[JV.PROP_PAGE_MERGE_BORDER];
  902. if (hasSignature && sheetData[JV.PROP_SIGNATURE_CELLS] && sheetData[JV.PROP_SIGNATURE_CELLS].length > 0) {
  903. // 有签名情况下,还是有必要创建一个dummy cell的(画框用)
  904. _createDummyCell(sheetData[JV.PROP_SIGNATURE_CELLS], sheetData);
  905. }
  906. self_setDataEx(sheetData, yPos, 0);
  907. } else {
  908. // total data in one sheet
  909. let rowOffset = 0;
  910. for (let i = 0; i < pageData.items.length; i++) {
  911. const shtItemData = pageData.items[i];
  912. currentPageMergePos = shtItemData[JV.PAGE_SPECIAL_MERGE_POS];
  913. currentMergeBorder = shtItemData[JV.PROP_PAGE_MERGE_BORDER];
  914. const tmpPos = yMultiPos[i];
  915. // console.log(yMultiPos[i]);
  916. cellIdx = 0;
  917. if (hasSignature && shtItemData[JV.PROP_SIGNATURE_CELLS] && shtItemData[JV.PROP_SIGNATURE_CELLS].length > 0) {
  918. // 有签名情况下,还是有必要创建一个dummy cell的(画框用)
  919. _createDummyCell(shtItemData[JV.PROP_SIGNATURE_CELLS], shtItemData);
  920. }
  921. self_setDataEx(shtItemData, tmpPos, rowOffset);
  922. rowOffset += tmpPos.length - 2;
  923. }
  924. }
  925. rst.push('</sheetData>');
  926. };
  927. preAnalyzePos(pageData, sheetData, xPos, yPos, yMultiPos);
  928. rst.push(dftHeadXml + '\r\n');
  929. rst.push('<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  930. const colStr = private_getCellIdxStr(xPos.length - 3);
  931. rst.push('<dimension ref="A1:' + colStr + '' + yPos.length + '"/>');
  932. if (isFirstSheet) {
  933. rst.push('<sheetViews><sheetView showGridLines="1" tabSelected="1" workbookViewId="0">');
  934. } else {
  935. rst.push('<sheetViews><sheetView showGridLines="1" workbookViewId="0">');
  936. }
  937. // rst.push('<selection sqref="A1:' + colStr + '1"/>');
  938. rst.push('<selection sqref="A1:A1"/>');
  939. rst.push('</sheetView></sheetViews>');
  940. rst.push('<sheetFormatPr defaultRowHeight="13.5"/>');
  941. private_setCols();
  942. private_setSheetData();
  943. private_setMergedCells();
  944. rst.push('<phoneticPr fontId="1" type="noConversion"/>');
  945. // rst.push('<pageMargins left="0.315" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
  946. rst.push('<pageMargins left="' + (parseFloat(pageData[JV.NODE_PAGE_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]) * 0.39375) +
  947. '" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
  948. let actPageSize = paperSize;
  949. if (sheetData && sheetData[JV.PROP_PAGE_SIZE]) {
  950. actPageSize = sheetData[JV.PROP_PAGE_SIZE];
  951. } else {
  952. actPageSize = (pageData[JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] !== 'A3') ? paperSize : pageData[JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE];
  953. }
  954. // 这里的逻辑是:如果初始设置为A3的模板,表示为固化显示(未来可能会有其他的标记来处理,目前先这样)
  955. const paperSizeIdx = JV.PAGES_SIZE_STR.indexOf(actPageSize);
  956. let pStr = '';
  957. if (paperSizeIdx >= 0) {
  958. pStr = 'paperSize="' + JV.PAGES_SIZE_IDX[paperSizeIdx] + '"';
  959. }
  960. let orientationStr = (pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][0] > pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1]) ? 'landscape' : 'portrait';
  961. if (currentPageMergePos) {
  962. orientationStr = (currentPageMergePos[JV.NODE_PAGE_SIZE][0] > currentPageMergePos[JV.NODE_PAGE_SIZE][1]) ? 'landscape' : 'portrait';
  963. }
  964. rst.push('<pageSetup ' + pStr + ' fitToWidth="0" fitToHeight="0" orientation="' + orientationStr + '" />');
  965. rst.push('<headerFooter alignWithMargins="0"/>');
  966. // 插入分页符---------------------------
  967. if (pageData.pageBreaks) {
  968. let pgBrks = [];
  969. if (sheetData !== null) {
  970. pgBrks = pageData.pageBreaks[pgBrkIdx];
  971. } else {
  972. pgBrks = pageData.pageBreaks[0];
  973. }
  974. if (pgBrks.length > 1) {
  975. rst.push('<rowBreaks count="' + pgBrks.length + '" manualBreakCount="' + pgBrks.length + '" >');
  976. for (let pbi = 0; pbi < pgBrks.length; pbi++) {
  977. rst.push('<brk id="' + pgBrks[pbi] + '" max="16383" man="1" />');
  978. }
  979. rst.push('</rowBreaks>');
  980. }
  981. }
  982. // 插入分页符结束---------------------------
  983. // 图形要放在最后,否则excel会报错(但WPS不会)
  984. if (hasSignature && (sheetData === null || signSheetIdxArr[sheetIdx])) {
  985. rst.push('<drawing r:id="rId1"/>');
  986. }
  987. rst.push('</worksheet>');
  988. return rst;
  989. }
  990. function writeWorkSheetRels(signSheetIdx) {
  991. const rst = [];
  992. rst.push('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
  993. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  994. rst.push('<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing" Target="../drawings/drawing' + signSheetIdx + '.xml"/>');
  995. rst.push('</Relationships>');
  996. return rst;
  997. }
  998. async function writeImage(path, pic) {
  999. let rst = null;
  1000. if (pic) {
  1001. rst = convertPicBase64ToBlob(pic);
  1002. } else if (path) {
  1003. // 前端方式处理不同,不用考虑哪里(水印也是),直接都一股脑地申请下载即可(重复的浏览器会找cache!)
  1004. let picpath = path; // 前端的path已经是完整的了
  1005. rst = await getBlob(picpath); //这里通过getBlob函数得到的结果已经是所需要的流数据,无需转换
  1006. }
  1007. return rst;
  1008. }
  1009. function writeDrawings(pageData, signKeyArr, signPathArr, isSinglePage, signSheetIdxArr) {
  1010. const rst = [];
  1011. // 优化调整,不再每页都设置一个,重复的签名只需要一个即可(极端case: 13xx多页的签名文档,导出有1.3G大)
  1012. if (isSinglePage) {
  1013. rst.push(writeDrawing(pageData, null, signKeyArr, 0));
  1014. } else {
  1015. for (let i = 0; i < pageData.items.length; i++) {
  1016. if (signSheetIdxArr[i]) {
  1017. rst.push(writeDrawing(pageData, pageData.items[i], signKeyArr, i));
  1018. }
  1019. }
  1020. }
  1021. // */
  1022. return rst;
  1023. }
  1024. function writeDrawing(pageData, sheetData, subSignKeyArr, sheetIdx) {
  1025. const rst = [];
  1026. const xPos = [];
  1027. const yPos = [];
  1028. const yMultiPos = [];
  1029. const _getPicIndex = (signCell) => {
  1030. // signatureCell.flowAccIdx = signPath.flowAccIdx;
  1031. let signKey = signCell.signature_name;
  1032. if (signCell.hasOwnProperty('flowAccIdx') && signCell.flowAccIdx >= 0) {
  1033. signKey = `${signCell.signature_name}_${signCell.flowAccIdx}`;
  1034. }
  1035. return subSignKeyArr.indexOf(signKey);
  1036. };
  1037. const private_setSheetDrawingCellData = function(signCell, theYPos, startPicIdx, offsetRow) {
  1038. let cellControl;
  1039. if (typeof signCell[JV.PROP_CONTROL] === 'string') {
  1040. cellControl = pageData[JV.NODE_CONTROL_COLLECTION][signCell[JV.PROP_CONTROL]];
  1041. } else {
  1042. cellControl = signCell[JV.PROP_CONTROL];
  1043. }
  1044. const area = getAndSetProperSignatureArea(signCell, cellControl);
  1045. // 1. from
  1046. // let colIdx = xPos.indexOf(signCell[JV.PROP_AREA][JV.PROP_LEFT]);
  1047. // let rowIdx = theYPos.indexOf(signCell[JV.PROP_AREA][JV.PROP_TOP]) + offsetRow; // 这里要考虑Row的偏移量(多页在同同一个sheet输出)
  1048. let colIdx = xPos.indexOf(area[0]);
  1049. let rowIdx = theYPos.indexOf(area[1]) + offsetRow; // 这里要考虑Row的偏移量(多页在同同一个sheet输出)
  1050. // 因为 xPos, theYPos的首位是0,所以还需要减1
  1051. colIdx--;
  1052. rowIdx--;
  1053. rst.push('<xdr:twoCellAnchor editAs="oneCell">');
  1054. rst.push('<xdr:from>');
  1055. rst.push('<xdr:col>' + colIdx + '</xdr:col>');
  1056. rst.push('<xdr:colOff>0</xdr:colOff>');
  1057. rst.push('<xdr:row>' + rowIdx + '</xdr:row>');
  1058. rst.push('<xdr:rowOff>0</xdr:rowOff>');
  1059. rst.push('</xdr:from>');
  1060. // 2. to
  1061. colIdx = xPos.indexOf(area[2]);
  1062. rowIdx = theYPos.indexOf(area[3]) + offsetRow;
  1063. // 因为 xPos, theYPos的首位是0,所以还需要减1
  1064. colIdx--;
  1065. rowIdx--;
  1066. rst.push('<xdr:to>');
  1067. rst.push('<xdr:col>' + colIdx + '</xdr:col>');
  1068. rst.push('<xdr:colOff>0</xdr:colOff>');
  1069. rst.push('<xdr:row>' + rowIdx + '</xdr:row>');
  1070. rst.push('<xdr:rowOff>0</xdr:rowOff>');
  1071. rst.push('</xdr:to>');
  1072. // 3. pic
  1073. rst.push('<xdr:pic>');
  1074. // 3.1
  1075. rst.push('<xdr:nvPicPr>');
  1076. rst.push('<xdr:cNvPr id="' + startPicIdx + '" name="图片 ' + startPicIdx + '"/>');
  1077. rst.push('<xdr:cNvPicPr><a:picLocks noChangeAspect="1" noChangeArrowheads="1"/></xdr:cNvPicPr>');
  1078. rst.push('</xdr:nvPicPr>');
  1079. // 3.2
  1080. rst.push('<xdr:blipFill>');
  1081. // 重点!!!
  1082. // const picIdx = subSignKeyArr.indexOf(signCell.signature_name) + 1;
  1083. const picIdx = _getPicIndex(signCell) + 1;
  1084. // 3.2.1
  1085. rst.push('<a:blip xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:embed="rId' + picIdx + '" cstate="print">');
  1086. rst.push('<a:extLst>');
  1087. rst.push('<a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">'); // 参考excel本身,都是固定的uri
  1088. rst.push('<a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0"/>');
  1089. rst.push('</a:ext>');
  1090. rst.push('</a:extLst>');
  1091. rst.push('</a:blip>');
  1092. // 3.2.2
  1093. rst.push('<a:srcRect/>');
  1094. // 3.2.3
  1095. rst.push('<a:stretch><a:fillRect/></a:stretch>');
  1096. rst.push('</xdr:blipFill>');
  1097. // 3.3
  1098. rst.push('<xdr:spPr bwMode="auto">');
  1099. // 3.3.1
  1100. rst.push('<a:xfrm>');
  1101. // 以下经过Excel及WPS的测试,这数据好像没什么意义,都设为0
  1102. rst.push('<a:off x="0" y="0"/>');
  1103. rst.push('<a:ext cx="0" cy="0"/>');
  1104. rst.push('</a:xfrm>');
  1105. // 3.3.2
  1106. rst.push('<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>');
  1107. // 3.3.3
  1108. rst.push('<a:noFill/>');
  1109. // 3.3.4
  1110. rst.push('<a:extLst>');
  1111. rst.push('<a:ext uri="{909E8E84-426E-40DD-AFC4-6F175D3DCCD1}">');
  1112. rst.push('<a14:hiddenFill xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main">');
  1113. rst.push('<a:solidFill><a:srgbClr val="FFFFFF"/></a:solidFill>');
  1114. rst.push('</a14:hiddenFill>');
  1115. rst.push('</a:ext>');
  1116. rst.push('</a:extLst>');
  1117. // 收尾
  1118. rst.push('</xdr:spPr>');
  1119. rst.push('</xdr:pic>');
  1120. // 4. client Data
  1121. rst.push('<xdr:clientData/>');
  1122. rst.push('</xdr:twoCellAnchor>');
  1123. };
  1124. preAnalyzePos(pageData, sheetData, xPos, yPos, yMultiPos);
  1125. rst.push('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
  1126. rst.push('<xdr:wsDr xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">');
  1127. // console.log('subSignKeyArr');
  1128. // console.log(subSignKeyArr);
  1129. if (sheetData) {
  1130. // console.log('sheetData');
  1131. let startPicIdx = 2 + sheetIdx * 1000;
  1132. for (const sCell of sheetData[JV.PROP_SIGNATURE_CELLS]) {
  1133. if (_getPicIndex(sCell) >= 0) {
  1134. // if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1135. private_setSheetDrawingCellData(sCell, yPos, startPicIdx, 0);
  1136. startPicIdx++;
  1137. }
  1138. }
  1139. for (const sCell of sheetData[JV.PROP_WATERMARK_CELLS]) {
  1140. if (_getPicIndex(sCell) >= 0) {
  1141. // if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1142. private_setSheetDrawingCellData(sCell, yPos, startPicIdx, 0);
  1143. startPicIdx++;
  1144. }
  1145. }
  1146. } else {
  1147. // total data in one sheet
  1148. let rowOffset = 0;
  1149. for (let i = 0; i < pageData.items.length; i++) {
  1150. const shtItemData = pageData.items[i];
  1151. const tmpPos = yMultiPos[i];
  1152. let startPicIdx = 2;
  1153. for (const sCell of shtItemData[JV.PROP_SIGNATURE_CELLS]) {
  1154. if (_getPicIndex(sCell) >= 0) {
  1155. // if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1156. private_setSheetDrawingCellData(sCell, tmpPos, startPicIdx, rowOffset);
  1157. startPicIdx++;
  1158. }
  1159. }
  1160. for (const sCell of shtItemData[JV.PROP_WATERMARK_CELLS]) {
  1161. if (_getPicIndex(sCell) >= 0) {
  1162. // if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1163. private_setSheetDrawingCellData(sCell, tmpPos, startPicIdx, rowOffset);
  1164. startPicIdx++;
  1165. }
  1166. }
  1167. rowOffset += tmpPos.length - 2;
  1168. }
  1169. }
  1170. rst.push('</xdr:wsDr>');
  1171. return rst;
  1172. }
  1173. function writeDrawingsRels(amt, startIdx) {
  1174. const rst = [];
  1175. rst.push('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
  1176. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  1177. for (let idx = 0; idx < amt; idx++) {
  1178. rst.push('<Relationship Id="rId' + (idx + 1) + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/image' + (startIdx + idx + 1) + '.png"/>');
  1179. }
  1180. rst.push('</Relationships>');
  1181. return rst;
  1182. }
  1183. function mergeProperties(orgObj, newObj) {
  1184. const orgPropArr = [];
  1185. const newPropArr = [];
  1186. for (const p in orgObj) {
  1187. orgPropArr.push(p);
  1188. }
  1189. for (const p in newObj) {
  1190. newPropArr.push(p);
  1191. }
  1192. for (let i = 0; i < newPropArr.length; i++) {
  1193. if (orgPropArr.indexOf(newPropArr[i]) < 0) {
  1194. orgObj[newPropArr[i]] = newObj[newPropArr[i]];
  1195. }
  1196. }
  1197. }
  1198. function _createDummyCell(dummyOrgCells, parentPage) {
  1199. for (const signature of dummyOrgCells) {
  1200. if (!signature.isStamp) {
  1201. const dummyCell = {
  1202. font: 'Content',
  1203. control: 'Default',
  1204. style: 'Default_None',
  1205. Value: '',
  1206. area: { Left: 0, Right: 0, Top: 0, Bottom: 0 },
  1207. };
  1208. if (signature.style) dummyCell.style = signature.style;
  1209. dummyCell[JV.PROP_AREA][JV.PROP_LEFT] = signature[JV.PROP_AREA][JV.PROP_LEFT];
  1210. dummyCell[JV.PROP_AREA][JV.PROP_RIGHT] = signature[JV.PROP_AREA][JV.PROP_RIGHT];
  1211. dummyCell[JV.PROP_AREA][JV.PROP_TOP] = signature[JV.PROP_AREA][JV.PROP_TOP];
  1212. dummyCell[JV.PROP_AREA][JV.PROP_BOTTOM] = signature[JV.PROP_AREA][JV.PROP_BOTTOM];
  1213. parentPage[JV.PROP_CELLS].push(dummyCell);
  1214. }
  1215. }
  1216. }
  1217. function createDummySignatureCell(pageData) {
  1218. for (let pageIdx = 0; pageIdx < pageData.items.length; pageIdx++) {
  1219. const page = pageData.items[pageIdx];
  1220. if (page[JV.PROP_SIGNATURE_CELLS] && page[JV.PROP_SIGNATURE_CELLS].length > 0) {
  1221. _createDummyCell(page[JV.PROP_SIGNATURE_CELLS], page);
  1222. }
  1223. if (page[JV.PROP_SIGNATURE_DATE_CELLS] && page[JV.PROP_SIGNATURE_DATE_CELLS].length > 0) {
  1224. _createDummyCell(page[JV.PROP_SIGNATURE_DATE_CELLS], page);
  1225. }
  1226. if (page[JV.PROP_SIGNATURE_AUDIT_CELLS] && page[JV.PROP_SIGNATURE_AUDIT_CELLS].length > 0) {
  1227. _createDummyCell(page[JV.PROP_SIGNATURE_AUDIT_CELLS], page);
  1228. }
  1229. }
  1230. }
  1231. function _checkAndSetSignatureCache(pageData, signKeyArr, signPathArr, roleRel, signSheetIdxArr, isSinglePage, checkAudit = false) {
  1232. // 备注:电子签名是以图形的方式处理,一页可以有多个签名,多页的签名基本是引用同样的图片,在这里先处理一下,后期统一引用。
  1233. // 另:以后的图片(在电子签名(signature_cells)以外的图片)会单独处理(如计算草图、水印等)
  1234. let rst = false;
  1235. let chkRoles = [];
  1236. if (roleRel instanceof Array) {
  1237. chkRoles = roleRel;
  1238. } else {
  1239. if (roleRel) chkRoles.push(roleRel);
  1240. }
  1241. const _getDupPicPath = function(path) {
  1242. let rst = null;
  1243. for (const sp of signPathArr) {
  1244. if (sp.path === path) {
  1245. rst = sp;
  1246. break;
  1247. }
  1248. }
  1249. return rst;
  1250. };
  1251. const _chkSignKeys = (signature_name, pageIdx) => {
  1252. let rst = false;
  1253. let signKey = signature_name;
  1254. for (const role_rel of chkRoles) {
  1255. if (role_rel.signature_name === signature_name && role_rel.type === '流程') {
  1256. signKey = `${signature_name}_${role_rel.flowAccList[pageIdx]}`;
  1257. break;
  1258. }
  1259. }
  1260. rst = signKeyArr.includes(signKey);
  1261. return rst;
  1262. };
  1263. const _getFlowPicIndex = (signCell, pageIdx) => {
  1264. let rst = -1;
  1265. for (const role_rel of chkRoles) {
  1266. if (role_rel.signature_name === signCell.signature_name && role_rel.type === '流程') {
  1267. rst = role_rel.flowAccList[pageIdx];
  1268. break;
  1269. }
  1270. }
  1271. return rst;
  1272. };
  1273. for (let pageIdx = 0; pageIdx < pageData.items.length; pageIdx++) {
  1274. const page = pageData.items[pageIdx];
  1275. // 优化调整,不再每页都设置一个,重复的签名只需要一个即可(极端case: 13xx多页的签名文档,导出有1.3G大)
  1276. const curSignIdx = signKeyArr.length - 1;
  1277. signSheetIdxArr[pageIdx] = false;
  1278. if (page[JV.PROP_SIGNATURE_CELLS] && page[JV.PROP_SIGNATURE_CELLS].length > 0) {
  1279. for (const signatureCell of page[JV.PROP_SIGNATURE_CELLS]) {
  1280. // 这里把图片的路径作为key值判断
  1281. if (!_chkSignKeys(signatureCell.signature_name, pageIdx)) {
  1282. // if (signKeyArr.indexOf(signatureCell.signature_name) < 0) {
  1283. // 还得判断用户是否有选择签名输出
  1284. for (const role_rel of chkRoles) {
  1285. const role = getTheRightRole(role_rel, pageIdx);
  1286. if (signatureCell.signature_name === role.signature_name) {
  1287. if (!(role.signature_name.indexOf(JV.SIGNATURE_NAME_DUMMY) < 0 && role.sign_output.indexOf('normal_sign') < 0)) {
  1288. if (!checkAudit || role.signature_name.indexOf(JV.SIGNATURE_NAME_DUMMY) >= 0 || rptSignatureHelper._chkIfAudit(role)) {
  1289. // if (!checkAudit || role.signature_name.indexOf(JV.SIGNATURE_NAME_DUMMY) >= 0 || _chkIfAudit(role)) {
  1290. // 如果签名但用户并没有选择显示签名,则不输出!
  1291. if (signatureCell.isStamp) {
  1292. //签章不能用role的图片!!!
  1293. let sPObj = _getDupPicPath(signatureCell.path);
  1294. // if (sPObj !== null && isSinglePage) { // 只有在isSinglePage为true时,才需要优化签名
  1295. if (sPObj !== null && (isSinglePage || signatureCell.isStamp)) { // 但感觉实际上allInOneBook的isStamp情况下,也需要优化签名
  1296. signatureCell.signature_name = sPObj.signature_name; // 重点,如果有相同路径的,修改signature_name!
  1297. rst = true;
  1298. } else {
  1299. const signPath = { signature_name: signatureCell.signature_name, path: null, pic: null };
  1300. signPathArr.push(signPath);
  1301. signPath.path = signatureCell.path;
  1302. signKeyArr.push(signatureCell.signature_name);
  1303. rst = true;
  1304. signSheetIdxArr[pageIdx] = true;
  1305. }
  1306. } else if (role.sign_pic && role_rel.type !== '流程') {
  1307. // 非流程的sign_pic才需要这里处理
  1308. const signPath = { signature_name: signatureCell.signature_name, path: null, pic: null };
  1309. signPathArr.push(signPath);
  1310. signPath.pic = role.sign_pic;
  1311. signKeyArr.push(signatureCell.signature_name);
  1312. rst = true;
  1313. signSheetIdxArr[pageIdx] = true;
  1314. } else if (role.sign_path) {
  1315. let sPObj = _getDupPicPath(role.sign_path);
  1316. if (sPObj !== null && isSinglePage) { // 只有在isSinglePage为true时,才需要优化签名
  1317. signatureCell.signature_name = sPObj.signature_name; // 重点,如果有相同路径的,修改signature_name!
  1318. if (sPObj.flowAccIdx) signatureCell.flowAccIdx = sPObj.flowAccIdx;
  1319. rst = true;
  1320. } else {
  1321. const signPath = { signature_name: signatureCell.signature_name, path: null, pic: null };
  1322. if (role_rel.type === '流程') {
  1323. signPath.flowAccIdx = role_rel.flowAccList[pageIdx]
  1324. if (signPath.flowAccIdx >= 0) {
  1325. signKeyArr.push(`${signatureCell.signature_name}_${role_rel.flowAccList[pageIdx]}`);
  1326. signatureCell.flowAccIdx = signPath.flowAccIdx;
  1327. signPathArr.push(signPath);
  1328. signPath.path = role.sign_path;
  1329. rst = true;
  1330. signSheetIdxArr[pageIdx] = true;
  1331. } else {
  1332. // 这里不应有签名
  1333. }
  1334. } else {
  1335. signKeyArr.push(signatureCell.signature_name);
  1336. signPathArr.push(signPath);
  1337. signPath.path = role.sign_path;
  1338. rst = true;
  1339. signSheetIdxArr[pageIdx] = true;
  1340. }
  1341. }
  1342. }
  1343. }
  1344. }
  1345. break;
  1346. }
  1347. }
  1348. } else {
  1349. // 终于需要此code branch了,因有流程签名,如果是流程签名,还需要加flowAccIdx属性
  1350. const flowIdx = _getFlowPicIndex(signatureCell, pageIdx);
  1351. if (flowIdx >= 0) {
  1352. signatureCell.flowAccIdx = flowIdx;
  1353. }
  1354. }
  1355. }
  1356. }
  1357. if (page[JV.PROP_WATERMARK_CELLS] && page[JV.PROP_WATERMARK_CELLS].length > 0) {
  1358. // 水印,单独处理
  1359. const waterMarkCell = page[JV.PROP_WATERMARK_CELLS][0];
  1360. if (signKeyArr.indexOf(waterMarkCell.signature_name) < 0) {
  1361. const signPath = { path: null, pic: null };
  1362. signPathArr.push(signPath);
  1363. signPath.pic = waterMarkCell.pic;
  1364. signPath.path = waterMarkCell.path;
  1365. signKeyArr.push(waterMarkCell.signature_name);
  1366. rst = true;
  1367. signSheetIdxArr[pageIdx] = true;
  1368. } else if (!isSinglePage) {
  1369. signSheetIdxArr[pageIdx] = true;
  1370. }
  1371. }
  1372. }
  1373. return rst;
  1374. }
  1375. const excelExportUtil = {
  1376. exportExcel: async function(pageData, paperSize, fName, options, custSheetNames, custSheetMergeBands, roleRel, callback, checkAudit = false) {
  1377. const rptOptions = ({ singlePage: false, fileName: 'report' });
  1378. if (options === 'true' || options === true) {
  1379. rptOptions.singlePage = true;
  1380. }
  1381. const isSinglePage = rptOptions.singlePage;
  1382. const sheets = [];
  1383. const signKeyArr = [];
  1384. const signPathArr = [];
  1385. const signSheetIdxArr = []; // 确定哪些sheet有签名(在多表导出的时候,有些表可能没有签名,但其他的表有签名)
  1386. let thisRoleRel = [];
  1387. if (roleRel !== null && roleRel !== undefined) {
  1388. thisRoleRel = JSON.parse(JSON.stringify(roleRel));
  1389. }
  1390. await rptSignatureHelper.resetDummySignature(pageData, thisRoleRel, true); // 把草图转换一下roleRel
  1391. // const hasSignature = false; // 暂时不支持电子签名、草图导出excel
  1392. const hasSignature = _checkAndSetSignatureCache(pageData, signKeyArr, signPathArr, thisRoleRel, signSheetIdxArr, isSinglePage, checkAudit); // 因草图的关系,thisRoleRel是否为null就不是充要的条件
  1393. if (!hasSignature) {
  1394. createDummySignatureCell(pageData);
  1395. }
  1396. if (isSinglePage) {
  1397. // 加分页符(分页符要在外部处理好)-------------------
  1398. _setupPgBrks(pageData);
  1399. // 加分页符结束-------------------
  1400. sheets.push({ sheetName: '全部页' });
  1401. } else {
  1402. if (custSheetNames && custSheetNames.length === pageData.items.length) {
  1403. for (let i = 0; i < pageData.items.length; i++) {
  1404. sheets.push({ sheetName: custSheetNames[i] });
  1405. }
  1406. } else {
  1407. for (let i = 0; i < pageData.items.length; i++) {
  1408. sheets.push({ sheetName: '第' + (i + 1) + '页' });
  1409. }
  1410. }
  1411. }
  1412. // 1.
  1413. let file = '[Content_Types].xml';
  1414. let data = writeContentTypes(sheets, isSinglePage, hasSignature);
  1415. const zip = new JSZip();
  1416. zip.file(file, data.join(''), { compression: 'DEFLATE' });
  1417. // 2.
  1418. const zip_rels = zip.folder('_rels');
  1419. file = '.rels';
  1420. data = writeRootRels();
  1421. zip_rels.file(file, data.join(''), { compression: 'DEFLATE' });
  1422. // 3.
  1423. const zip_docProps = zip.folder('docProps');
  1424. file = 'app.xml';
  1425. data = writeApp(sheets, isSinglePage);
  1426. zip_docProps.file(file, data.join(''), { compression: 'DEFLATE' });
  1427. file = 'core.xml';
  1428. data = writeCore();
  1429. zip_docProps.file(file, data.join(''), { compression: 'DEFLATE' });
  1430. // 4.
  1431. const zip_xl = zip.folder('xl');
  1432. file = 'workbook.xml';
  1433. data = writeXlWorkBook(sheets, isSinglePage);
  1434. zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
  1435. const zip_rels2 = zip_xl.folder('_rels');
  1436. file = 'workbook.xml.rels';
  1437. data = writeXlRels(sheets, isSinglePage);
  1438. zip_rels2.file(file, data.join(''), { compression: 'DEFLATE' });
  1439. // 5.
  1440. const zip_theme = zip_xl.folder('theme');
  1441. file = 'theme1.xml';
  1442. data = writeTheme();
  1443. zip_theme.file(file, data, { compression: 'DEFLATE' });
  1444. if (hasSignature) {
  1445. // const _getTheRightPath = (signPath) => {
  1446. // for (const role of thisRoleRel) {
  1447. // if (role.signature_name === signPath.signature_name && role.type === '流程') {
  1448. // //
  1449. // }
  1450. // }
  1451. // };
  1452. // 5.1 写所有的image
  1453. const zip_media = zip_xl.folder('media');
  1454. let picIdx = 0;
  1455. // 优化调整,不再每页都设置一个,重复的签名只需要一个即可(极端case: 13xx多页的签名文档,导出有1.3G大)
  1456. for (let pathIdx = 0; pathIdx < signPathArr.length; pathIdx++) {
  1457. picIdx++;
  1458. // 因业务调整,需要增加流程判断
  1459. // thisRoleRel
  1460. data = await writeImage(signPathArr[pathIdx].path, signPathArr[pathIdx].pic);
  1461. file = 'image' + picIdx + '.png';
  1462. zip_media.file(file, data, { compression: 'DEFLATE' });
  1463. }
  1464. // 5.2
  1465. const zip_drawings = zip_xl.folder('drawings');
  1466. data = writeDrawings(pageData, signKeyArr, signPathArr, isSinglePage, signSheetIdxArr);
  1467. // data = writeDrawings(pageData, signKeyArr, signPathArr, false, signSheetIdxArr); // 备注:这里根本不需要考虑是否single page
  1468. for (let psIdx = 0; psIdx < data.length; psIdx++) {
  1469. file = 'drawing' + (psIdx + 1) + '.xml';
  1470. zip_drawings.file(file, data[psIdx].join(''), { compression: 'DEFLATE' });
  1471. }
  1472. // 5.3
  1473. const zip_drawings_rels = zip_drawings.folder('_rels');
  1474. let relsIdx = 0;
  1475. let relsImgAmt = 0;
  1476. for (let ssIdx = 0; ssIdx < signKeyArr.length; ssIdx++) {
  1477. data = writeDrawingsRels(signKeyArr.length, relsImgAmt); // 一个drawingX.xml.rels文件与一个drawingX.xml对应
  1478. relsIdx++;
  1479. if (isSinglePage) {
  1480. relsImgAmt += signKeyArr.length;
  1481. }
  1482. file = 'drawing' + relsIdx + '.xml.rels';
  1483. zip_drawings_rels.file(file, data.join(''), { compression: 'DEFLATE' });
  1484. }
  1485. }
  1486. // 6.
  1487. const zip_xl_worksheets = zip_xl.folder('worksheets');
  1488. const sharedStrList = [];
  1489. const stylesObj = {};
  1490. data = writeSheets(pageData, paperSize, sharedStrList, stylesObj, isSinglePage, custSheetMergeBands, hasSignature, signSheetIdxArr);
  1491. if (isSinglePage) {
  1492. for (let i = 0; i < 1; i++) {
  1493. file = 'sheet' + (i + 1) + '.xml';
  1494. zip_xl_worksheets.file(file, data[i].join(''), { compression: 'DEFLATE' });
  1495. }
  1496. } else {
  1497. for (let i = 0; i < data.length; i++) {
  1498. file = 'sheet' + (i + 1) + '.xml';
  1499. zip_xl_worksheets.file(file, data[i].join(''), { compression: 'DEFLATE' });
  1500. }
  1501. }
  1502. // 7.
  1503. if (hasSignature) {
  1504. const zip_xl_worksheets_rels = zip_xl_worksheets.folder('_rels');
  1505. if (isSinglePage) {
  1506. data = writeWorkSheetRels(1);
  1507. file = 'sheet1.xml.rels';
  1508. zip_xl_worksheets_rels.file(file, data.join(''), { compression: 'DEFLATE' });
  1509. } else {
  1510. let ssIdx = 0;
  1511. for (let i = 0; i < data.length; i++) {
  1512. if (signSheetIdxArr[i]) {
  1513. ssIdx++;
  1514. const ssData = writeWorkSheetRels(ssIdx);
  1515. file = 'sheet' + (i + 1) + '.xml.rels';
  1516. zip_xl_worksheets_rels.file(file, ssData.join(''), { compression: 'DEFLATE' });
  1517. }
  1518. }
  1519. }
  1520. }
  1521. file = 'sharedStrings.xml';
  1522. data = writeSharedString(sharedStrList);
  1523. zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
  1524. file = 'styles.xml';
  1525. data = writeStyles(stylesObj, hasSignature);
  1526. zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
  1527. zip.generateAsync({type: 'blob'}).then(function(content){
  1528. saveAs(content, `${fName}.xlsx`); // 页面需要引用 FileSaver.js 才有这个方法
  1529. });
  1530. },
  1531. exportExcelInOneBook: async function(pageDataArray, paperSize, fName, roleRelArr, callback, checkAudit = false) {
  1532. const me = this;
  1533. const newPageData = {};
  1534. // 1. 重新编排一下数据,把一份报表的pageData合并到一起作为一个Sheet输出(需要重新调整数据纵向坐标),多份报表数据就形成多个Sheet
  1535. // -- 简单来说,就是重新包装数据
  1536. try {
  1537. // 1.1 newPageData外围属性
  1538. const newContrl = {};
  1539. const newFont = {};
  1540. const newStyle = {};
  1541. const custMergeBands = [];
  1542. for (let i = 0; i < pageDataArray.length; i++) {
  1543. mergeProperties(newContrl, pageDataArray[i][JV.NODE_CONTROL_COLLECTION]);
  1544. mergeProperties(newFont, pageDataArray[i][JV.NODE_FONT_COLLECTION]);
  1545. mergeProperties(newStyle, pageDataArray[i][JV.NODE_STYLE_COLLECTION]);
  1546. }
  1547. newPageData[JV.NODE_CONTROL_COLLECTION] = newContrl;
  1548. newPageData[JV.NODE_FONT_COLLECTION] = newFont;
  1549. newPageData[JV.NODE_STYLE_COLLECTION] = newStyle;
  1550. newPageData[JV.NODE_PAGE_INFO] = pageDataArray[0][JV.NODE_PAGE_INFO];
  1551. newPageData[JV.BAND_PROP_MERGE_BAND] = pageDataArray[0][JV.BAND_PROP_MERGE_BAND];
  1552. // 1.2 重新设置pageDataArray的各个cell的Top/Bottom坐标
  1553. const sheetNames = [];
  1554. const newPagePos = [];
  1555. for (let i = 0; i < pageDataArray.length; i++) {
  1556. let offsetY = 0;
  1557. const mergeBand = {};
  1558. custMergeBands.push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND]);
  1559. // 备注:不同的报表有可能有不同的边框,如封面表就是无边框的
  1560. mergeBand[JV.PROP_LEFT] = [];
  1561. mergeBand[JV.PROP_RIGHT] = [];
  1562. mergeBand[JV.PROP_TOP] = [];
  1563. mergeBand[JV.PROP_BOTTOM] = [];
  1564. newPagePos.push(mergeBand);
  1565. if (pageDataArray[i][JV.BAND_PROP_MERGE_BAND]) {
  1566. mergeBand[JV.PROP_LEFT].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_LEFT]);
  1567. mergeBand[JV.PROP_RIGHT].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_RIGHT]);
  1568. }
  1569. sheetNames.push(pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_MAIN_INFO_RPT_NAME]);
  1570. // 加分页符(分页符要在外部处理好)-------------------
  1571. _setupPgBrks(pageDataArray[i]);
  1572. // 加分页符结束-------------------
  1573. let maxY = 0;
  1574. let minY = 100000;
  1575. const checkAndResetCellYPos = function(cells) {
  1576. if (cells && cells.length > 0) {
  1577. for (const cell of cells) {
  1578. if (maxY < cell[JV.PROP_AREA][JV.PROP_BOTTOM]) {
  1579. maxY = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  1580. }
  1581. if (minY > cell[JV.PROP_AREA][JV.PROP_TOP]) {
  1582. minY = cell[JV.PROP_AREA][JV.PROP_TOP];
  1583. }
  1584. cell[JV.PROP_AREA][JV.PROP_BOTTOM] += offsetY;
  1585. cell[JV.PROP_AREA][JV.PROP_TOP] += offsetY;
  1586. if (cell.orgArea) {
  1587. cell.orgArea[JV.PROP_BOTTOM] += offsetY;
  1588. cell.orgArea[JV.PROP_TOP] += offsetY;
  1589. }
  1590. if (cell.maxRect) {
  1591. cell.maxRect[JV.IDX_TOP] += offsetY;
  1592. cell.maxRect[JV.IDX_BOTTOM] += offsetY;
  1593. }
  1594. }
  1595. }
  1596. };
  1597. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1598. maxY = 0;
  1599. minY = 100000;
  1600. const currentPage = pageDataArray[i].items[j];
  1601. if (currentPage[JV.PAGE_SPECIAL_MERGE_POS]) {
  1602. let pos = currentPage[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_TOP][0] + offsetY;
  1603. mergeBand[JV.PROP_TOP].push(pos);
  1604. pos = currentPage[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_BOTTOM][0] + offsetY;
  1605. mergeBand[JV.PROP_BOTTOM].push(pos);
  1606. } else if (currentPage[JV.PROP_PAGE_MERGE_BORDER]) {
  1607. let pos = currentPage[JV.PROP_PAGE_MERGE_BORDER][JV.PROP_TOP] + offsetY;
  1608. mergeBand[JV.PROP_TOP].push(pos);
  1609. pos = currentPage[JV.PROP_PAGE_MERGE_BORDER][JV.PROP_BOTTOM] + offsetY;
  1610. mergeBand[JV.PROP_BOTTOM].push(pos);
  1611. } else if (pageDataArray[i][JV.BAND_PROP_MERGE_BAND]) {
  1612. mergeBand[JV.PROP_TOP].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_TOP] + offsetY);
  1613. mergeBand[JV.PROP_BOTTOM].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_BOTTOM] + offsetY);
  1614. }
  1615. // 1.2.1 重新设置普通cells的Top Bottom坐标
  1616. checkAndResetCellYPos(currentPage[JV.PROP_CELLS]);
  1617. checkAndResetCellYPos(currentPage[JV.PROP_INTERACT_CELLS]);
  1618. // 1.2.2 重新设置电子签名cells的Top Bottom坐标
  1619. checkAndResetCellYPos(currentPage[JV.PROP_SIGNATURE_CELLS]);
  1620. // 1.2.3 重新设置电子签名日期cells的Top Bottom坐标
  1621. checkAndResetCellYPos(currentPage[JV.PROP_SIGNATURE_DATE_CELLS]);
  1622. // 1.2.4 重置电子签名审核意见cells的Top Bottom坐标
  1623. checkAndResetCellYPos(currentPage[JV.PROP_SIGNATURE_AUDIT_CELLS]);
  1624. // 1.2.5 重置水印的Top Bottom坐标
  1625. checkAndResetCellYPos(currentPage[JV.PROP_WATERMARK_CELLS]);
  1626. 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;
  1627. offsetY += (maxY - minY);
  1628. if (bottomGap > 10) {
  1629. offsetY += (bottomGap - 10);
  1630. }
  1631. }
  1632. }
  1633. // 2. newPageData的items属性及相关分页符
  1634. newPageData.items = [];
  1635. newPageData.pageBreaks = [];
  1636. for (let i = 0; i < pageDataArray.length; i++) {
  1637. const pageItem = {};
  1638. pageItem[JV.PROP_PAGE_SEQ] = i + 1;
  1639. pageItem[JV.PROP_CELLS] = [];
  1640. pageItem[JV.PROP_INTERACT_CELLS] = [];
  1641. pageItem[JV.PROP_SIGNATURE_CELLS] = [];
  1642. pageItem[JV.PROP_SIGNATURE_DATE_CELLS] = [];
  1643. pageItem[JV.PROP_SIGNATURE_AUDIT_CELLS] = [];
  1644. pageItem[JV.PROP_WATERMARK_CELLS] = [];
  1645. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1646. const currentPage = pageDataArray[i].items[j];
  1647. pageItem[JV.PROP_CELLS].push(...(currentPage[JV.PROP_CELLS] || []));
  1648. pageItem[JV.PROP_INTERACT_CELLS].push(...(currentPage[JV.PROP_INTERACT_CELLS] || []));
  1649. pageItem[JV.PROP_SIGNATURE_CELLS].push(...(currentPage[JV.PROP_SIGNATURE_CELLS] || []));
  1650. pageItem[JV.PROP_SIGNATURE_DATE_CELLS].push(...(currentPage[JV.PROP_SIGNATURE_DATE_CELLS] || []));
  1651. pageItem[JV.PROP_SIGNATURE_AUDIT_CELLS].push(...(currentPage[JV.PROP_SIGNATURE_AUDIT_CELLS] || []));
  1652. pageItem[JV.PROP_WATERMARK_CELLS].push(...(currentPage[JV.PROP_WATERMARK_CELLS] || []));
  1653. }
  1654. newPagePos[i][JV.NODE_PAGE_SIZE] = pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE];
  1655. pageItem[JV.PAGE_SPECIAL_MERGE_POS] = newPagePos[i];
  1656. pageItem[JV.PROP_PAGE_SIZE] = pageDataArray[i][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE];
  1657. newPageData.items.push(pageItem);
  1658. // 重新分配分页符()------------------
  1659. if (pageDataArray[i].pageBreaks.length === 1) {
  1660. newPageData.pageBreaks.push(pageDataArray[i].pageBreaks[0]);
  1661. } else {
  1662. newPageData.pageBreaks.push([]);
  1663. }
  1664. // 重新分配分页符结束------------------
  1665. }
  1666. // 3. everything is ok, then call me
  1667. await me.exportExcel(newPageData, paperSize, fName, 'false', sheetNames, custMergeBands, roleRelArr, callback, checkAudit);
  1668. } catch (e) {
  1669. console.log(e);
  1670. }
  1671. }
  1672. };
  1673. function convertPicBase64ToBlob(picdata) {
  1674. //去掉头(如:'data:image/png;base64,'),并转换为byte
  1675. let splitPic = picdata.split(',');
  1676. let tmpPicData = '';
  1677. if (splitPic.length === 2) {
  1678. tmpPicData = splitPic[1];
  1679. } else {
  1680. tmpPicData = splitPic[0];
  1681. }
  1682. var bytes = window.atob(tmpPicData);
  1683. //处理异常,将ascii码小于0的转换为大于0
  1684. var ab = new Uint8Array(bytes.length);
  1685. for (var i = 0; i < bytes.length; i++) {
  1686. ab[i] = bytes.charCodeAt(i);
  1687. }
  1688. return new Blob([ab], { type: 'application/pdf' });
  1689. }