rpt_excel_util.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**
  2. * Created by Tony on 2017/4/1.
  3. */
  4. var JV = require('../rpt_component/Jpc_ValueDefine');
  5. var fs = require('fs');
  6. var strUtil = require('../../../public/stringUtil');
  7. var jpcCmnHelper = require('../rpt_component/helper/Jpc_Helper_Common');
  8. var DPI = jpcCmnHelper.getScreenDPI()[0];
  9. const dftHeadXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
  10. function writeContentTypes(sheets) {
  11. var rst = [];
  12. rst.push(dftHeadXml + '\r\n');
  13. rst.push('<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">');
  14. //...
  15. rst.push('<Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/>');
  16. rst.push('<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>');
  17. rst.push('<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>');
  18. rst.push('<Default Extension="xml" ContentType="application/xml"/>');
  19. rst.push('<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>');
  20. rst.push('<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>');
  21. rst.push('<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>');
  22. for (var i = 0; i < sheets.length; i++) {
  23. rst.push('<Override PartName="/xl/worksheets/' + sheets[i].sheetName + '.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>')
  24. }
  25. rst.push('<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>');
  26. rst.push('</Types>');
  27. return rst;
  28. }
  29. function writeRootRels(){
  30. var rst = [];
  31. rst.push(dftHeadXml + '\r\n');
  32. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  33. rst.push('<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>');
  34. rst.push('<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>');
  35. rst.push('<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>');
  36. rst.push('</Relationships>');
  37. return rst;
  38. }
  39. function writeApp(sheets) {
  40. var rst = [];
  41. rst.push(dftHeadXml + '\r\n');
  42. rst.push('<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">');
  43. rst.push('<Application>Microsoft Excel</Application>');
  44. rst.push('<DocSecurity>0</DocSecurity>');
  45. rst.push('<ScaleCrop>false</ScaleCrop>');
  46. rst.push('<HeadingPairs>');
  47. rst.push('<vt:vector size="2" baseType="variant">');
  48. rst.push('<vt:variant><vt:lpstr>¹¤×÷±í</vt:lpstr></vt:variant>');
  49. rst.push('<vt:variant><vt:i4>' + sheets.length + '</vt:i4></vt:variant>');
  50. rst.push('</vt:vector>');
  51. rst.push('</HeadingPairs>');
  52. rst.push('<TitlesOfParts>');
  53. rst.push('<vt:vector size="' + sheets.length + '" baseType="lpstr">');
  54. for (var i = 0; i < sheets.length; i++) {
  55. rst.push('<vt:lpstr>' + sheets[i].sheetName + '</vt:lpstr>')
  56. }
  57. rst.push('</vt:vector>');
  58. rst.push('</TitlesOfParts>');
  59. rst.push('<Company>SmartCost</Company>');
  60. rst.push('<LinksUpToDate>false</LinksUpToDate>');
  61. rst.push('<SharedDoc>false</SharedDoc>');
  62. rst.push('<HyperlinksChanged>false</HyperlinksChanged>');
  63. rst.push('<AppVersion>12.0000</AppVersion>');
  64. //rst.push('');
  65. rst.push('</Properties>');
  66. return rst;
  67. }
  68. function writeCore() {
  69. var rst = [];
  70. p_fillZero = function(val){
  71. var rst = val;
  72. if (val < 10) {
  73. rst = '0' + val;
  74. }
  75. return rst;
  76. };
  77. rst.push(dftHeadXml + '\r\n');
  78. 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">');
  79. rst.push('<dc:creator>SmartCost</dc:creator>');
  80. rst.push('<cp:lastModifiedBy>SmartCost</cp:lastModifiedBy>');
  81. var dt = new Date(), dtStr = dt.getFullYear() + '-' + p_fillZero(dt.getMonth()) + '-' + p_fillZero(dt.getDate()) + 'T' +
  82. p_fillZero(dt.getHours()) + ':' + p_fillZero(dt.getMinutes()) + ':' + p_fillZero(dt.getSeconds()) + 'Z';
  83. rst.push('<dcterms:created xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:created>');
  84. rst.push('<dcterms:modified xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:modified>');
  85. //rst.push('');
  86. rst.push('</cp:coreProperties>');
  87. return rst;
  88. }
  89. function writeXlWorkBook(sheets){
  90. var rst = [];
  91. rst.push(dftHeadXml + '\r\n');
  92. rst.push('<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  93. rst.push('<fileVersion appName="xl" lastEdited="4" lowestEdited="4" rupBuild="4505"/>');
  94. rst.push('<workbookPr defaultThemeVersion="124226"/>');
  95. rst.push('<bookViews><workbookView xWindow="360" yWindow="345" windowWidth="14655" windowHeight="4305"/></bookViews>');
  96. rst.push('<sheets>');
  97. for (var i = 0; i < sheets.length; i++) {
  98. rst.push('<sheet name="' + sheets[i].sheetName + '" sheetId="' + (i + 1) + 'r:id="rId' + (i + 1) + '1"/>')
  99. }
  100. rst.push('</sheets>');
  101. rst.push('<calcPr calcId="124519"/>');
  102. //rst.push('');
  103. rst.push('</workbook>');
  104. return rst;
  105. }
  106. function writeXlRels(sheets){
  107. var rst = [], idx = 1;
  108. rst.push(dftHeadXml + '\r\n');
  109. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  110. for (var i = 0; i < sheets.length; i++) {
  111. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/' + sheets[i].sheetName + '.xml/>"')
  112. idx++;
  113. }
  114. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>');
  115. idx++;
  116. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>');
  117. idx++;
  118. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/>');
  119. //rst.push('');
  120. rst.push('</Relationships>');
  121. return rst;
  122. }
  123. function writeTheme(){
  124. var rst = fs.readFileSync('./excel_base_files/theme1.xml', 'utf8', 'r');
  125. return rst;
  126. }
  127. function writeStyles(styleList){
  128. //
  129. }
  130. function writeSharedString(sharedStrList){
  131. //
  132. }
  133. function writeSheets(pageData){
  134. var rst = [], sharedStrList = [];
  135. for (var i = 0; i < pageData.items.length; i++) {
  136. //
  137. }
  138. }
  139. function writeSheet(sheetData, sharedStrList, styleList){
  140. var rst = [], xPos = [0], yPos = [0], headerStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  141. private_pre_analyze_pos = function(){
  142. var cell, pos;
  143. sheetData.cells.sort(function(cell1, cell2) {
  144. var rst = 0;
  145. if (cell1[JV.PROP_AREA][JV.PROP_TOP] > cell2[JV.PROP_AREA][JV.PROP_TOP]) {
  146. rst = 1;
  147. } else if (cell1[JV.PROP_AREA][JV.PROP_TOP] < cell2[JV.PROP_AREA][JV.PROP_TOP]) {
  148. rst = -1;
  149. } else {
  150. if (cell1[JV.PROP_AREA][JV.PROP_LEFT] > cell2[JV.PROP_AREA][JV.PROP_LEFT]) {
  151. rst = 1;
  152. } else if (cell1[JV.PROP_AREA][JV.PROP_LEFT] < cell2[JV.PROP_AREA][JV.PROP_LEFT]) {
  153. rst = -1;
  154. }
  155. }
  156. });
  157. for (var i = 0; i < sheetData.cells.length; i++) {
  158. cell = sheetData.cells[i];
  159. pos = cell[JV.PROP_AREA][JV.PROP_LEFT];
  160. if (xPos.indexOf(pos) < 0) xPos.push(pos);
  161. pos = cell[JV.PROP_AREA][JV.PROP_RIGHT];
  162. if (xPos.indexOf(pos) < 0) xPos.push(pos);
  163. pos = cell[JV.PROP_AREA][JV.PROP_TOP];
  164. if (yPos.indexOf(pos) < 0) yPos.push(pos);
  165. pos = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  166. if (yPos.indexOf(pos) < 0) yPos.push(pos);
  167. }
  168. xPos.sort();
  169. yPos.sort();
  170. };
  171. private_getCellIdxStr = function(idx){
  172. var rst = 'A';
  173. if (idx < 26) {
  174. rst = headerStr[idx];
  175. } else if (idx < 26*26+26) {
  176. var ti = Math.floor(idx / 26), tj = idx % 26;
  177. rst = headerStr[ti - 1] + headerStr[tj];
  178. } else if (idx < 26*26*26+26) {
  179. var ti = Math.floor(idx / (26*26)), tj = Math.floor((idx - ti * 26*26) / 26), tk = idx % 26;
  180. rst = headerStr[ti - 1] + headerStr[tj-1] + headerStr[tk];
  181. }
  182. return rst;
  183. };
  184. private_getSharedStrIdx = function(val) {
  185. var rst = sharedStrList.indexOf(val);
  186. if (rst < 0) {
  187. sharedStrList.push(val);
  188. rst = sharedStrList.length - 1;
  189. }
  190. return rst;
  191. };
  192. private_getStyleIdx = function(cell) {
  193. var rst = 1;
  194. //
  195. return rst;
  196. };
  197. private_setCols = function(){
  198. rst.push('<cols>');
  199. var w = 0;
  200. for (var i = 1; i < xPos.length; i++) {
  201. w = 1.0 * (xPos[i] - xPos[i - 1]) / DPI;
  202. w = Math.round(w * 1000) / 1000;
  203. rst.push('<col min="1" max="1" width="' + w + '" customWidth="1"/>');
  204. }
  205. rst.push('</cols>');
  206. };
  207. private_setMergedCells = function() {
  208. var cell, idxR, idxL, idxT, idxB, cnt = 0;
  209. rst.push('<mergeCells count="?">');
  210. var startIdx = rst.length - 1;
  211. for (var i = 0; i < sheetData.cells.length; i++) {
  212. cell = sheetData.cells[i];
  213. idxR = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_RIGHT]);
  214. idxL = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_LEFT]);
  215. idxB = yPos.indexOf(cell[JV.PROP_AREA][JV.PROP_BOTTOM]);
  216. idxT = yPos.indexOf(cell[JV.PROP_AREA][JV.PROP_TOP]);
  217. if (idxR - idxL > 1 || idxB - idxT > 1) {
  218. rst.push('<mergeCell ref="' + private_getCellIdxStr(idxL - 1) + idxT + ':' + private_getCellIdxStr(idxR - 2) + (idxB - 1) + '"/>');
  219. cnt++;
  220. }
  221. }
  222. rst[startIdx] = '<mergeCells count="' + cnt + '">';
  223. rst.push('</mergeCells>');
  224. };
  225. private_setSheetData = function(){
  226. rst.push('<sheetData>');
  227. var spanX = xPos.length - 1, cellIdx = 0, h = 0,
  228. hasMoreCols = true, nextColIdx = -1,
  229. nextRowIdx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
  230. for (var i = 0; i < yPos.length - 1; i++) {
  231. if (i === 0) {
  232. h = 1.0 * (yPos[i]) / DPI;
  233. } else {
  234. h = 1.0 * (yPos[i] - yPos[i - 1]) / DPI;
  235. }
  236. h = Math.round(h * 1000) / 1000;
  237. rst.push('<row r="' + (i+1) + '" spans="1:' + spanX + '" ht="' + h + '" customHeight="1">');
  238. //then put the cells of this row
  239. var colIdxStr = '';
  240. hasMoreCols = true;
  241. while (nextRowIdx < i) {
  242. if (cellIdx >= sheetData.cells.length || nextRowIdx > i) {
  243. break;
  244. } else {
  245. cellIdx++;
  246. nextRowIdx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
  247. }
  248. }
  249. if (nextRowIdx > i) {
  250. hasMoreCols = false;
  251. }
  252. nextColIdx = xPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_LEFT]);
  253. for (var j = 0; j < xPos.length; j++) {
  254. colIdxStr = private_getCellIdxStr(j);
  255. if (hasMoreCols) {
  256. if (nextColIdx == j) {
  257. var styleIdx = private_getStyleIdx(sheetData.cells[cellIdx]);
  258. if (strUtil.isEmptyString(sheetData.cells[cellIdx][JV.PROP_VALUE])) {
  259. rst.push('<c r="' + colIdxStr + (i+1) + '" s="' + styleIdx + '"/>');
  260. //should setup the right style instead!
  261. } else {
  262. var valIdx = private_getSharedStrIdx(sheetData.cells[cellIdx][JV.PROP_VALUE]);
  263. rst.push('<c r="' + colIdxStr + (i+1) + '" s="' + styleIdx + '">');
  264. rst.push('<v>' + valIdx + '</v>');
  265. rst.push('</c>');
  266. }
  267. cellIdx++;
  268. nextRowIdx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
  269. if (nextRowIdx > i) {
  270. hasMoreCols = false;
  271. } else {
  272. nextColIdx = xPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_LEFT]);
  273. }
  274. } else if (nextColIdx < 0) {
  275. //impossible!
  276. console.log('has abnormal case!');
  277. hasMoreCols = false;
  278. } else {
  279. rst.push('<c r="' + colIdxStr + (i+1) + '" s="1"/>');
  280. }
  281. } else {
  282. rst.push('<c r="' + colIdxStr + (i+1) + '" s="1"/>');
  283. }
  284. }
  285. rst.push('</row');
  286. }
  287. //sheetData.cells.length
  288. rst.push('</sheetData>');
  289. };
  290. private_pre_analyze_pos();
  291. rst.push(dftHeadXml + '\r\n');
  292. rst.push('<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  293. var colStr = private_getCellIdxStr(xPos.length - 2);
  294. rst.push('<dimension ref="A1:' + colStr + '' + yPos.length + '"/>');
  295. rst.push('<sheetViews><sheetView tabSelected="1" workbookViewId="0">');
  296. rst.push('<selection sqref="A1:' + colStr + '1"/>');
  297. rst.push('</sheetView></sheetViews>');
  298. rst.push('<sheetFormatPr defaultRowHeight="13.5"/>');
  299. private_setCols();
  300. private_setSheetData();
  301. private_setMergedCells();
  302. rst.push('<phoneticPr fontId="1" type="noConversion"/>');
  303. rst.push('<pageMargins left="0.315" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
  304. //rst.push('<pageSetup paperSize="9" fitToWidth="0" fitToHeight="0" orientation="landscape" horizontalDpi="300" verticalDpi="300"/>');
  305. rst.push('<headerFooter alignWithMargins="0"/>');
  306. rst.push('</worksheet>');
  307. //rst.push('');
  308. return rst;
  309. }
  310. module.exports = {
  311. exportExcel: function (pageData, options) {
  312. var rptOptions = (options || {singlePage: false, fileName: 'report'});
  313. }
  314. }