rpt_excel_util.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. var rst = [];
  132. if (sharedStrList && sharedStrList.length > 0) {
  133. rst.push(dftHeadXml + '\r\n');
  134. rst.push('<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="' + sharedStrList.length + '" uniqueCount="' + sharedStrList.length + '">');
  135. for (var i = 0; i < sharedStrList.length; i++) {
  136. rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
  137. }
  138. rst.push('</sst>');
  139. }
  140. return rst;
  141. }
  142. function writeSheets(pageData, sharedStrList, styleList){
  143. var rst = [];
  144. for (var i = 0; i < pageData.items.length; i++) {
  145. rst.push(writeSheet(pageData.items[i], sharedStrList, styleList));
  146. }
  147. return rst;
  148. }
  149. function writeSheet(sheetData, sharedStrList, styleList){
  150. var rst = [], xPos = [], yPos = [], headerStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  151. xPos.push(0);
  152. yPos.push(0);
  153. private_pre_analyze_pos = function(){
  154. var cell, pos;
  155. sheetData.cells.sort(function(cell1, cell2) {
  156. var rst = 0;
  157. if (cell1[JV.PROP_AREA][JV.PROP_TOP] > cell2[JV.PROP_AREA][JV.PROP_TOP]) {
  158. rst = 1;
  159. } else if (cell1[JV.PROP_AREA][JV.PROP_TOP] < cell2[JV.PROP_AREA][JV.PROP_TOP]) {
  160. rst = -1;
  161. } else {
  162. if (cell1[JV.PROP_AREA][JV.PROP_LEFT] > cell2[JV.PROP_AREA][JV.PROP_LEFT]) {
  163. rst = 1;
  164. } else if (cell1[JV.PROP_AREA][JV.PROP_LEFT] < cell2[JV.PROP_AREA][JV.PROP_LEFT]) {
  165. rst = -1;
  166. }
  167. }
  168. return rst;
  169. });
  170. for (var i = 0; i < sheetData.cells.length; i++) {
  171. cell = sheetData.cells[i];
  172. pos = cell[JV.PROP_AREA][JV.PROP_LEFT];
  173. if (xPos.indexOf(pos) < 0) xPos.push(pos);
  174. pos = cell[JV.PROP_AREA][JV.PROP_RIGHT];
  175. if (xPos.indexOf(pos) < 0) xPos.push(pos);
  176. pos = cell[JV.PROP_AREA][JV.PROP_TOP];
  177. if (yPos.indexOf(pos) < 0) yPos.push(pos);
  178. pos = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  179. if (yPos.indexOf(pos) < 0) yPos.push(pos);
  180. }
  181. xPos.sort(private_array_sort);
  182. yPos.sort(private_array_sort);
  183. };
  184. private_array_sort = function(i1, i2){
  185. var rst = 0;
  186. if (i1 > i2) {rst = 1} else
  187. if (i1 < i2) rst = -1;
  188. return rst;
  189. };
  190. private_getCellIdxStr = function(idx){
  191. var rst = 'A';
  192. if (idx < 26) {
  193. rst = headerStr[idx];
  194. } else if (idx < 26*26+26) {
  195. var ti = Math.floor(idx / 26), tj = idx % 26;
  196. rst = headerStr[ti - 1] + headerStr[tj];
  197. } else if (idx < 26*26*26+26) {
  198. var ti = Math.floor(idx / (26*26)), tj = Math.floor((idx - ti * 26*26) / 26), tk = idx % 26;
  199. rst = headerStr[ti - 1] + headerStr[tj-1] + headerStr[tk];
  200. }
  201. return rst;
  202. };
  203. private_getSharedStrIdx = function(val) {
  204. var rst = sharedStrList.indexOf(val);
  205. if (rst < 0) {
  206. sharedStrList.push(val);
  207. rst = sharedStrList.length - 1;
  208. }
  209. return rst;
  210. };
  211. private_getStyleIdx = function(cell) {
  212. var rst = 1;
  213. //
  214. return rst;
  215. };
  216. private_setCols = function(){
  217. //remark: 1 excel width = 2.117 mm
  218. rst.push('<cols>');
  219. var w = 0;
  220. for (var i = 1; i < xPos.length; i++) {
  221. w = 1.0 * (xPos[i] - xPos[i - 1]) / DPI * 25.4 / 2.117;
  222. w = Math.round(w * 1000) / 1000;
  223. rst.push('<col min="' + i +'" max="' + i +'" width="' + w + '" customWidth="1"/>');
  224. }
  225. rst.push('</cols>');
  226. };
  227. private_setMergedCells = function() {
  228. var cell, idxR, idxL, idxT, idxB, cnt = 0;
  229. rst.push('<mergeCells count="?">');
  230. var startIdx = rst.length - 1;
  231. for (var i = 0; i < sheetData.cells.length; i++) {
  232. cell = sheetData.cells[i];
  233. idxR = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_RIGHT]);
  234. idxL = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_LEFT]);
  235. idxB = yPos.indexOf(cell[JV.PROP_AREA][JV.PROP_BOTTOM]);
  236. idxT = yPos.indexOf(cell[JV.PROP_AREA][JV.PROP_TOP]);
  237. if (idxR - idxL > 1 || idxB - idxT > 1) {
  238. rst.push('<mergeCell ref="' + private_getCellIdxStr(idxL - 1) + idxT + ':' + private_getCellIdxStr(idxR - 2) + (idxB - 1) + '"/>');
  239. cnt++;
  240. }
  241. }
  242. rst[startIdx] = '<mergeCells count="' + cnt + '">';
  243. rst.push('</mergeCells>');
  244. };
  245. private_setSheetData = function(){
  246. //remark: 1 excel height = 0.3612 mm
  247. rst.push('<sheetData>');
  248. var spanX = xPos.length - 1, cellIdx = 0, h = 0,
  249. hasMoreCols = true, nextColIdx = -1,
  250. nextRowIdx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
  251. for (var i = 1; i < yPos.length - 1; i++) {
  252. if (i === 0) {
  253. h = 1.0 * (yPos[i]) / DPI * 25.4 / 0.3612;
  254. } else {
  255. h = 1.0 * (yPos[i] - yPos[i - 1]) / DPI * 25.4 / 0.3612;
  256. }
  257. h = Math.round(h * 1000) / 1000;
  258. rst.push('<row r="' + i + '" spans="1:' + spanX + '" ht="' + h + '" customHeight="1">');
  259. //then put the cells of this row
  260. var colIdxStr = '';
  261. hasMoreCols = true;
  262. while (nextRowIdx < i) {
  263. if (cellIdx >= sheetData.cells.length || nextRowIdx > i) {
  264. break;
  265. } else {
  266. cellIdx++;
  267. nextRowIdx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
  268. }
  269. }
  270. if (nextRowIdx > i) {
  271. hasMoreCols = false;
  272. }
  273. nextColIdx = xPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_LEFT]);
  274. for (var j = 0; j < xPos.length; j++) {
  275. colIdxStr = private_getCellIdxStr(j);
  276. if (hasMoreCols) {
  277. if (nextColIdx == j) {
  278. var styleIdx = private_getStyleIdx(sheetData.cells[cellIdx]);
  279. if (strUtil.isEmptyString(sheetData.cells[cellIdx][JV.PROP_VALUE])) {
  280. rst.push('<c r="' + colIdxStr + i + '" s="' + styleIdx + '"/>');
  281. //should setup the right style instead!
  282. } else {
  283. var valIdx = private_getSharedStrIdx(sheetData.cells[cellIdx][JV.PROP_VALUE]);
  284. rst.push('<c r="' + colIdxStr + i + '" s="' + styleIdx + '">');
  285. rst.push('<v>' + valIdx + '</v>');
  286. rst.push('</c>');
  287. }
  288. cellIdx++;
  289. if (cellIdx < sheetData.cells.length) {
  290. nextRowIdx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
  291. if (nextRowIdx > i) {
  292. hasMoreCols = false;
  293. } else {
  294. nextColIdx = xPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_LEFT]);
  295. }
  296. } else {
  297. hasMoreCols = false;
  298. }
  299. } else if (nextColIdx < 0) {
  300. //impossible!
  301. console.log('has abnormal case!');
  302. hasMoreCols = false;
  303. } else {
  304. rst.push('<c r="' + colIdxStr + i + '" s="1"/>');
  305. }
  306. } else {
  307. rst.push('<c r="' + colIdxStr + i + '" s="1"/>');
  308. }
  309. }
  310. rst.push('</row>');
  311. }
  312. //sheetData.cells.length
  313. rst.push('</sheetData>');
  314. };
  315. private_pre_analyze_pos();
  316. rst.push(dftHeadXml + '\r\n');
  317. rst.push('<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  318. var colStr = private_getCellIdxStr(xPos.length - 2);
  319. rst.push('<dimension ref="A1:' + colStr + '' + yPos.length + '"/>');
  320. rst.push('<sheetViews><sheetView tabSelected="1" workbookViewId="0">');
  321. rst.push('<selection sqref="A1:' + colStr + '1"/>');
  322. rst.push('</sheetView></sheetViews>');
  323. rst.push('<sheetFormatPr defaultRowHeight="13.5"/>');
  324. private_setCols();
  325. private_setSheetData();
  326. private_setMergedCells();
  327. rst.push('<phoneticPr fontId="1" type="noConversion"/>');
  328. rst.push('<pageMargins left="0.315" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
  329. //rst.push('<pageSetup paperSize="9" fitToWidth="0" fitToHeight="0" orientation="landscape" horizontalDpi="300" verticalDpi="300"/>');
  330. rst.push('<headerFooter alignWithMargins="0"/>');
  331. rst.push('</worksheet>');
  332. //rst.push('');
  333. return rst;
  334. }
  335. module.exports = {
  336. exportExcel: function (pageData, options) {
  337. var rptOptions = (options || {singlePage: false, fileName: 'report'});
  338. }
  339. ,testWriteContentTypes: function(sheets) {
  340. return writeContentTypes(sheets);
  341. }
  342. ,testWriteRootRels: function() {
  343. return writeRootRels();
  344. }
  345. ,testWriteApp: function(sheets) {
  346. return writeApp(sheets);
  347. }
  348. ,testWriteCore: function() {
  349. return writeCore();
  350. }
  351. ,testWriteXlWorkBook: function(sheets) {
  352. return writeXlWorkBook(sheets);
  353. }
  354. ,testWriteXlRels: function(sheets) {
  355. return writeXlRels(sheets);
  356. }
  357. ,testWriteSheets: function(pageData, sharedStrList, styleList){
  358. return writeSheets(pageData, sharedStrList, styleList);
  359. }
  360. ,testWriteSharedString: function(sharedStrList){
  361. return writeSharedString(sharedStrList);
  362. }
  363. }