rpt_excel_util.js 73 KB

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