rpt_excel_util.js 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /**
  2. * Created by Tony on 2017/4/1.
  3. */
  4. let JV = require('../rpt_component/jpc_value_define');
  5. let fs = require('fs');
  6. let JSZip = require("jszip");
  7. let strUtil = require('../../../public/stringUtil');
  8. let jpcCmnHelper = require('../rpt_component/helper/jpc_helper_common');
  9. let DPI = jpcCmnHelper.getScreenDPI()[0];
  10. let fsUtil = require('../../../public/fsUtil');
  11. const dftHeadXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
  12. function writeContentTypes(sheets, isSinglePage) {
  13. let rst = [];
  14. rst.push(dftHeadXml + '\r\n');
  15. rst.push('<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">');
  16. //...
  17. rst.push('<Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/>');
  18. rst.push('<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>');
  19. rst.push('<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>');
  20. rst.push('<Default Extension="xml" ContentType="application/xml"/>');
  21. rst.push('<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>');
  22. rst.push('<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>');
  23. rst.push('<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>');
  24. if (isSinglePage) {
  25. rst.push('<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>');
  26. } else {
  27. for (let i = 0; i < sheets.length; i++) {
  28. rst.push('<Override PartName="/xl/worksheets/sheet' + (i + 1) + '.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>');
  29. }
  30. }
  31. rst.push('<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>');
  32. rst.push('</Types>');
  33. return rst;
  34. }
  35. function writeRootRels(){
  36. let rst = [];
  37. rst.push(dftHeadXml + '\r\n');
  38. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  39. rst.push('<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>');
  40. rst.push('<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>');
  41. rst.push('<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>');
  42. rst.push('</Relationships>');
  43. return rst;
  44. }
  45. function writeApp(sheets, isSinglePage) {
  46. let rst = [];
  47. rst.push(dftHeadXml + '\r\n');
  48. rst.push('<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">');
  49. rst.push('<Application>Microsoft Excel</Application>');
  50. rst.push('<DocSecurity>0</DocSecurity>');
  51. rst.push('<ScaleCrop>false</ScaleCrop>');
  52. rst.push('<HeadingPairs>');
  53. rst.push('<vt:vector size="2" baseType="variant">');
  54. rst.push('<vt:variant><vt:lpstr>工作表</vt:lpstr></vt:variant>');
  55. if (isSinglePage) rst.push('<vt:variant><vt:i4>1</vt:i4></vt:variant>')
  56. else rst.push('<vt:variant><vt:i4>' + sheets.length + '</vt:i4></vt:variant>');
  57. rst.push('</vt:vector>');
  58. rst.push('</HeadingPairs>');
  59. rst.push('<TitlesOfParts>');
  60. if (isSinglePage) {
  61. rst.push('<vt:vector size="1" baseType="lpstr">');
  62. rst.push('<vt:lpstr>' + sheets[0].sheetName + '</vt:lpstr>');
  63. } else {
  64. rst.push('<vt:vector size="' + sheets.length + '" baseType="lpstr">');
  65. for (let i = 0; i < sheets.length; i++) {
  66. rst.push('<vt:lpstr>' + sheets[i].sheetName + '</vt:lpstr>');
  67. }
  68. }
  69. rst.push('</vt:vector>');
  70. rst.push('</TitlesOfParts>');
  71. rst.push('<Company>SmartCost</Company>');
  72. rst.push('<LinksUpToDate>false</LinksUpToDate>');
  73. rst.push('<SharedDoc>false</SharedDoc>');
  74. rst.push('<HyperlinksChanged>false</HyperlinksChanged>');
  75. rst.push('<AppVersion>12.0000</AppVersion>');
  76. rst.push('</Properties>');
  77. return rst;
  78. }
  79. function writeCore() {
  80. let rst = [];
  81. let p_fillZero = function(val){
  82. let rst = val;
  83. if (val < 10) {
  84. rst = '0' + val;
  85. }
  86. return rst;
  87. };
  88. rst.push(dftHeadXml + '\r\n');
  89. 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">');
  90. rst.push('<dc:creator>SmartCost</dc:creator>');
  91. rst.push('<cp:lastModifiedBy>SmartCost</cp:lastModifiedBy>');
  92. let dt = new Date();
  93. dt.setDate(dt.getDate() - 8/24); //it's GMT time, so please add the server offset time ( -8 hours )
  94. let dtStr = dt.getFullYear() + '-' + p_fillZero(dt.getMonth()+1) + '-' + p_fillZero(dt.getDate()) + 'T' +
  95. p_fillZero(dt.getHours()) + ':' + p_fillZero(dt.getMinutes()) + ':' + p_fillZero(dt.getSeconds()) + 'Z';
  96. rst.push('<dcterms:created xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:created>');
  97. rst.push('<dcterms:modified xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:modified>');
  98. //rst.push('');
  99. rst.push('</cp:coreProperties>');
  100. return rst;
  101. }
  102. function writeXlWorkBook(sheets, isSinglePage){
  103. let rst = [];
  104. rst.push(dftHeadXml + '\r\n');
  105. rst.push('<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  106. rst.push('<fileVersion appName="xl" lastEdited="4" lowestEdited="4" rupBuild="4505"/>');
  107. rst.push('<workbookPr defaultThemeVersion="124226"/>');
  108. rst.push('<bookViews><workbookView xWindow="360" yWindow="345" windowWidth="14655" windowHeight="4305"/></bookViews>');
  109. rst.push('<sheets>');
  110. if (isSinglePage) {
  111. rst.push('<sheet name="' + sheets[0].sheetName + '" sheetId="1" r:id="rId1"/>');
  112. } else {
  113. for (let i = 0; i < sheets.length; i++) {
  114. rst.push('<sheet name="' + sheets[i].sheetName + '" sheetId="' + (i + 1) + '" r:id="rId' + (i + 1) + '"/>');
  115. }
  116. }
  117. rst.push('</sheets>');
  118. rst.push('<calcPr calcId="124519"/>');
  119. rst.push('</workbook>');
  120. return rst;
  121. }
  122. function writeXlRels(sheets, isSinglePage){
  123. let rst = [], idx = 1;
  124. rst.push(dftHeadXml + '\r\n');
  125. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  126. if (isSinglePage) {
  127. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>');
  128. idx++;
  129. } else {
  130. for (let i = 0; i < sheets.length; i++) {
  131. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet' + (i + 1) + '.xml"/>');
  132. idx++;
  133. }
  134. }
  135. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>');
  136. idx++;
  137. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>');
  138. idx++;
  139. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/>');
  140. rst.push('</Relationships>');
  141. return rst;
  142. }
  143. function writeTheme(){
  144. let rst = fs.readFileSync(__dirname + '/excel_base_files/theme1.xml', 'utf8', 'r');
  145. return rst;
  146. }
  147. function writeStyles(stylesObj){
  148. let rst = [];
  149. rst.push(dftHeadXml + '\r\n');
  150. rst.push('<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">');
  151. //1. push fonts
  152. rst.push('<fonts count="' + stylesObj.fonts.length + '">');
  153. for (let i = 0; i < stylesObj.fonts.length; i++) {
  154. let font = stylesObj.fonts[i];
  155. rst.push('<font>');
  156. if (strUtil.convertStrToBoolean(font[JV.FONT_PROPS[3]])) {
  157. rst.push('<b/>');
  158. }
  159. if (strUtil.convertStrToBoolean(font[JV.FONT_PROPS[5]])) {
  160. rst.push('<u/>');
  161. }
  162. rst.push('<sz val="' + font.size + '"/>');
  163. rst.push('<color indexed="' + font.colorIdx + '"/>');
  164. rst.push('<name val="' + font[JV.FONT_PROPS[0]] + '"/>');
  165. rst.push('<charset val="' + font.charset + '"/>');
  166. rst.push('</font>');
  167. }
  168. rst.push('</fonts>');
  169. //2. push default fills
  170. rst.push('<fills count="2"><fill><patternFill patternType="none" /></fill><fill><patternFill patternType="gray125" /></fill></fills>');
  171. //3. push borders
  172. rst.push('<borders count="' + stylesObj.borders.length + '">');
  173. let private_setBorder = function(border, borderDirection) {
  174. if (parseInt(border[borderDirection][JV.PROP_LINE_WEIGHT]) === 0) {
  175. rst.push('<' + borderDirection.toLowerCase() + '/>');
  176. } else {
  177. let bW = 'thin';
  178. if (parseInt(border[borderDirection][JV.PROP_LINE_WEIGHT]) === 2) bW = 'medium';
  179. if (parseInt(border[borderDirection][JV.PROP_LINE_WEIGHT]) > 2) bW = 'thick';
  180. rst.push('<' + borderDirection.toLowerCase() + ' style="' + bW + '">' + '<color indexed="64"/>' + '</' + borderDirection.toLowerCase() + '>');
  181. }
  182. };
  183. for (let i = 0; i < stylesObj.borders.length; i++) {
  184. let border = stylesObj.borders[i];
  185. rst.push('<border>');
  186. private_setBorder(border, JV.PROP_LEFT);
  187. private_setBorder(border, JV.PROP_RIGHT);
  188. private_setBorder(border, JV.PROP_TOP);
  189. private_setBorder(border, JV.PROP_BOTTOM);
  190. rst.push('<diagonal />');
  191. rst.push('</border>');
  192. }
  193. rst.push('</borders>');
  194. //4. push cellStyleXfs
  195. rst.push('<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"><alignment vertical="center"/></xf></cellStyleXfs>');
  196. //5. push cellXfs
  197. rst.push('<cellXfs count="' + stylesObj.cellXfs.length + '">');
  198. for (let i = 0; i < stylesObj.cellXfs.length; i++) {
  199. let excelStyle = stylesObj.cellXfs[i];
  200. rst.push('<xf numFmtId="0" fontId="' + excelStyle.fontId + '" fillId="0" borderId="' + excelStyle.borderId + '" xfId="0">');
  201. //pageData[JV.NODE_FONT_COLLECTION] excelStyle.fontId
  202. let alignStr = "<alignment";
  203. let textRotation = 0;
  204. let newHorizontal = excelStyle[JV.CONTROL_PROPS[2]];
  205. let newVertical = excelStyle[JV.CONTROL_PROPS[3]];
  206. if (parseInt(excelStyle.fontAngle) !== 0) {
  207. let tmpH = newHorizontal, tmpV = newVertical;
  208. if (excelStyle.fontAngle > 0) {
  209. textRotation = 180;
  210. if (newHorizontal === "left") {
  211. tmpV = 'top';
  212. } else if (newHorizontal === "right") {
  213. tmpV = 'bottom';
  214. } else {
  215. tmpV = 'center';
  216. }
  217. if (newVertical === "top") {
  218. tmpH = 'right';
  219. } else if (newVertical === "bottom") {
  220. tmpH = 'left';
  221. } else {
  222. tmpH = 'center';
  223. }
  224. } else {
  225. textRotation = 90;
  226. if (newHorizontal === "left") {
  227. tmpV = 'bottom';
  228. } else if (newHorizontal === "right") {
  229. tmpV = 'top';
  230. } else {
  231. tmpV = 'center';
  232. }
  233. if (newVertical === "top") {
  234. tmpH = 'left';
  235. } else if (newVertical === "bottom") {
  236. tmpH = 'right';
  237. } else {
  238. tmpH = 'center';
  239. }
  240. }
  241. newHorizontal = tmpH;
  242. newVertical = tmpV;
  243. }
  244. alignStr += ' horizontal="' + newHorizontal + '" vertical="' + newVertical + '"';
  245. if (strUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[0]])) {
  246. alignStr += ' shrinkToFit="1"';
  247. }
  248. if (strUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[4]])) {
  249. alignStr += ' wrapText="1"';
  250. }
  251. if (textRotation !== 0) {
  252. alignStr += ' textRotation="' + textRotation + '"';
  253. }
  254. alignStr += '/>';
  255. rst.push(alignStr);
  256. rst.push('</xf>');
  257. }
  258. rst.push('</cellXfs>');
  259. //6. others (xfl style / dxfs / tableStyles)
  260. rst.push('<cellStyles count="1"><cellStyle name="常规" xfId="0" builtinId="0"/></cellStyles>');
  261. rst.push('<dxfs count="0"/>');
  262. rst.push('<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleLight16"/>');
  263. rst.push('</styleSheet>');
  264. return rst;
  265. }
  266. function writeSharedString(sharedStrList){
  267. let rst = [];
  268. if (sharedStrList && sharedStrList.length > 0) {
  269. rst.push(dftHeadXml + '\r\n');
  270. rst.push('<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="' + sharedStrList.length + '" uniqueCount="' + sharedStrList.length + '">');
  271. for (let i = 0; i < sharedStrList.length; i++) {
  272. //rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
  273. if (typeof sharedStrList[i] === 'string') {
  274. if (sharedStrList[i].indexOf('|') >= 0) {
  275. //rst.push('<si><t>' + sharedStrList[i].split('|').join('\r\n') + '</t></si>');
  276. rst.push('<si><t>' + sharedStrList[i].split('|').join('\n') + '</t></si>');
  277. } else {
  278. rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
  279. }
  280. // rst.push('<si><t>' + sharedStrList[i].replace('|','\r\n') + '</t></si>');
  281. } else {
  282. rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
  283. }
  284. }
  285. rst.push('</sst>');
  286. }
  287. return rst;
  288. }
  289. function writeSheets(pageData, paperSize, sharedStrList, stylesObj, isSinglePage){
  290. let rst = [];
  291. let private_pushDftFont = function(){
  292. let font = {};
  293. if (!(stylesObj.fonts)) {
  294. stylesObj.fonts = [];
  295. }
  296. font[JV.FONT_PROPS[0]] = "宋体"; //font name
  297. font.size = 12;
  298. font.charset = 134;
  299. font.colorIdx = "8";
  300. stylesObj.fonts.push(font);
  301. };
  302. let private_buildFirstDftStyle = function () {
  303. stylesObj.cellXfs = [];
  304. stylesObj.borders = [];
  305. let fontId = 0;
  306. let borderId = 0;
  307. let border = {};
  308. border[JV.PROP_LEFT] = {};
  309. border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = 0;
  310. border[JV.PROP_RIGHT] = {};
  311. border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = 0;
  312. border[JV.PROP_TOP] = {};
  313. border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = 0;
  314. border[JV.PROP_BOTTOM] = {};
  315. border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = 0;
  316. stylesObj.borders.push(border);
  317. let cellControl = pageData[JV.NODE_CONTROL_COLLECTION].Default;
  318. let sheetControl = {};
  319. sheetControl.fontId = fontId;
  320. sheetControl.borderId = borderId;
  321. sheetControl.fontAngle = 0;
  322. for (let i = 0; i < JV.CONTROL_PROPS.length; i++) {
  323. sheetControl[JV.CONTROL_PROPS[i]] = cellControl[JV.CONTROL_PROPS[i]];
  324. }
  325. stylesObj.cellXfs.push(sheetControl);
  326. }
  327. private_pushDftFont();
  328. private_buildFirstDftStyle();
  329. if (isSinglePage) {
  330. rst.push(writeSheet(pageData, null, paperSize, sharedStrList, stylesObj));
  331. } else {
  332. for (let i = 0; i < pageData.items.length; i++) {
  333. rst.push(writeSheet(pageData, pageData.items[i], paperSize, sharedStrList, stylesObj));
  334. }
  335. }
  336. return rst;
  337. }
  338. function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj){
  339. let rst = [], xPos = [], yPos = [], yMultiPos = [], headerStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  340. let cacheBorderCell = {};
  341. let currentPageMergePos = null; //在 JV.PAGING_OPTION_INFINITY 场合应用
  342. let private_pre_analyze_pos = function(){
  343. let cell, pos;
  344. let self_analyze_sheet_pos = function (theShtData, theXPos, theYPos) {
  345. // theShtData.cells.sort(function(cell1, cell2) {
  346. // let rst = 0;
  347. // if (cell1[JV.PROP_AREA][JV.PROP_TOP] > cell2[JV.PROP_AREA][JV.PROP_TOP]) {
  348. // rst = 1;
  349. // } else if (cell1[JV.PROP_AREA][JV.PROP_TOP] < cell2[JV.PROP_AREA][JV.PROP_TOP]) {
  350. // rst = -1;
  351. // } else {
  352. // if (cell1[JV.PROP_AREA][JV.PROP_LEFT] > cell2[JV.PROP_AREA][JV.PROP_LEFT]) {
  353. // rst = 1;
  354. // } else if (cell1[JV.PROP_AREA][JV.PROP_LEFT] < cell2[JV.PROP_AREA][JV.PROP_LEFT]) {
  355. // rst = -1;
  356. // }
  357. // }
  358. // return rst;
  359. // });
  360. for (let i = 0; i < theShtData.cells.length; i++) {
  361. cell = theShtData.cells[i];
  362. pos = cell[JV.PROP_AREA][JV.PROP_LEFT];
  363. if (theXPos.indexOf(pos) < 0) theXPos.push(pos);
  364. pos = cell[JV.PROP_AREA][JV.PROP_RIGHT];
  365. if (theXPos.indexOf(pos) < 0) theXPos.push(pos);
  366. pos = cell[JV.PROP_AREA][JV.PROP_TOP];
  367. if (theYPos.indexOf(pos) < 0) theYPos.push(pos);
  368. pos = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  369. if (theYPos.indexOf(pos) < 0) theYPos.push(pos);
  370. }
  371. };
  372. xPos.push(0);
  373. if (sheetData) {
  374. //current sheet data
  375. yPos.push(0);
  376. self_analyze_sheet_pos(sheetData, xPos, yPos);
  377. xPos.sort(private_array_sort);
  378. yPos.sort(private_array_sort);
  379. } else {
  380. //total data in one sheet
  381. for (let shtItemData of pageData.items) {
  382. let tmpPos = [];
  383. tmpPos.push(0);
  384. self_analyze_sheet_pos(shtItemData, xPos, tmpPos);
  385. tmpPos.sort(private_array_sort);
  386. yMultiPos.push(tmpPos);
  387. }
  388. xPos.sort(private_array_sort);
  389. yPos = yMultiPos[0];
  390. }
  391. };
  392. let private_array_sort = function(i1, i2){
  393. let rst = 0;
  394. if (i1 > i2) {rst = 1} else
  395. if (i1 < i2) rst = -1;
  396. return rst;
  397. };
  398. let private_getCellIdxStr = function(idx){
  399. let rst = 'A';
  400. if (idx < 26) {
  401. rst = headerStr[idx];
  402. } else if (idx < 26*26+26) {
  403. let ti = Math.floor(idx / 26), tj = idx % 26;
  404. rst = headerStr[ti - 1] + headerStr[tj];
  405. } else if (idx < 26*26*26+26) {
  406. let ti = Math.floor(idx / (26*26)), tj = Math.floor((idx - ti * 26*26) / 26), tk = idx % 26;
  407. rst = headerStr[ti - 1] + headerStr[tj-1] + headerStr[tk];
  408. }
  409. return rst;
  410. };
  411. let private_getSharedStrIdx = function(val) {
  412. let rst = sharedStrList.indexOf(val);
  413. if (rst < 0) {
  414. sharedStrList.push(val);
  415. rst = sharedStrList.length - 1;
  416. }
  417. return rst;
  418. };
  419. let private_getFontId = function(cell) {
  420. let rst = 0, hasFont = false;
  421. if (!(stylesObj.fonts)) {
  422. stylesObj.fonts = [];
  423. //for (let i = 0; i < sheetData.font_collection)
  424. }
  425. let sheetFont = null;
  426. if (typeof cell[JV.PROP_FONT] === "string") {
  427. sheetFont = pageData[JV.NODE_FONT_COLLECTION][cell[JV.PROP_FONT]];
  428. } else {
  429. sheetFont = cell[JV.PROP_FONT];
  430. }
  431. for (let i = 0; i < stylesObj.fonts.length; i++) {
  432. let font = stylesObj.fonts[i];
  433. if (sheetFont) {
  434. if (font[JV.FONT_PROPS[0]] === sheetFont[JV.FONT_PROPS[0]] && font.size === Math.round(sheetFont[JV.FONT_PROPS[1]] * 3 / 4)
  435. && font[JV.FONT_PROPS[3]] === sheetFont[JV.FONT_PROPS[3]] && font[JV.FONT_PROPS[5]] === sheetFont[JV.FONT_PROPS[5]]) {
  436. hasFont = true;
  437. rst = i;
  438. break;
  439. }
  440. } else {
  441. break;
  442. }
  443. }
  444. if (!hasFont) {
  445. let font = {};
  446. font[JV.FONT_PROPS[0]] = sheetFont[JV.FONT_PROPS[0]]; //font name
  447. font.size = Math.round(sheetFont[JV.FONT_PROPS[1]] * 3 / 4);
  448. font.charset = 134;
  449. font.colorIdx = "8";
  450. font[JV.FONT_PROPS[3]] = sheetFont[JV.FONT_PROPS[3]]; //font bold
  451. font[JV.FONT_PROPS[5]] = sheetFont[JV.FONT_PROPS[5]]; //font underline
  452. stylesObj.fonts.push(font);
  453. rst = stylesObj.fonts.length - 1;
  454. }
  455. return rst;
  456. };
  457. let private_checkBorder = function(cell, border, sheetBorder) {
  458. let rst = true, borderLineWidths = [], sheetBorderLineWidths = [];
  459. borderLineWidths.push(border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]);
  460. borderLineWidths.push(border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]);
  461. borderLineWidths.push(border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]);
  462. borderLineWidths.push(border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]);
  463. if (sheetBorder[JV.PROP_LEFT] && sheetBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]) {
  464. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_LEFT, true));
  465. } else {
  466. sheetBorderLineWidths.push(0);
  467. }
  468. if (sheetBorder[JV.PROP_RIGHT] && sheetBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]) {
  469. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_RIGHT, true));
  470. } else {
  471. sheetBorderLineWidths.push(0);
  472. }
  473. if (sheetBorder[JV.PROP_TOP] && sheetBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]) {
  474. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_TOP, false));
  475. } else {
  476. sheetBorderLineWidths.push(0);
  477. }
  478. if (sheetBorder[JV.PROP_BOTTOM] && sheetBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]) {
  479. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_BOTTOM, false));
  480. } else {
  481. sheetBorderLineWidths.push(0);
  482. }
  483. for (let i = 0; i < 4; i++) {
  484. if (borderLineWidths[i] != sheetBorderLineWidths[i]) {
  485. rst = false;
  486. break;
  487. }
  488. }
  489. return rst;
  490. };
  491. let private_chkAndGetMergeLine = function(cell, sheetBorder, borderStr, needFurtherChk) {
  492. let rst = 0,
  493. mergeBorder = (sheetData)?sheetData[JV.PROP_PAGE_MERGE_BORDER]:pageData[JV.BAND_PROP_MERGE_BAND],
  494. mergeBand = pageData[JV.BAND_PROP_MERGE_BAND]
  495. ;
  496. if (sheetBorder[borderStr] && sheetBorder[borderStr][JV.PROP_LINE_WEIGHT] !== undefined) {
  497. rst = sheetBorder[borderStr][JV.PROP_LINE_WEIGHT];
  498. }
  499. if (currentPageMergePos) {
  500. let side = currentPageMergePos[borderStr];
  501. if (side.indexOf(cell[JV.PROP_AREA][borderStr]) >= 0) {
  502. if (needFurtherChk) {
  503. let topSide = currentPageMergePos[JV.PROP_TOP];
  504. let bottomSide = currentPageMergePos[JV.PROP_BOTTOM];
  505. for (let i = 0; i < topSide.length; i++) {
  506. if (cell[JV.PROP_AREA][JV.PROP_TOP] >= topSide[i]) {
  507. if (cell[JV.PROP_AREA][JV.PROP_BOTTOM] <= bottomSide[i]) {
  508. let destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
  509. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  510. break;
  511. }
  512. } else {
  513. break;
  514. }
  515. }
  516. } else {
  517. let destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
  518. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  519. }
  520. }
  521. } else {
  522. if (cell[JV.PROP_AREA][borderStr] === mergeBorder[borderStr]) {
  523. let destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
  524. if (needFurtherChk) {
  525. if (cell[JV.PROP_AREA][JV.PROP_TOP] >= mergeBorder[JV.PROP_TOP] &&
  526. cell[JV.PROP_AREA][JV.PROP_BOTTOM] <= mergeBorder[JV.PROP_BOTTOM]) {
  527. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  528. }
  529. } else {
  530. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  531. }
  532. }
  533. }
  534. return parseInt(rst);
  535. };
  536. let private_getIniBorder = function() {
  537. let rst = {};
  538. rst[JV.PROP_LEFT] = {};
  539. rst[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = 0;
  540. rst[JV.PROP_RIGHT] = {};
  541. rst[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = 0;
  542. rst[JV.PROP_TOP] = {};
  543. rst[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = 0;
  544. rst[JV.PROP_BOTTOM] = {};
  545. rst[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = 0;
  546. return rst;
  547. };
  548. let private_getBorderId = function(cell) {
  549. let rst = 0, hasBorder = false;
  550. if (!(stylesObj.borders)) {
  551. stylesObj.borders = [];
  552. }
  553. let sheetBorder = pageData[JV.NODE_STYLE_COLLECTION][cell.style];
  554. let mergedBorder = private_getIniBorder();
  555. mergedBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_LEFT, true);
  556. mergedBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_RIGHT, true);
  557. mergedBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_TOP, false);
  558. mergedBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_BOTTOM, false);
  559. for (let i = 0; i < stylesObj.borders.length; i++) {
  560. let border = stylesObj.borders[i];
  561. if (private_checkBorder(cell, border, mergedBorder)) {
  562. hasBorder = true;
  563. rst = i;
  564. break;
  565. }
  566. }
  567. if (!hasBorder) {
  568. stylesObj.borders.push(mergedBorder);
  569. rst = stylesObj.borders.length - 1;
  570. }
  571. return rst;
  572. };
  573. let private_checkControl = function(cellControl, sheetControl) {
  574. let rst = true;
  575. for (let i = 0; i < JV.CONTROL_PROPS.length; i++) {
  576. if (cellControl[JV.CONTROL_PROPS[i]] != sheetControl[JV.CONTROL_PROPS[i]]) {
  577. rst = false;
  578. break;
  579. }
  580. }
  581. return rst;
  582. };
  583. let private_getStyleId = function(cell) {
  584. let rst = 1, hasStyle = false;
  585. if (!(stylesObj.cellXfs)) stylesObj.cellXfs = [];
  586. let fontId = private_getFontId(cell);
  587. let fontAngle = 0;
  588. if (typeof cell[JV.PROP_FONT] === "string") {
  589. fontAngle = parseInt(pageData[JV.NODE_FONT_COLLECTION][cell[JV.PROP_FONT]].FontAngle);
  590. } else {
  591. fontAngle = parseInt(cell[JV.PROP_FONT].FontAngle);
  592. }
  593. let borderId = private_getBorderId(cell);
  594. let cellControl = null;
  595. if (typeof cell[JV.PROP_CONTROL] === "string") {
  596. cellControl = pageData[JV.NODE_CONTROL_COLLECTION][cell[JV.PROP_CONTROL]];
  597. } else {
  598. cellControl = cell[JV.PROP_CONTROL];
  599. }
  600. for (let i = 0; i < stylesObj.cellXfs.length; i++) {
  601. let sheetControl = stylesObj.cellXfs[i];
  602. if (sheetControl.fontId === fontId && sheetControl.borderId === borderId) {
  603. if (private_checkControl(cellControl, sheetControl)) {
  604. rst = i;
  605. hasStyle = true;
  606. break;
  607. }
  608. }
  609. }
  610. if (!hasStyle) {
  611. let sheetControl = {};
  612. sheetControl.fontId = fontId;
  613. sheetControl.borderId = borderId;
  614. sheetControl.fontAngle = fontAngle;
  615. for (let i = 0; i < JV.CONTROL_PROPS.length; i++) {
  616. sheetControl[JV.CONTROL_PROPS[i]] = cellControl[JV.CONTROL_PROPS[i]];
  617. }
  618. stylesObj.cellXfs.push(sheetControl);
  619. rst = stylesObj.cellXfs.length - 1;
  620. }
  621. return rst;
  622. };
  623. let private_setCols = function(){
  624. //remark: 1 excel unit width = 2.117 mm
  625. rst.push('<cols>');
  626. let w = 0;
  627. /*
  628. for (let i = 1; i < xPos.length - 1; i++) {
  629. w = 1.0 * (xPos[i + 1] - xPos[i]) / DPI * 25.4 / 2.117;
  630. w = Math.round(w * 1000) / 1000;
  631. rst.push('<col min="' + i +'" max="' + i +'" width="' + w + '" customWidth="1"/>');
  632. }
  633. /*/
  634. for (let i = 1; i < xPos.length - 1; i++) {
  635. w = 1.0 * (xPos[i + 1] - xPos[i]) / DPI * 25.4 / 2.117;
  636. w = Math.round(w * 1000) / 1000;
  637. rst.push('<col min="' + i +'" max="' + i +'" width="' + w + '" customWidth="1"/>');
  638. }
  639. rst.push('<col min="' + (xPos.length - 1) +'" max="' + (xPos.length - 1) +'" width="' + 10 + '" customWidth="1"/>');
  640. //*/
  641. rst.push('</cols>');
  642. };
  643. let private_setMergedCells = function() {
  644. let cell, idxR, idxL, idxT, idxB, cnt = 0;
  645. rst.push('<mergeCells count="?">');
  646. let startIdx = rst.length - 1;
  647. let self_setMergedCells = function (theData, theYPos, offsetY) {
  648. for (let i = 0; i < theData.cells.length; i++) {
  649. cell = theData.cells[i];
  650. idxR = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_RIGHT]);
  651. idxL = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_LEFT]);
  652. idxB = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_BOTTOM]);
  653. idxT = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_TOP]);
  654. if (idxR - idxL > 1 || idxB - idxT > 1) {
  655. rst.push('<mergeCell ref="' + private_getCellIdxStr(idxL - 1) + (idxT + offsetY) + ':' + private_getCellIdxStr(idxR - 2) + (idxB - 1 + offsetY) + '"/>');
  656. cnt++;
  657. }
  658. }
  659. }
  660. if (sheetData) {
  661. self_setMergedCells(sheetData, yPos, 0);
  662. } else {
  663. let osY = 0;
  664. for (let i = 0; i < pageData.items.length; i++) {
  665. let shtItemData = pageData.items[i];
  666. let tmpPos = yMultiPos[i];
  667. self_setMergedCells(shtItemData, tmpPos, osY);
  668. osY += tmpPos.length - 2;
  669. }
  670. }
  671. rst[startIdx] = '<mergeCells count="' + cnt + '">';
  672. rst.push('</mergeCells>');
  673. };
  674. let private_chkIfNeedCacheCell = function(cell){
  675. let rst = false;
  676. if (cell[JV.PROP_AREA][JV.PROP_LEFT] === pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_LEFT] ||
  677. cell[JV.PROP_AREA][JV.PROP_RIGHT] === pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_RIGHT] ||
  678. cell[JV.PROP_AREA][JV.PROP_TOP] === pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_TOP] ||
  679. cell[JV.PROP_AREA][JV.PROP_BOTTOM] === pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_BOTTOM]){
  680. if (cell[JV.PROP_AREA][JV.PROP_LEFT] >= pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_LEFT] &&
  681. cell[JV.PROP_AREA][JV.PROP_RIGHT] <= pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_RIGHT] &&
  682. cell[JV.PROP_AREA][JV.PROP_TOP] >= pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_TOP] &&
  683. cell[JV.PROP_AREA][JV.PROP_BOTTOM] <= pageData[JV.BAND_PROP_MERGE_BAND][JV.PROP_BOTTOM]) {
  684. rst = true;
  685. }
  686. }
  687. return rst;
  688. };
  689. let private_cacheMergeBandBorderIdxs = function() {
  690. let cell, idxR, idxL, idxT, idxB;
  691. let self_cachMergeIdxs = function (theData, theYPos, offsetY) {
  692. for (let i = 0; i < theData.cells.length; i++) {
  693. cell = theData.cells[i];
  694. idxR = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_RIGHT]);
  695. idxL = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_LEFT]);
  696. idxB = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_BOTTOM]);
  697. idxT = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_TOP]);
  698. if (idxR - idxL > 1 || idxB - idxT > 1) {
  699. if (private_chkIfNeedCacheCell(cell)) {
  700. for (let xi = idxL; xi < idxR; xi++) {
  701. for (let yj = idxT; yj < idxB; yj++) {
  702. cacheBorderCell[private_getCellIdxStr(xi - 1) + (yj + offsetY)] = cell;
  703. }
  704. }
  705. }
  706. }
  707. }
  708. }
  709. if (sheetData) {
  710. self_cachMergeIdxs(sheetData, yPos, 0);
  711. } else {
  712. let osY = 0;
  713. for (let i = 0; i < pageData.items.length; i++) {
  714. let shtItemData = pageData.items[i];
  715. let tmpPos = yMultiPos[i];
  716. self_cachMergeIdxs(shtItemData, tmpPos, osY);
  717. osY += tmpPos.length - 2;
  718. }
  719. }
  720. };
  721. let private_setSheetData = function(){
  722. //remark: 1 excel unit height = 0.3612 mm
  723. rst.push('<sheetData>');
  724. let spanX = xPos.length - 2, cellIdx = 0, h = 0
  725. ;
  726. let self_setDataEx = function (theShtData, theYPos, offsetY) {
  727. let rows = [];
  728. //1. build full set of blank rows/cells
  729. for (let i = 1; i < theYPos.length - 1; i++) {
  730. let rowObj = {};
  731. h = (theYPos[i+1] - theYPos[i]) / DPI * 25.4 / 0.3612;
  732. h = Math.round(h * 1000) / 1000;
  733. rowObj.height = h;
  734. rowObj.r = i + offsetY;
  735. rowObj.items = [];
  736. rows.push(rowObj);
  737. for (let j = 1; j < xPos.length - 1; j++) {
  738. let colIdxStr = private_getCellIdxStr(j - 1);
  739. let cellObj = {};
  740. cellObj.r = colIdxStr + (i + offsetY);
  741. cellObj.s = 0;
  742. cellObj.isBlank = true;
  743. rows[i - 1].items.push(cellObj);
  744. }
  745. }
  746. //2. then fill up the cell style-ids and values
  747. let rowIdx1 = 0, colIdx1 = 0, rowIdx2 = 0, colIdx2 = 0, colIdxStr = '';
  748. for (let cIdx = 0; cIdx < theShtData.cells.length; cIdx++) {
  749. let styleIdx = private_getStyleId(theShtData.cells[cIdx]);
  750. //colIdxStr = private_getCellIdxStr(j - 1);
  751. rowIdx1 = theYPos.indexOf(theShtData.cells[cIdx][JV.PROP_AREA][JV.PROP_TOP]);
  752. colIdx1 = xPos.indexOf(theShtData.cells[cIdx][JV.PROP_AREA][JV.PROP_LEFT]);
  753. let cellObj = rows[rowIdx1 - 1].items[colIdx1 - 1];
  754. cellObj.s = styleIdx;
  755. cellObj.isBlank = false;
  756. if (!(strUtil.isEmptyString(theShtData.cells[cIdx][JV.PROP_VALUE]))) {
  757. let valIdx = private_getSharedStrIdx(theShtData.cells[cIdx][JV.PROP_VALUE]);
  758. cellObj.v = valIdx;
  759. }
  760. rowIdx2 = theYPos.indexOf(theShtData.cells[cIdx][JV.PROP_AREA][JV.PROP_BOTTOM]);
  761. colIdx2 = xPos.indexOf(theShtData.cells[cIdx][JV.PROP_AREA][JV.PROP_RIGHT]);
  762. if ((rowIdx2 - rowIdx1 > 1) || (colIdx2 - colIdx1 > 1)) {
  763. for (let i = 0; i < rowIdx2 - rowIdx1; i++) {
  764. for (let j = 0; j < colIdx2 - colIdx1; j++) {
  765. if (i === 0 && j === 0) continue;
  766. cellObj = rows[rowIdx1 - 1 + i].items[colIdx1 - 1 + j];
  767. cellObj.s = styleIdx;
  768. cellObj.isBlank = true;
  769. }
  770. }
  771. }
  772. }
  773. //3. then fill up rst
  774. for (let i = 0; i < rows.length; i++) {
  775. rst.push('<row r="' + (i + 1 + offsetY) + '" spans="1:' + spanX + '" ht="' + rows[i].height + '" customHeight="1">');
  776. for (let j = 0; j < rows[i].items.length; j++) {
  777. let cellObj = rows[i].items[j];
  778. if (cellObj.v === undefined) {
  779. rst.push('<c r="' + cellObj.r + '" s="' + cellObj.s + '"/>');
  780. } else {
  781. rst.push('<c r="' + cellObj.r + '" s="' + cellObj.s + '" t="s">');
  782. rst.push('<v>' + cellObj.v + '</v>');
  783. rst.push('</c>');
  784. }
  785. }
  786. rst.push('</row>');
  787. }
  788. //4. maybe need to dispose the memory
  789. //...
  790. };
  791. //private_cacheMergeBandBorderIdxs();
  792. if (sheetData) {
  793. //current sheet data
  794. currentPageMergePos = sheetData[JV.PAGE_SPECIAL_MERGE_POS];
  795. self_setDataEx(sheetData, yPos, 0);
  796. } else {
  797. //total data in one sheet
  798. let cnt = 0;
  799. for (let i = 0; i < pageData.items.length; i++) {
  800. let shtItemData = pageData.items[i];
  801. currentPageMergePos = shtItemData[JV.PAGE_SPECIAL_MERGE_POS];
  802. let tmpPos = yMultiPos[i];
  803. cellIdx = 0;
  804. self_setDataEx(shtItemData, tmpPos, cnt);
  805. cnt += tmpPos.length - 2;
  806. }
  807. }
  808. rst.push('</sheetData>');
  809. };
  810. private_pre_analyze_pos();
  811. rst.push(dftHeadXml + '\r\n');
  812. rst.push('<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  813. let colStr = private_getCellIdxStr(xPos.length - 3);
  814. rst.push('<dimension ref="A1:' + colStr + '' + yPos.length + '"/>');
  815. rst.push('<sheetViews><sheetView tabSelected="1" workbookViewId="0">');
  816. //rst.push('<selection sqref="A1:' + colStr + '1"/>');
  817. rst.push('<selection sqref="A1:A1"/>');
  818. rst.push('</sheetView></sheetViews>');
  819. rst.push('<sheetFormatPr defaultRowHeight="13.5"/>');
  820. private_setCols();
  821. private_setSheetData();
  822. private_setMergedCells();
  823. rst.push('<phoneticPr fontId="1" type="noConversion"/>');
  824. // rst.push('<pageMargins left="0.315" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
  825. rst.push('<pageMargins left="' + (parseFloat(pageData[JV.NODE_PAGE_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]) * 0.39375) +
  826. '" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
  827. let paperSizeIdx = JV.PAGES_SIZE_STR.indexOf(paperSize);
  828. let pStr = '';
  829. if (paperSizeIdx >= 0) {
  830. pStr = 'paperSize="' + JV.PAGES_SIZE_IDX[paperSizeIdx] + '"';
  831. }
  832. let orientationStr = (pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][0] > pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1])?'landscape':'portrait';
  833. if (currentPageMergePos) {
  834. orientationStr = (currentPageMergePos[JV.NODE_PAGE_SIZE][0] > currentPageMergePos[JV.NODE_PAGE_SIZE][1])?'landscape':'portrait';
  835. }
  836. rst.push('<pageSetup ' + pStr + ' fitToWidth="0" fitToHeight="0" orientation="' + orientationStr + '" />');
  837. rst.push('<headerFooter alignWithMargins="0"/>');
  838. rst.push('</worksheet>');
  839. return rst;
  840. }
  841. function mergeProperties(orgObj, newObj) {
  842. let orgPropArr = [], newPropArr = [];
  843. for (let p in orgObj) {
  844. orgPropArr.push(p);
  845. }
  846. for (let p in newObj) {
  847. newPropArr.push(p);
  848. }
  849. for (let i = 0; i < newPropArr.length; i++) {
  850. if (orgPropArr.indexOf(newPropArr[i]) < 0) {
  851. orgObj[newPropArr[i]] = newObj[newPropArr[i]];
  852. }
  853. }
  854. }
  855. module.exports = {
  856. exportExcel: function (pageData, paperSize, fName, options, custSheetNames, callback) {
  857. let rptOptions = ({singlePage: false, fileName: 'report'});
  858. if (options === 'true') {
  859. rptOptions.singlePage = true;
  860. }
  861. let isSinglePage = rptOptions.singlePage;
  862. let sheets = [];
  863. if (isSinglePage) {
  864. sheets.push({sheetName: '全部页'});
  865. } else {
  866. if (custSheetNames && custSheetNames.length === pageData.items.length) {
  867. for (let i = 0; i < pageData.items.length; i++) {
  868. sheets.push({sheetName: custSheetNames[i]});
  869. }
  870. } else {
  871. for (let i = 0; i < pageData.items.length; i++) {
  872. sheets.push({sheetName: '第' + (i + 1) + '页'});
  873. }
  874. }
  875. }
  876. //1.
  877. let file = '[Content_Types].xml';
  878. let data = writeContentTypes(sheets, isSinglePage);
  879. let zip = new JSZip();
  880. zip.file(file, data.join(''), {compression: 'DEFLATE'});
  881. //2.
  882. let zip_rels = zip.folder('_rels');
  883. file = '.rels';
  884. data = writeRootRels();
  885. zip_rels.file(file, data.join(''), {compression: 'DEFLATE'});
  886. //3.
  887. let zip_docProps = zip.folder('docProps');
  888. file = 'app.xml';
  889. data = writeApp(sheets, isSinglePage);
  890. zip_docProps.file(file, data.join(''), {compression: 'DEFLATE'});
  891. file = 'core.xml';
  892. data = writeCore();
  893. zip_docProps.file(file, data.join(''), {compression: 'DEFLATE'});
  894. //4.
  895. let zip_xl = zip.folder('xl');
  896. file = 'workbook.xml';
  897. data = writeXlWorkBook(sheets, isSinglePage);
  898. zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
  899. let zip_rels2 = zip_xl.folder('_rels');
  900. file = 'workbook.xml.rels';
  901. data = writeXlRels(sheets, isSinglePage);
  902. zip_rels2.file(file, data.join(''), {compression: 'DEFLATE'});
  903. //5.
  904. let zip_theme = zip_xl.folder('theme');
  905. file = 'theme1.xml';
  906. data = writeTheme();
  907. zip_theme.file(file, data, {compression: 'DEFLATE'});
  908. //6.
  909. let zip_worksheets = zip_xl.folder('worksheets');
  910. let sharedStrList = [], stylesObj = {};
  911. data = writeSheets(pageData, paperSize, sharedStrList, stylesObj, isSinglePage);
  912. if (isSinglePage) {
  913. for (let i = 0; i < 1; i++) {
  914. file = 'sheet' + (i + 1) + '.xml';
  915. zip_worksheets.file(file, data[i].join(''), {compression: 'DEFLATE'});
  916. }
  917. } else {
  918. for (let i = 0; i < data.length; i++) {
  919. file = 'sheet' + (i + 1) + '.xml';
  920. zip_worksheets.file(file, data[i].join(''), {compression: 'DEFLATE'});
  921. }
  922. }
  923. file = 'sharedStrings.xml';
  924. data = writeSharedString(sharedStrList);
  925. zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
  926. file = 'styles.xml';
  927. data = writeStyles(stylesObj);
  928. zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
  929. if (fName) {
  930. let newName = '' + (new Date()).valueOf();
  931. zip.generateNodeStream({type:'nodebuffer',streamFiles:true})
  932. .pipe(fs.createWriteStream(__dirname.slice(0, __dirname.length - 21) + '/tmp/' + newName + '.xlsx'))
  933. .on('finish', function () {
  934. // JSZip generates a readable stream with a "end" event,
  935. // but is piped here in a writable stream which emits a "finish" event.
  936. console.log(newName + ".xlsx was written.");
  937. if (callback) callback(newName);
  938. }
  939. );
  940. } else {
  941. //return zip.generateNodeStream({type:'nodebuffer',streamFiles:true});
  942. return zip;
  943. }
  944. },
  945. exportExcelInOneBook: function (pageDataArray, paperSize, fName, callback) {
  946. let me = this, newPageData = {};
  947. //1. 重新编排一下数据,把一份报表的pageData合并到一起作为一个Sheet输出(需要重新调整数据纵向坐标),多份报表数据就形成多个Sheet
  948. // -- 简单来说,就是重新包装数据
  949. try {
  950. // 1.1 newPageData外围属性
  951. let newContrl = {}, newFont = {}, newStyle = {};
  952. for (let i = 0; i < pageDataArray.length; i++) {
  953. mergeProperties(newContrl, pageDataArray[i][JV.NODE_CONTROL_COLLECTION]);
  954. mergeProperties(newFont, pageDataArray[i][JV.NODE_FONT_COLLECTION]);
  955. mergeProperties(newStyle, pageDataArray[i][JV.NODE_STYLE_COLLECTION]);
  956. }
  957. newPageData[JV.NODE_CONTROL_COLLECTION] = newContrl;
  958. newPageData[JV.NODE_FONT_COLLECTION] = newFont;
  959. newPageData[JV.NODE_STYLE_COLLECTION] = newStyle;
  960. newPageData[JV.NODE_PAGE_INFO] = pageDataArray[0][JV.NODE_PAGE_INFO];
  961. newPageData[JV.BAND_PROP_MERGE_BAND] = pageDataArray[0][JV.BAND_PROP_MERGE_BAND];
  962. // 1.2 重新设置pageDataArray的各个cell的Top/Bottom坐标
  963. let sheetNames = [], newPagePos = [];
  964. for (let i = 0; i < pageDataArray.length; i++) {
  965. let offsetY = 0;
  966. let mergeBand = {};
  967. mergeBand[JV.PROP_LEFT] = [];
  968. mergeBand[JV.PROP_RIGHT] = [];
  969. mergeBand[JV.PROP_TOP] = [];
  970. mergeBand[JV.PROP_BOTTOM] = [];
  971. newPagePos.push(mergeBand);
  972. mergeBand[JV.PROP_LEFT].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_LEFT]);
  973. mergeBand[JV.PROP_RIGHT].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_RIGHT]);
  974. sheetNames.push(pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_MAIN_INFO_RPT_NAME]);
  975. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  976. let maxY = 0, minY = 100000;
  977. if (pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS]) {
  978. let pos = pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_TOP][0] + offsetY;
  979. mergeBand[JV.PROP_TOP].push(pos);
  980. pos = pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_BOTTOM][0] + offsetY;
  981. mergeBand[JV.PROP_BOTTOM].push(pos);
  982. } else {
  983. mergeBand[JV.PROP_TOP].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_TOP] + offsetY);
  984. mergeBand[JV.PROP_BOTTOM].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_BOTTOM] + offsetY);
  985. }
  986. for (let k = 0; k < pageDataArray[i].items[j].cells.length; k++) {
  987. if (maxY < pageDataArray[i].items[j].cells[k][JV.PROP_AREA][JV.PROP_BOTTOM]) {
  988. maxY = pageDataArray[i].items[j].cells[k][JV.PROP_AREA][JV.PROP_BOTTOM];
  989. }
  990. if (minY > pageDataArray[i].items[j].cells[k][JV.PROP_AREA][JV.PROP_TOP]) {
  991. minY = pageDataArray[i].items[j].cells[k][JV.PROP_AREA][JV.PROP_TOP];
  992. }
  993. pageDataArray[i].items[j].cells[k][JV.PROP_AREA][JV.PROP_BOTTOM] += offsetY;
  994. pageDataArray[i].items[j].cells[k][JV.PROP_AREA][JV.PROP_TOP] += offsetY;
  995. }
  996. offsetY += (maxY - minY);
  997. }
  998. }
  999. //2. newPageData的items属性
  1000. newPageData.items = [];
  1001. for (let i = 0; i < pageDataArray.length; i++) {
  1002. let pageItem = {};
  1003. pageItem[JV.PROP_PAGE_SEQ] = i + 1;
  1004. pageItem[JV.PROP_CELLS] = [];
  1005. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1006. for (let k = 0; k < pageDataArray[i].items[j].cells.length; k++) {
  1007. pageItem[JV.PROP_CELLS].push(pageDataArray[i].items[j].cells[k]);
  1008. }
  1009. }
  1010. newPagePos[i][JV.NODE_PAGE_SIZE] = pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE];
  1011. pageItem[JV.PAGE_SPECIAL_MERGE_POS] = newPagePos[i];
  1012. newPageData.items.push(pageItem);
  1013. }
  1014. //3. everything is ok, then call me
  1015. me.exportExcel(newPageData, paperSize, fName, 'false', sheetNames, callback);
  1016. // fsUtil.writeObjToFile(newPageData, 'D:/GitHome/ConstructionOperation/tmp/combinedHeader.js');
  1017. } catch (e) {
  1018. console.log(e);
  1019. }
  1020. }
  1021. }