rpt_excel_util.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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 JSZip = require("jszip");
  7. var strUtil = require('../../../public/stringUtil');
  8. var jpcCmnHelper = require('../rpt_component/helper/Jpc_Helper_Common');
  9. var DPI = jpcCmnHelper.getScreenDPI()[0];
  10. const dftHeadXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
  11. function writeContentTypes(sheets) {
  12. var rst = [];
  13. rst.push(dftHeadXml + '\r\n');
  14. rst.push('<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">');
  15. //...
  16. rst.push('<Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/>');
  17. rst.push('<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>');
  18. rst.push('<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>');
  19. rst.push('<Default Extension="xml" ContentType="application/xml"/>');
  20. rst.push('<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>');
  21. rst.push('<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>');
  22. rst.push('<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>');
  23. for (var i = 0; i < sheets.length; i++) {
  24. rst.push('<Override PartName="/xl/worksheets/sheet' + (i + 1) + '.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>')
  25. }
  26. rst.push('<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>');
  27. rst.push('</Types>');
  28. return rst;
  29. }
  30. function writeRootRels(){
  31. var rst = [];
  32. rst.push(dftHeadXml + '\r\n');
  33. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  34. rst.push('<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>');
  35. rst.push('<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>');
  36. rst.push('<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>');
  37. rst.push('</Relationships>');
  38. return rst;
  39. }
  40. function writeApp(sheets) {
  41. var rst = [];
  42. rst.push(dftHeadXml + '\r\n');
  43. rst.push('<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">');
  44. rst.push('<Application>Microsoft Excel</Application>');
  45. rst.push('<DocSecurity>0</DocSecurity>');
  46. rst.push('<ScaleCrop>false</ScaleCrop>');
  47. rst.push('<HeadingPairs>');
  48. rst.push('<vt:vector size="2" baseType="variant">');
  49. rst.push('<vt:variant><vt:lpstr>工作表</vt:lpstr></vt:variant>');
  50. rst.push('<vt:variant><vt:i4>' + sheets.length + '</vt:i4></vt:variant>');
  51. rst.push('</vt:vector>');
  52. rst.push('</HeadingPairs>');
  53. rst.push('<TitlesOfParts>');
  54. rst.push('<vt:vector size="' + sheets.length + '" baseType="lpstr">');
  55. for (var i = 0; i < sheets.length; i++) {
  56. rst.push('<vt:lpstr>' + sheets[i].sheetName + '</vt:lpstr>')
  57. }
  58. rst.push('</vt:vector>');
  59. rst.push('</TitlesOfParts>');
  60. rst.push('<Company>SmartCost</Company>');
  61. rst.push('<LinksUpToDate>false</LinksUpToDate>');
  62. rst.push('<SharedDoc>false</SharedDoc>');
  63. rst.push('<HyperlinksChanged>false</HyperlinksChanged>');
  64. rst.push('<AppVersion>12.0000</AppVersion>');
  65. //rst.push('');
  66. rst.push('</Properties>');
  67. return rst;
  68. }
  69. function writeCore() {
  70. var rst = [];
  71. p_fillZero = function(val){
  72. var rst = val;
  73. if (val < 10) {
  74. rst = '0' + val;
  75. }
  76. return rst;
  77. };
  78. rst.push(dftHeadXml + '\r\n');
  79. 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">');
  80. rst.push('<dc:creator>SmartCost</dc:creator>');
  81. rst.push('<cp:lastModifiedBy>SmartCost</cp:lastModifiedBy>');
  82. var dt = new Date(), dtStr = dt.getFullYear() + '-' + p_fillZero(dt.getMonth()+1) + '-' + p_fillZero(dt.getDate()) + 'T' +
  83. p_fillZero(dt.getHours()) + ':' + p_fillZero(dt.getMinutes()) + ':' + p_fillZero(dt.getSeconds()) + 'Z';
  84. rst.push('<dcterms:created xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:created>');
  85. rst.push('<dcterms:modified xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:modified>');
  86. //rst.push('');
  87. rst.push('</cp:coreProperties>');
  88. return rst;
  89. }
  90. function writeXlWorkBook(sheets){
  91. var rst = [];
  92. rst.push(dftHeadXml + '\r\n');
  93. rst.push('<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  94. rst.push('<fileVersion appName="xl" lastEdited="4" lowestEdited="4" rupBuild="4505"/>');
  95. rst.push('<workbookPr defaultThemeVersion="124226"/>');
  96. rst.push('<bookViews><workbookView xWindow="360" yWindow="345" windowWidth="14655" windowHeight="4305"/></bookViews>');
  97. rst.push('<sheets>');
  98. for (var i = 0; i < sheets.length; i++) {
  99. rst.push('<sheet name="' + sheets[i].sheetName + '" sheetId="' + (i + 1) + '" r:id="rId' + (i + 1) + '"/>')
  100. }
  101. rst.push('</sheets>');
  102. rst.push('<calcPr calcId="124519"/>');
  103. //rst.push('');
  104. rst.push('</workbook>');
  105. return rst;
  106. }
  107. function writeXlRels(sheets){
  108. var rst = [], idx = 1;
  109. rst.push(dftHeadXml + '\r\n');
  110. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  111. for (var i = 0; i < sheets.length; i++) {
  112. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet' + (i + 1) + '.xml"/>')
  113. idx++;
  114. }
  115. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>');
  116. idx++;
  117. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>');
  118. idx++;
  119. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/>');
  120. //rst.push('');
  121. rst.push('</Relationships>');
  122. return rst;
  123. }
  124. function writeTheme(){
  125. var rst = fs.readFileSync(__dirname + '/excel_base_files/theme1.xml', 'utf8', 'r');
  126. return rst;
  127. }
  128. function writeStyles(stylesObj){
  129. var rst = [];
  130. rst.push(dftHeadXml + '\r\n');
  131. rst.push('<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">');
  132. //1. push fonts
  133. rst.push('<fonts count="' + stylesObj.fonts.length + '">')
  134. for (var i = 0; i < stylesObj.fonts.length; i++) {
  135. var font = stylesObj.fonts[i];
  136. rst.push('<font>');
  137. if (strUtil.convertStrToBoolean(font[JV.FONT_PROPS[3]])) {
  138. rst.push('<b/>');
  139. }
  140. rst.push('<sz val="' + font.size + '"/>');
  141. rst.push('<color indexed="' + font.colorIdx + '"/>');
  142. rst.push('<name val="' + font[JV.FONT_PROPS[0]] + '"/>');
  143. rst.push('<charset val="' + font.charset + '"/>');
  144. rst.push('</font>');
  145. }
  146. rst.push('</fonts>');
  147. //2. push default fills
  148. rst.push('<fills count="2"><fill><patternFill patternType="none" /></fill><fill><patternFill patternType="gray125" /></fill></fills>');
  149. //3. push borders
  150. rst.push('<borders count="' + stylesObj.borders.length + '">')
  151. private_setBorder = function(border, borderDirection) {
  152. if (border[borderDirection][JV.PROP_LINE_WEIGHT] == 0) {
  153. rst.push('<' + borderDirection.toLowerCase() + '/>');
  154. } else {
  155. var bW = 'thin';
  156. if (border[borderDirection][JV.PROP_LINE_WEIGHT] = 2) bW = 'medium';
  157. if (border[borderDirection][JV.PROP_LINE_WEIGHT] > 2) bW = 'thick';
  158. rst.push('<' + borderDirection.toLowerCase() + ' style="' + bW + '">' + '<color indexed="64"/>' + '</' + borderDirection.toLowerCase() + '>');
  159. }
  160. };
  161. for (var i = 0; i < stylesObj.borders.length; i++) {
  162. var border = stylesObj.borders[i];
  163. rst.push('<border>');
  164. private_setBorder(border, JV.PROP_LEFT);
  165. private_setBorder(border, JV.PROP_RIGHT);
  166. private_setBorder(border, JV.PROP_TOP);
  167. private_setBorder(border, JV.PROP_BOTTOM);
  168. rst.push('<diagonal />');
  169. rst.push('</border>');
  170. }
  171. rst.push('</borders>');
  172. //4. push cellStyleXfs
  173. rst.push('<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"><alignment vertical="center"/></xf></cellStyleXfs>');
  174. //5. push cellXfs
  175. rst.push('<cellXfs count="' + stylesObj.cellXfs.length + '">');
  176. for (var i = 0; i < stylesObj.cellXfs.length; i++) {
  177. var excelStyle = stylesObj.cellXfs[i];
  178. rst.push('<xf numFmtId="0" fontId="' + excelStyle.fontId + '" fillId="0" borderId="' + excelStyle.borderId + '" xfId="0">');
  179. var alignStr = '<alignment horizontal="' + excelStyle[JV.CONTROL_PROPS[2]] + '" vertical="' + excelStyle[JV.CONTROL_PROPS[3]] + '"';
  180. if (strUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[1]])) {
  181. alignStr = alignStr + ' shrinkToFit="1"';
  182. }
  183. if (strUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[4]])) {
  184. alignStr = alignStr + ' wrapText="1"';
  185. }
  186. alignStr = alignStr + '/>';
  187. rst.push(alignStr);
  188. rst.push('</xf>');
  189. }
  190. rst.push('</cellXfs>');
  191. //6. others (xfl style / dxfs / tableStyles)
  192. rst.push('<cellStyles count="1"><cellStyle name="常规" xfId="0" builtinId="0"/></cellStyles>');
  193. rst.push('<dxfs count="0"/>');
  194. rst.push('<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleLight16"/>');
  195. rst.push('</styleSheet>');
  196. return rst;
  197. }
  198. function writeSharedString(sharedStrList){
  199. var rst = [];
  200. if (sharedStrList && sharedStrList.length > 0) {
  201. rst.push(dftHeadXml + '\r\n');
  202. rst.push('<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="' + sharedStrList.length + '" uniqueCount="' + sharedStrList.length + '">');
  203. for (var i = 0; i < sharedStrList.length; i++) {
  204. rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
  205. }
  206. rst.push('</sst>');
  207. }
  208. return rst;
  209. }
  210. function writeSheets(pageData, sharedStrList, stylesObj){
  211. var rst = [];
  212. private_pushDftFont = function(){
  213. var font = {};
  214. if (!(stylesObj.fonts)) {
  215. stylesObj.fonts = [];
  216. }
  217. font[JV.FONT_PROPS[0]] = "宋体"; //font name
  218. font.size = 11;
  219. font.charset = 134;
  220. font.colorIdx = "8";
  221. stylesObj.fonts.push(font);
  222. };
  223. private_pushDftFont();
  224. for (var i = 0; i < pageData.items.length; i++) {
  225. rst.push(writeSheet(pageData, pageData.items[i], sharedStrList, stylesObj));
  226. }
  227. return rst;
  228. }
  229. function writeSheet(pageData, sheetData, sharedStrList, stylesObj){
  230. var rst = [], xPos = [], yPos = [], headerStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  231. xPos.push(0);
  232. yPos.push(0);
  233. private_pre_analyze_pos = function(){
  234. var cell, pos;
  235. sheetData.cells.sort(function(cell1, cell2) {
  236. var rst = 0;
  237. if (cell1[JV.PROP_AREA][JV.PROP_TOP] > cell2[JV.PROP_AREA][JV.PROP_TOP]) {
  238. rst = 1;
  239. } else if (cell1[JV.PROP_AREA][JV.PROP_TOP] < cell2[JV.PROP_AREA][JV.PROP_TOP]) {
  240. rst = -1;
  241. } else {
  242. if (cell1[JV.PROP_AREA][JV.PROP_LEFT] > cell2[JV.PROP_AREA][JV.PROP_LEFT]) {
  243. rst = 1;
  244. } else if (cell1[JV.PROP_AREA][JV.PROP_LEFT] < cell2[JV.PROP_AREA][JV.PROP_LEFT]) {
  245. rst = -1;
  246. }
  247. }
  248. return rst;
  249. });
  250. for (var i = 0; i < sheetData.cells.length; i++) {
  251. cell = sheetData.cells[i];
  252. pos = cell[JV.PROP_AREA][JV.PROP_LEFT];
  253. if (xPos.indexOf(pos) < 0) xPos.push(pos);
  254. pos = cell[JV.PROP_AREA][JV.PROP_RIGHT];
  255. if (xPos.indexOf(pos) < 0) xPos.push(pos);
  256. pos = cell[JV.PROP_AREA][JV.PROP_TOP];
  257. if (yPos.indexOf(pos) < 0) yPos.push(pos);
  258. pos = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  259. if (yPos.indexOf(pos) < 0) yPos.push(pos);
  260. }
  261. xPos.sort(private_array_sort);
  262. yPos.sort(private_array_sort);
  263. };
  264. private_array_sort = function(i1, i2){
  265. var rst = 0;
  266. if (i1 > i2) {rst = 1} else
  267. if (i1 < i2) rst = -1;
  268. return rst;
  269. };
  270. private_getCellIdxStr = function(idx){
  271. var rst = 'A';
  272. if (idx < 26) {
  273. rst = headerStr[idx];
  274. } else if (idx < 26*26+26) {
  275. var ti = Math.floor(idx / 26), tj = idx % 26;
  276. rst = headerStr[ti - 1] + headerStr[tj];
  277. } else if (idx < 26*26*26+26) {
  278. var ti = Math.floor(idx / (26*26)), tj = Math.floor((idx - ti * 26*26) / 26), tk = idx % 26;
  279. rst = headerStr[ti - 1] + headerStr[tj-1] + headerStr[tk];
  280. }
  281. return rst;
  282. };
  283. private_getSharedStrIdx = function(val) {
  284. var rst = sharedStrList.indexOf(val);
  285. if (rst < 0) {
  286. sharedStrList.push(val);
  287. rst = sharedStrList.length - 1;
  288. }
  289. return rst;
  290. };
  291. private_getFontId = function(cell) {
  292. var rst = 0, hasFont = false;
  293. if (!(stylesObj.fonts)) {
  294. stylesObj.fonts = [];
  295. //for (var i = 0; i < sheetData.font_collection)
  296. }
  297. var sheetFont = pageData.font_collection[cell.font];
  298. for (var i = 0; i < stylesObj.fonts.length; i++) {
  299. var font = stylesObj.fonts[i];
  300. if (sheetFont) {
  301. if (font[JV.FONT_PROPS[0]] === sheetFont[JV.FONT_PROPS[0]] && font.size === Math.round(sheetFont[JV.FONT_PROPS[1]] * 3 / 4) && font[JV.FONT_PROPS[3]] == sheetFont[JV.FONT_PROPS[3]]) {
  302. hasFont = true;
  303. rst = i;
  304. break;
  305. }
  306. } else {
  307. break;
  308. }
  309. }
  310. if (!hasFont) {
  311. var font = {};
  312. font[JV.FONT_PROPS[0]] = sheetFont[JV.FONT_PROPS[0]]; //font name
  313. font.size = Math.round(sheetFont[JV.FONT_PROPS[1]] * 3 / 4);
  314. font.charset = 134;
  315. font.colorIdx = "8";
  316. font[JV.FONT_PROPS[3]] = sheetFont[JV.FONT_PROPS[3]]; //font bold
  317. stylesObj.fonts.push(font);
  318. rst = stylesObj.fonts.length - 1;
  319. }
  320. return rst;
  321. };
  322. private_checkBorder = function(border, sheetBorder) {
  323. var rst = true, borderLineWidths = [], sheetBorderLineWidths = [];
  324. borderLineWidths.push(border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]);
  325. borderLineWidths.push(border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]);
  326. borderLineWidths.push(border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]);
  327. borderLineWidths.push(border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]);
  328. if (sheetBorder[JV.PROP_LEFT] && sheetBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]) {
  329. sheetBorderLineWidths.push(parseInt(border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]));
  330. } else {
  331. sheetBorderLineWidths.push(0);
  332. }
  333. if (sheetBorder[JV.PROP_RIGHT] && sheetBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]) {
  334. sheetBorderLineWidths.push(parseInt(border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]));
  335. } else {
  336. sheetBorderLineWidths.push(0);
  337. }
  338. if (sheetBorder[JV.PROP_TOP] && sheetBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]) {
  339. sheetBorderLineWidths.push(parseInt(border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]));
  340. } else {
  341. sheetBorderLineWidths.push(0);
  342. }
  343. if (sheetBorder[JV.PROP_BOTTOM] && sheetBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]) {
  344. sheetBorderLineWidths.push(parseInt(border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]));
  345. } else {
  346. sheetBorderLineWidths.push(0);
  347. }
  348. for (var i = 0; i < 4; i++) {
  349. if (borderLineWidths[i] != sheetBorderLineWidths[i]) {
  350. rst = false;
  351. break;
  352. }
  353. }
  354. return rst;
  355. };
  356. private_getBorderId = function(cell) {
  357. var rst = 0, hasBorder = false;
  358. if (!(stylesObj.borders)) {
  359. stylesObj.borders = [];
  360. }
  361. var sheetBorder = pageData.style_collection[cell.style];
  362. for (var i = 0; i < stylesObj.borders.length; i++) {
  363. var border = stylesObj.borders[i];
  364. if (private_checkBorder(border, sheetBorder)) {
  365. hasBorder = true;
  366. rst = i;
  367. break;
  368. }
  369. }
  370. if (!hasBorder) {
  371. var border = {};
  372. border[JV.PROP_LEFT] = {};
  373. border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = 0;
  374. border[JV.PROP_RIGHT] = {};
  375. border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = 0;
  376. border[JV.PROP_TOP] = {};
  377. border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = 0;
  378. border[JV.PROP_BOTTOM] = {};
  379. border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = 0;
  380. if (sheetBorder && sheetBorder[JV.PROP_LEFT]) {
  381. border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = parseInt(sheetBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]);
  382. }
  383. if (sheetBorder && sheetBorder[JV.PROP_RIGHT]) {
  384. border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = parseInt(sheetBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]);
  385. }
  386. if (sheetBorder && sheetBorder[JV.PROP_TOP]) {
  387. border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = parseInt(sheetBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]);
  388. }
  389. if (sheetBorder && sheetBorder[JV.PROP_BOTTOM]) {
  390. border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = parseInt(sheetBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]);
  391. }
  392. stylesObj.borders.push(border);
  393. rst = stylesObj.borders.length - 1;
  394. }
  395. return rst;
  396. };
  397. private_checkControl = function(cellControl, sheetControl) {
  398. var rst = true;
  399. for (var i = 0; i < JV.CONTROL_PROPS.length; i++) {
  400. if (cellControl[JV.CONTROL_PROPS[i]] != sheetControl[JV.CONTROL_PROPS[i]]) {
  401. rst = false;
  402. break;
  403. }
  404. }
  405. return rst;
  406. };
  407. private_getStyleId = function(cell) {
  408. var rst = 1, hasStyle = false;
  409. if (!(stylesObj.cellXfs)) stylesObj.cellXfs = [];
  410. var fontId = private_getFontId(cell);
  411. var borderId = private_getBorderId(cell);
  412. var cellControl = pageData[JV.NODE_CONTROL_COLLECTION][cell[JV.PROP_CONTROL]];
  413. for (var i = 0; i < stylesObj.cellXfs.length; i++) {
  414. var sheetControl = stylesObj.cellXfs[i];
  415. if (sheetControl.fontId == fontId && sheetControl.borderId == borderId) {
  416. if (private_checkControl(cellControl, sheetControl)) {
  417. rst = i;
  418. hasStyle = true;
  419. break;
  420. }
  421. }
  422. }
  423. if (!hasStyle) {
  424. var sheetControl = {};
  425. sheetControl.fontId = fontId;
  426. sheetControl.borderId = borderId;
  427. for (var i = 0; i < JV.CONTROL_PROPS.length; i++) {
  428. sheetControl[JV.CONTROL_PROPS[i]] = cellControl[JV.CONTROL_PROPS[i]];
  429. }
  430. stylesObj.cellXfs.push(sheetControl);
  431. rst = stylesObj.cellXfs.length - 1;
  432. }
  433. return rst;
  434. };
  435. private_setCols = function(){
  436. //remark: 1 excel width = 2.117 mm
  437. rst.push('<cols>');
  438. var w = 0;
  439. for (var i = 1; i < xPos.length; i++) {
  440. w = 1.0 * (xPos[i] - xPos[i - 1]) / DPI * 25.4 / 2.117;
  441. w = Math.round(w * 1000) / 1000;
  442. rst.push('<col min="' + i +'" max="' + i +'" width="' + w + '" customWidth="1"/>');
  443. }
  444. rst.push('</cols>');
  445. };
  446. private_setMergedCells = function() {
  447. var cell, idxR, idxL, idxT, idxB, cnt = 0;
  448. rst.push('<mergeCells count="?">');
  449. var startIdx = rst.length - 1;
  450. for (var i = 0; i < sheetData.cells.length; i++) {
  451. cell = sheetData.cells[i];
  452. idxR = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_RIGHT]);
  453. idxL = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_LEFT]);
  454. idxB = yPos.indexOf(cell[JV.PROP_AREA][JV.PROP_BOTTOM]);
  455. idxT = yPos.indexOf(cell[JV.PROP_AREA][JV.PROP_TOP]);
  456. if (idxR - idxL > 1 || idxB - idxT > 1) {
  457. rst.push('<mergeCell ref="' + private_getCellIdxStr(idxL - 1) + idxT + ':' + private_getCellIdxStr(idxR - 2) + (idxB - 1) + '"/>');
  458. cnt++;
  459. }
  460. }
  461. rst[startIdx] = '<mergeCells count="' + cnt + '">';
  462. rst.push('</mergeCells>');
  463. };
  464. private_setSheetData = function(){
  465. //remark: 1 excel height = 0.3612 mm
  466. rst.push('<sheetData>');
  467. var spanX = xPos.length - 1, cellIdx = 0, h = 0,
  468. hasMoreCols = true, nextColIdx = -1,
  469. nextRowIdx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
  470. for (var i = 1; i < yPos.length - 1; i++) {
  471. h = 1.0 * (yPos[i+1] - yPos[i]) / DPI * 25.4 / 0.3612;
  472. h = Math.round(h * 1000) / 1000;
  473. rst.push('<row r="' + i + '" spans="1:' + spanX + '" ht="' + h + '" customHeight="1">');
  474. //then put the cells of this row
  475. var colIdxStr = '';
  476. hasMoreCols = true;
  477. while (nextRowIdx < i) {
  478. if (cellIdx >= sheetData.cells.length || nextRowIdx > i) {
  479. break;
  480. } else {
  481. cellIdx++;
  482. nextRowIdx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
  483. }
  484. }
  485. if (nextRowIdx > i) {
  486. hasMoreCols = false;
  487. }
  488. nextColIdx = xPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_LEFT]);
  489. for (var j = 1; j < xPos.length; j++) {
  490. colIdxStr = private_getCellIdxStr(j - 1);
  491. if (hasMoreCols) {
  492. if (nextColIdx == j) {
  493. var styleIdx = private_getStyleId(sheetData.cells[cellIdx]);
  494. if (strUtil.isEmptyString(sheetData.cells[cellIdx][JV.PROP_VALUE])) {
  495. rst.push('<c r="' + colIdxStr + i + '" s="' + styleIdx + '"/>');
  496. //should setup the right style instead!
  497. } else {
  498. var valIdx = private_getSharedStrIdx(sheetData.cells[cellIdx][JV.PROP_VALUE]);
  499. rst.push('<c r="' + colIdxStr + i + '" s="' + styleIdx + '" t="s">');
  500. rst.push('<v>' + valIdx + '</v>');
  501. rst.push('</c>');
  502. }
  503. cellIdx++;
  504. if (cellIdx < sheetData.cells.length) {
  505. nextRowIdx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
  506. if (nextRowIdx > i) {
  507. hasMoreCols = false;
  508. } else {
  509. nextColIdx = xPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_LEFT]);
  510. }
  511. } else {
  512. hasMoreCols = false;
  513. }
  514. } else if (nextColIdx < 0) {
  515. //impossible!
  516. console.log('has abnormal case!');
  517. hasMoreCols = false;
  518. } else {
  519. rst.push('<c r="' + colIdxStr + i + '" s="1"/>');
  520. }
  521. } else {
  522. rst.push('<c r="' + colIdxStr + i + '" s="1"/>');
  523. }
  524. }
  525. rst.push('</row>');
  526. }
  527. //sheetData.cells.length
  528. rst.push('</sheetData>');
  529. };
  530. private_pre_analyze_pos();
  531. rst.push(dftHeadXml + '\r\n');
  532. rst.push('<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  533. var colStr = private_getCellIdxStr(xPos.length - 2);
  534. rst.push('<dimension ref="A1:' + colStr + '' + yPos.length + '"/>');
  535. rst.push('<sheetViews><sheetView tabSelected="1" workbookViewId="0">');
  536. rst.push('<selection sqref="A1:' + colStr + '1"/>');
  537. rst.push('</sheetView></sheetViews>');
  538. rst.push('<sheetFormatPr defaultRowHeight="13.5"/>');
  539. private_setCols();
  540. private_setSheetData();
  541. private_setMergedCells();
  542. rst.push('<phoneticPr fontId="1" type="noConversion"/>');
  543. rst.push('<pageMargins left="0.315" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
  544. //rst.push('<pageSetup paperSize="9" fitToWidth="0" fitToHeight="0" orientation="landscape" horizontalDpi="300" verticalDpi="300"/>');
  545. rst.push('<headerFooter alignWithMargins="0"/>');
  546. rst.push('</worksheet>');
  547. //rst.push('');
  548. return rst;
  549. }
  550. module.exports = {
  551. exportExcel: function (pageData, options) {
  552. var rptOptions = (options || {singlePage: false, fileName: 'report'});
  553. var sheets = [];
  554. for (var i = 0; i < pageData.items.length; i++) {
  555. sheets.push({sheetName: '第' + (i + 1) + '页'});
  556. }
  557. //1.
  558. var file = '[Content_Types].xml';
  559. var data = writeContentTypes(sheets);
  560. var zip = new JSZip();
  561. zip.file(file, data.join(''), {compression: 'DEFLATE'});
  562. //2.
  563. var zip_rels = zip.folder('_rels');
  564. file = '.rels';
  565. data = writeRootRels();
  566. zip_rels.file(file, data.join(''), {compression: 'DEFLATE'});
  567. //3.
  568. var zip_docProps = zip.folder('docProps');
  569. file = 'app.xml';
  570. data = writeApp(sheets);
  571. zip_docProps.file(file, data.join(''), {compression: 'DEFLATE'});
  572. file = 'core.xml';
  573. data = writeCore();
  574. zip_docProps.file(file, data.join(''), {compression: 'DEFLATE'});
  575. //4.
  576. var zip_xl = zip.folder('xl');
  577. file = 'workbook.xml';
  578. data = writeXlWorkBook(sheets);
  579. zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
  580. var zip_rels2 = zip_xl.folder('_rels');
  581. file = 'workbook.xml.rels';
  582. data = writeXlRels(sheets);
  583. zip_rels2.file(file, data.join(''), {compression: 'DEFLATE'});
  584. //5.
  585. var zip_theme = zip_xl.folder('theme');
  586. file = 'theme1.xml';
  587. data = writeTheme();
  588. zip_theme.file(file, data, {compression: 'DEFLATE'});
  589. //6.
  590. var zip_worksheets = zip_xl.folder('worksheets');
  591. var sharedStrList = [], stylesObj = {};
  592. data = writeSheets(pageData, sharedStrList, stylesObj);
  593. for (var i = 0; i < data.length; i++) {
  594. file = 'sheet' + (i + 1) + '.xml';
  595. zip_worksheets.file(file, data[i].join(''), {compression: 'DEFLATE'});
  596. }
  597. file = 'sharedStrings.xml';
  598. data = writeSharedString(sharedStrList);
  599. zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
  600. file = 'styles.xml';
  601. data = writeStyles(stylesObj);
  602. zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
  603. zip.generateNodeStream({type:'nodebuffer',streamFiles:true})
  604. //.pipe(fs.createWriteStream('../../../tmp/outExcel.xlsx'))
  605. .pipe(fs.createWriteStream('../../../tmp/outExcel.zip'))
  606. .on('finish', function () {
  607. // JSZip generates a readable stream with a "end" event,
  608. // but is piped here in a writable stream which emits a "finish" event.
  609. console.log("outExcel.xlsx was written.");
  610. }
  611. );
  612. }
  613. ,testWriteContentTypes: function(sheets) {
  614. return writeContentTypes(sheets);
  615. }
  616. ,testWriteRootRels: function() {
  617. return writeRootRels();
  618. }
  619. ,testWriteApp: function(sheets) {
  620. return writeApp(sheets);
  621. }
  622. ,testWriteCore: function() {
  623. return writeCore();
  624. }
  625. ,testWriteXlWorkBook: function(sheets) {
  626. return writeXlWorkBook(sheets);
  627. }
  628. ,testWriteXlRels: function(sheets) {
  629. return writeXlRels(sheets);
  630. }
  631. ,testWriteSheets: function(pageData, sharedStrList, stylesObj){
  632. return writeSheets(pageData, sharedStrList, stylesObj);
  633. }
  634. ,testWriteSharedString: function(sharedStrList){
  635. return writeSharedString(sharedStrList);
  636. }
  637. ,testWriteTheme: function() {
  638. return writeTheme();
  639. }
  640. ,testWriteStyles: function(stylesObj) {
  641. return writeStyles(stylesObj);
  642. }
  643. }