rpt_jsexcel.js 90 KB

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