rpt_excel_util.js 66 KB

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