rpt_excel_util.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * Created by Tony on 2017/4/1.
  3. */
  4. var JV = require('../rpt_component/Jpc_ValueDefine');
  5. var fs = require('fs');
  6. const dftHeadXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
  7. function writeContentTypes(sheets) {
  8. var rst = [];
  9. rst.push(dftHeadXml + '\r\n');
  10. rst.push('<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">');
  11. //...
  12. rst.push('<Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/>');
  13. rst.push('<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>');
  14. rst.push('<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>');
  15. rst.push('<Default Extension="xml" ContentType="application/xml"/>');
  16. rst.push('<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>');
  17. rst.push('<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>');
  18. rst.push('<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>');
  19. for (var i = 0; i < sheets.length; i++) {
  20. rst.push('<Override PartName="/xl/worksheets/' + sheets[i].sheetName + '.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>')
  21. }
  22. rst.push('<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>');
  23. rst.push('</Types>');
  24. return rst;
  25. }
  26. function writeRootRels(){
  27. var rst = [];
  28. rst.push(dftHeadXml + '\r\n');
  29. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  30. rst.push('<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>');
  31. rst.push('<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>');
  32. rst.push('<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>');
  33. rst.push('</Relationships>');
  34. return rst;
  35. }
  36. function writeApp(sheets) {
  37. var rst = [];
  38. rst.push(dftHeadXml + '\r\n');
  39. rst.push('<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">');
  40. rst.push('<Application>Microsoft Excel</Application>');
  41. rst.push('<DocSecurity>0</DocSecurity>');
  42. rst.push('<ScaleCrop>false</ScaleCrop>');
  43. rst.push('<HeadingPairs>');
  44. rst.push('<vt:vector size="2" baseType="variant">');
  45. rst.push('<vt:variant><vt:lpstr>¹¤×÷±í</vt:lpstr></vt:variant>');
  46. rst.push('<vt:variant><vt:i4>' + sheets.length + '</vt:i4></vt:variant>');
  47. rst.push('</vt:vector>');
  48. rst.push('</HeadingPairs>');
  49. rst.push('<TitlesOfParts>');
  50. rst.push('<vt:vector size="' + sheets.length + '" baseType="lpstr">');
  51. for (var i = 0; i < sheets.length; i++) {
  52. rst.push('<vt:lpstr>' + sheets[i].sheetName + '</vt:lpstr>')
  53. }
  54. rst.push('</vt:vector>');
  55. rst.push('</TitlesOfParts>');
  56. rst.push('<Company>SmartCost</Company>');
  57. rst.push('<LinksUpToDate>false</LinksUpToDate>');
  58. rst.push('<SharedDoc>false</SharedDoc>');
  59. rst.push('<HyperlinksChanged>false</HyperlinksChanged>');
  60. rst.push('<AppVersion>12.0000</AppVersion>');
  61. //rst.push('');
  62. rst.push('</Properties>');
  63. return rst;
  64. }
  65. function writeCore() {
  66. var rst = [];
  67. p_fillZero = function(val){
  68. var rst = val;
  69. if (val < 10) {
  70. rst = '0' + val;
  71. }
  72. return rst;
  73. };
  74. rst.push(dftHeadXml + '\r\n');
  75. 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">');
  76. rst.push('<dc:creator>SmartCost</dc:creator>');
  77. rst.push('<cp:lastModifiedBy>SmartCost</cp:lastModifiedBy>');
  78. var dt = new Date(), dtStr = dt.getFullYear() + '-' + p_fillZero(dt.getMonth()) + '-' + p_fillZero(dt.getDate()) + 'T' +
  79. p_fillZero(dt.getHours()) + ':' + p_fillZero(dt.getMinutes()) + ':' + p_fillZero(dt.getSeconds()) + 'Z';
  80. rst.push('<dcterms:created xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:created>');
  81. rst.push('<dcterms:modified xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:modified>');
  82. //rst.push('');
  83. rst.push('</cp:coreProperties>');
  84. return rst;
  85. }
  86. function writeXlWorkBook(sheets){
  87. var rst = [];
  88. rst.push(dftHeadXml + '\r\n');
  89. rst.push('<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  90. rst.push('<fileVersion appName="xl" lastEdited="4" lowestEdited="4" rupBuild="4505"/>');
  91. rst.push('<workbookPr defaultThemeVersion="124226"/>');
  92. rst.push('<bookViews><workbookView xWindow="360" yWindow="345" windowWidth="14655" windowHeight="4305"/></bookViews>');
  93. rst.push('<sheets>');
  94. for (var i = 0; i < sheets.length; i++) {
  95. rst.push('<sheet name="' + sheets[i].sheetName + '" sheetId="' + (i + 1) + 'r:id="rId' + (i + 1) + '1"/>')
  96. }
  97. rst.push('</sheets>');
  98. rst.push('<calcPr calcId="124519"/>');
  99. //rst.push('');
  100. rst.push('</workbook>');
  101. return rst;
  102. }
  103. function writeXlRels(sheets){
  104. var rst = [], idx = 1;
  105. rst.push(dftHeadXml + '\r\n');
  106. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  107. for (var i = 0; i < sheets.length; i++) {
  108. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/' + sheets[i].sheetName + '.xml/>"')
  109. idx++;
  110. }
  111. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>');
  112. idx++;
  113. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>');
  114. idx++;
  115. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/>');
  116. //rst.push('');
  117. rst.push('</Relationships>');
  118. return rst;
  119. }
  120. function writeTheme(){
  121. var rst = fs.readFileSync('./excel_base_files/theme1.xml', 'utf8', 'r');
  122. return rst;
  123. }
  124. function writeStyles(pageData){
  125. //
  126. }
  127. function writeSharedString(pageData){
  128. //
  129. }
  130. function writeSheets(pageData){
  131. var rst = [], sharedStrList = [];
  132. for (var i = 0; i < pageData.items.length; i++) {
  133. //
  134. }
  135. }
  136. function writeSheet(sheetData, sharedStrList){
  137. var rst = [], x = [0], y = [0], headerStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  138. private_pre_analyze_pos = function(){
  139. var cell, pos;
  140. for (var i = 0; i < sheetData.cells.length; i++) {
  141. cell = sheetData.cells[i];
  142. pos = cell[JV.PROP_AREA][JV.PROP_LEFT];
  143. if (x.indexOf(pos) < 0) x.push(pos);
  144. pos = cell[JV.PROP_AREA][JV.PROP_RIGHT];
  145. if (x.indexOf(pos) < 0) x.push(pos);
  146. pos = cell[JV.PROP_AREA][JV.PROP_TOP];
  147. if (y.indexOf(pos) < 0) y.push(pos);
  148. pos = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  149. if (y.indexOf(pos) < 0) y.push(pos);
  150. }
  151. x.sort();
  152. y.sort();
  153. };
  154. private_getCellIdxStr = function(idx){
  155. var rst = 'A', prefix = '', tmpIdx = idx;
  156. if (tmpIdx > 26) {
  157. var ti = Math.floor(tmpIdx / 26), tj = tmpIdx % 26;
  158. if (ti < 26) {
  159. //
  160. } else {
  161. prefix = ''
  162. }
  163. } else {
  164. //
  165. }
  166. //if (idx < )
  167. };
  168. private_getStrIdx = function(val) {
  169. //
  170. };
  171. private_pre_analyze_pos();
  172. rst.push(dftHeadXml + '\r\n');
  173. rst.push('<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  174. rst.push('<dimension ref="A1:U4332"/>');
  175. rst.push('');
  176. rst.push('');
  177. rst.push('');
  178. rst.push('');
  179. rst.push('');
  180. rst.push('');
  181. rst.push('');
  182. rst.push('');
  183. rst.push('');
  184. rst.push('');
  185. //rst.push('');
  186. return rst;
  187. }
  188. module.exports = {
  189. exportExcel: function (pageData, options) {
  190. var rptOptions = (options || {singlePage: false, fileName: 'report'});
  191. }
  192. }