rpt_excel_util.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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()+1) + '-' + 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(__dirname + '/excel_base_files/theme1.xml', 'utf8', 'r');
  125. return rst;
  126. }
  127. function writeStyles(stylesObj){
  128. var rst = [];
  129. rst.push(dftHeadXml + '\r\n');
  130. rst.push('<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">');
  131. //1. push fonts
  132. rst.push('<fonts count="' + stylesObj.fonts.length + '">')
  133. for (var i = 0; i < stylesObj.fonts.length; i++) {
  134. var font = stylesObj.fonts[i];
  135. rst.push('<font>');
  136. if (strUtil.convertStrToBoolean(font[JV.FONT_PROPS[3]])) {
  137. rst.push('<b/>');
  138. }
  139. rst.push('<sz val="' + font.size + '"/>');
  140. rst.push('<color indexed="' + font.colorIdx + '"/>');
  141. rst.push('<name val="' + font[JV.FONT_PROPS[0]] + '"/>');
  142. rst.push('<charset val="' + font.charset + '"/>');
  143. rst.push('</font>');
  144. }
  145. rst.push('<fonts>');
  146. //2. push default fills
  147. rst.push('<fills count="2"><fill><patternFill patternType="none" /></fill><fill><patternFill patternType="gray125" /></fill></fills>');
  148. //3. push borders
  149. rst.push('<borders count="' + stylesObj.borders.length + '">')
  150. private_setBorder = function(border, borderDirection) {
  151. if (border[borderDirection][JV.PROP_LINE_WEIGHT] == 0) {
  152. rst.push('<' + borderDirection.toLowerCase() + '/>');
  153. } else {
  154. var bW = 'thin';
  155. if (border[borderDirection][JV.PROP_LINE_WEIGHT] = 2) bW = 'medium';
  156. if (border[borderDirection][JV.PROP_LINE_WEIGHT] > 2) bW = 'thick';
  157. rst.push('<' + borderDirection.toLowerCase() + ' style="' + bW + '">' + '<color indexed="64"/>' + '</' + borderDirection.toLowerCase() + '>');
  158. }
  159. };
  160. for (var i = 0; i < stylesObj.borders.length; i++) {
  161. var border = stylesObj.borders[i];
  162. private_setBorder(border, JV.PROP_LEFT);
  163. private_setBorder(border, JV.PROP_RIGHT);
  164. private_setBorder(border, JV.PROP_TOP);
  165. private_setBorder(border, JV.PROP_BOTTOM);
  166. }
  167. rst.push('</borders>');
  168. //4. push cellStyleXfs
  169. rst.push('<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"><alignment vertical="center"/></xf></cellStyleXfs>');
  170. //5. push cellXfs
  171. rst.push('<cellXfs count="' + stylesObj.cellXfs.length + '">');
  172. for (var i = 0; i < stylesObj.cellXfs.length; i++) {
  173. var excelStyle = stylesObj.cellXfs[i];
  174. rst.push('<xf numFmtId="0" fontId="' + excelStyle.fontId + '" fillId="0" borderId="' + excelStyle.borderId + '" xfId="0">');
  175. var alignStr = '<alignment horizontal="' + excelStyle[JV.CONTROL_PROPS[2]] + '" vertical="' + excelStyle[JV.CONTROL_PROPS[3]] + '"';
  176. if (strUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[1]])) {
  177. alignStr = alignStr + ' shrinkToFit="1"';
  178. }
  179. if (strUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[4]])) {
  180. alignStr = alignStr + ' wrapText="1"';
  181. }
  182. alignStr = alignStr + '/>';
  183. rst.push(alignStr);
  184. rst.push('</xf>');
  185. }
  186. rst.push('</cellXfs>');
  187. //6. others (xfl style / dxfs / tableStyles)
  188. //..
  189. rst.push('</styleSheet>');
  190. return rst;
  191. }
  192. function writeSharedString(sharedStrList){
  193. var rst = [];
  194. if (sharedStrList && sharedStrList.length > 0) {
  195. rst.push(dftHeadXml + '\r\n');
  196. rst.push('<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="' + sharedStrList.length + '" uniqueCount="' + sharedStrList.length + '">');
  197. for (var i = 0; i < sharedStrList.length; i++) {
  198. rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
  199. }
  200. rst.push('</sst>');
  201. }
  202. return rst;
  203. }
  204. function writeSheets(pageData, sharedStrList, stylesObj){
  205. var rst = [];
  206. for (var i = 0; i < pageData.items.length; i++) {
  207. rst.push(writeSheet(pageData.items[i], sharedStrList, stylesObj));
  208. }
  209. return rst;
  210. }
  211. function writeSheet(sheetData, sharedStrList, stylesObj){
  212. var rst = [], xPos = [], yPos = [], headerStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  213. xPos.push(0);
  214. yPos.push(0);
  215. private_pre_analyze_pos = function(){
  216. var cell, pos;
  217. sheetData.cells.sort(function(cell1, cell2) {
  218. var rst = 0;
  219. if (cell1[JV.PROP_AREA][JV.PROP_TOP] > cell2[JV.PROP_AREA][JV.PROP_TOP]) {
  220. rst = 1;
  221. } else if (cell1[JV.PROP_AREA][JV.PROP_TOP] < cell2[JV.PROP_AREA][JV.PROP_TOP]) {
  222. rst = -1;
  223. } else {
  224. if (cell1[JV.PROP_AREA][JV.PROP_LEFT] > cell2[JV.PROP_AREA][JV.PROP_LEFT]) {
  225. rst = 1;
  226. } else if (cell1[JV.PROP_AREA][JV.PROP_LEFT] < cell2[JV.PROP_AREA][JV.PROP_LEFT]) {
  227. rst = -1;
  228. }
  229. }
  230. return rst;
  231. });
  232. for (var i = 0; i < sheetData.cells.length; i++) {
  233. cell = sheetData.cells[i];
  234. pos = cell[JV.PROP_AREA][JV.PROP_LEFT];
  235. if (xPos.indexOf(pos) < 0) xPos.push(pos);
  236. pos = cell[JV.PROP_AREA][JV.PROP_RIGHT];
  237. if (xPos.indexOf(pos) < 0) xPos.push(pos);
  238. pos = cell[JV.PROP_AREA][JV.PROP_TOP];
  239. if (yPos.indexOf(pos) < 0) yPos.push(pos);
  240. pos = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  241. if (yPos.indexOf(pos) < 0) yPos.push(pos);
  242. }
  243. xPos.sort(private_array_sort);
  244. yPos.sort(private_array_sort);
  245. };
  246. private_array_sort = function(i1, i2){
  247. var rst = 0;
  248. if (i1 > i2) {rst = 1} else
  249. if (i1 < i2) rst = -1;
  250. return rst;
  251. };
  252. private_getCellIdxStr = function(idx){
  253. var rst = 'A';
  254. if (idx < 26) {
  255. rst = headerStr[idx];
  256. } else if (idx < 26*26+26) {
  257. var ti = Math.floor(idx / 26), tj = idx % 26;
  258. rst = headerStr[ti - 1] + headerStr[tj];
  259. } else if (idx < 26*26*26+26) {
  260. var ti = Math.floor(idx / (26*26)), tj = Math.floor((idx - ti * 26*26) / 26), tk = idx % 26;
  261. rst = headerStr[ti - 1] + headerStr[tj-1] + headerStr[tk];
  262. }
  263. return rst;
  264. };
  265. private_getSharedStrIdx = function(val) {
  266. var rst = sharedStrList.indexOf(val);
  267. if (rst < 0) {
  268. sharedStrList.push(val);
  269. rst = sharedStrList.length - 1;
  270. }
  271. return rst;
  272. };
  273. private_getFontId = function(cell) {
  274. var rst = 0, hasFont = false;
  275. if (!(stylesObj.fonts)) {
  276. stylesObj.fonts = [];
  277. //for (var i = 0; i < sheetData.font_collection)
  278. }
  279. var sheetFont = sheetData.font_collection[cell.font];
  280. for (var i = 0; i < stylesObj.fonts.length; i++) {
  281. var font = stylesObj.fonts[i];
  282. if (sheetFont) {
  283. 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]]) {
  284. hasFont = true;
  285. rst = i;
  286. break;
  287. }
  288. } else {
  289. break;
  290. }
  291. }
  292. if (!hasFont) {
  293. var font = {};
  294. font[JV.FONT_PROPS[0]] = sheetFont[JV.FONT_PROPS[0]]; //font name
  295. font.size = Math.round(sheetFont[JV.FONT_PROPS[1]] * 3 / 4);
  296. font.charset = 134;
  297. font.colorIdx = "8";
  298. font[JV.FONT_PROPS[3]] = sheetFont[JV.FONT_PROPS[3]]; //font bold
  299. stylesObj.fonts.push(font);
  300. rst = stylesObj.fonts.length - 1;
  301. }
  302. return rst;
  303. };
  304. private_checkBorder = function(border, sheetBorder) {
  305. var rst = true, borderLineWidths = [], sheetBorderLineWidths = [];
  306. borderLineWidths.push(border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]);
  307. borderLineWidths.push(border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]);
  308. borderLineWidths.push(border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]);
  309. borderLineWidths.push(border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]);
  310. if (sheetBorder[JV.PROP_LEFT] && sheetBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]) {
  311. sheetBorderLineWidths.push(parseInt(border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]));
  312. } else {
  313. sheetBorderLineWidths.push(0);
  314. }
  315. if (sheetBorder[JV.PROP_RIGHT] && sheetBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]) {
  316. sheetBorderLineWidths.push(parseInt(border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]));
  317. } else {
  318. sheetBorderLineWidths.push(0);
  319. }
  320. if (sheetBorder[JV.PROP_TOP] && sheetBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]) {
  321. sheetBorderLineWidths.push(parseInt(border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]));
  322. } else {
  323. sheetBorderLineWidths.push(0);
  324. }
  325. if (sheetBorder[JV.PROP_BOTTOM] && sheetBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]) {
  326. sheetBorderLineWidths.push(parseInt(border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]));
  327. } else {
  328. sheetBorderLineWidths.push(0);
  329. }
  330. for (var i = 0; i < 4; i++) {
  331. if (borderLineWidths[i] != sheetBorderLineWidths[i]) {
  332. rst = false;
  333. break;
  334. }
  335. }
  336. return rst;
  337. };
  338. private_getBorderId = function(cell) {
  339. var rst = 0, hasBorder = false;
  340. if (!(stylesObj.borders)) {
  341. stylesObj.borders = [];
  342. }
  343. var sheetBorder = sheetData.style_collection[cell.style];
  344. for (var i = 0; i < stylesObj.borders.length; i++) {
  345. var border = stylesObj.borders[i];
  346. if (private_checkBorder(border, sheetBorder)) {
  347. hasBorder = true;
  348. rst = i;
  349. break;
  350. }
  351. }
  352. if (!hasBorder) {
  353. var border = {};
  354. border[JV.PROP_LEFT] = {};
  355. border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = 0;
  356. border[JV.PROP_RIGHT] = {};
  357. border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = 0;
  358. border[JV.PROP_TOP] = {};
  359. border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = 0;
  360. border[JV.PROP_BOTTOM] = {};
  361. border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = 0;
  362. if (sheetBorder && sheetBorder[JV.PROP_LEFT]) {
  363. border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = parseInt(sheetBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]);
  364. }
  365. if (sheetBorder && sheetBorder[JV.PROP_RIGHT]) {
  366. border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = parseInt(sheetBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]);
  367. }
  368. if (sheetBorder && sheetBorder[JV.PROP_TOP]) {
  369. border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = parseInt(sheetBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]);
  370. }
  371. if (sheetBorder && sheetBorder[JV.PROP_BOTTOM]) {
  372. border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = parseInt(sheetBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]);
  373. }
  374. stylesObj.borders.push(border);
  375. rst = stylesObj.borders.length - 1;
  376. }
  377. return rst;
  378. };
  379. private_checkControl = function(cellControl, sheetControl) {
  380. var rst = true;
  381. for (var i = 0; i < JV.CONTROL_PROPS.length; i++) {
  382. if (cellControl[JV.CONTROL_PROPS[i]] != sheetControl[JV.CONTROL_PROPS[i]]) {
  383. rst = false;
  384. break;
  385. }
  386. }
  387. return rst;
  388. };
  389. private_getStyleId = function(cell) {
  390. var rst = 1, hasStyle = false;
  391. if (!(stylesObj.cellXfs)) stylesObj.cellXfs = [];
  392. var fontId = private_getFontId(cell);
  393. var borderId = private_getBorderId(cell);
  394. var cellControl = sheetData[JV.NODE_CONTROL_COLLECTION][cell[JV.PROP_CONTROL]];
  395. for (var i = 0; i < stylesObj.cellXfs.length; i++) {
  396. var sheetControl = stylesObj.cellXfs[i];
  397. if (sheetControl.fontId == fontId && sheetControl.borderId == borderId) {
  398. if (private_checkControl(cellControl, sheetControl)) {
  399. rst = i;
  400. hasStyle = true;
  401. break;
  402. }
  403. }
  404. }
  405. if (!hasStyle) {
  406. var sheetControl = {};
  407. sheetControl.fontId = fontId;
  408. sheetControl.borderId = borderId;
  409. for (var i = 0; i < JV.CONTROL_PROPS.length; i++) {
  410. sheetControl[JV.CONTROL_PROPS[i]] = cellControl[JV.CONTROL_PROPS[i]];
  411. }
  412. stylesObj.cellXfs.push(sheetControl);
  413. rst = stylesObj.cellXfs.length - 1;
  414. }
  415. return rst;
  416. };
  417. private_setCols = function(){
  418. //remark: 1 excel width = 2.117 mm
  419. rst.push('<cols>');
  420. var w = 0;
  421. for (var i = 1; i < xPos.length; i++) {
  422. w = 1.0 * (xPos[i] - xPos[i - 1]) / DPI * 25.4 / 2.117;
  423. w = Math.round(w * 1000) / 1000;
  424. rst.push('<col min="' + i +'" max="' + i +'" width="' + w + '" customWidth="1"/>');
  425. }
  426. rst.push('</cols>');
  427. };
  428. private_setMergedCells = function() {
  429. var cell, idxR, idxL, idxT, idxB, cnt = 0;
  430. rst.push('<mergeCells count="?">');
  431. var startIdx = rst.length - 1;
  432. for (var i = 0; i < sheetData.cells.length; i++) {
  433. cell = sheetData.cells[i];
  434. idxR = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_RIGHT]);
  435. idxL = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_LEFT]);
  436. idxB = yPos.indexOf(cell[JV.PROP_AREA][JV.PROP_BOTTOM]);
  437. idxT = yPos.indexOf(cell[JV.PROP_AREA][JV.PROP_TOP]);
  438. if (idxR - idxL > 1 || idxB - idxT > 1) {
  439. rst.push('<mergeCell ref="' + private_getCellIdxStr(idxL - 1) + idxT + ':' + private_getCellIdxStr(idxR - 2) + (idxB - 1) + '"/>');
  440. cnt++;
  441. }
  442. }
  443. rst[startIdx] = '<mergeCells count="' + cnt + '">';
  444. rst.push('</mergeCells>');
  445. };
  446. private_setSheetData = function(){
  447. //remark: 1 excel height = 0.3612 mm
  448. rst.push('<sheetData>');
  449. var spanX = xPos.length - 1, cellIdx = 0, h = 0,
  450. hasMoreCols = true, nextColIdx = -1,
  451. nextRowIdx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
  452. for (var i = 1; i < yPos.length - 1; i++) {
  453. if (i === 0) {
  454. h = 1.0 * (yPos[i]) / DPI * 25.4 / 0.3612;
  455. } else {
  456. h = 1.0 * (yPos[i] - yPos[i - 1]) / DPI * 25.4 / 0.3612;
  457. }
  458. h = Math.round(h * 1000) / 1000;
  459. rst.push('<row r="' + i + '" spans="1:' + spanX + '" ht="' + h + '" customHeight="1">');
  460. //then put the cells of this row
  461. var colIdxStr = '';
  462. hasMoreCols = true;
  463. while (nextRowIdx < i) {
  464. if (cellIdx >= sheetData.cells.length || nextRowIdx > i) {
  465. break;
  466. } else {
  467. cellIdx++;
  468. nextRowIdx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
  469. }
  470. }
  471. if (nextRowIdx > i) {
  472. hasMoreCols = false;
  473. }
  474. nextColIdx = xPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_LEFT]);
  475. for (var j = 0; j < xPos.length; j++) {
  476. colIdxStr = private_getCellIdxStr(j);
  477. if (hasMoreCols) {
  478. if (nextColIdx == j) {
  479. var styleIdx = private_getStyleId(sheetData.cells[cellIdx]);
  480. if (strUtil.isEmptyString(sheetData.cells[cellIdx][JV.PROP_VALUE])) {
  481. rst.push('<c r="' + colIdxStr + i + '" s="' + styleIdx + '"/>');
  482. //should setup the right style instead!
  483. } else {
  484. var valIdx = private_getSharedStrIdx(sheetData.cells[cellIdx][JV.PROP_VALUE]);
  485. rst.push('<c r="' + colIdxStr + i + '" s="' + styleIdx + '">');
  486. rst.push('<v>' + valIdx + '</v>');
  487. rst.push('</c>');
  488. }
  489. cellIdx++;
  490. if (cellIdx < sheetData.cells.length) {
  491. nextRowIdx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
  492. if (nextRowIdx > i) {
  493. hasMoreCols = false;
  494. } else {
  495. nextColIdx = xPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_LEFT]);
  496. }
  497. } else {
  498. hasMoreCols = false;
  499. }
  500. } else if (nextColIdx < 0) {
  501. //impossible!
  502. console.log('has abnormal case!');
  503. hasMoreCols = false;
  504. } else {
  505. rst.push('<c r="' + colIdxStr + i + '" s="1"/>');
  506. }
  507. } else {
  508. rst.push('<c r="' + colIdxStr + i + '" s="1"/>');
  509. }
  510. }
  511. rst.push('</row>');
  512. }
  513. //sheetData.cells.length
  514. rst.push('</sheetData>');
  515. };
  516. private_pre_analyze_pos();
  517. rst.push(dftHeadXml + '\r\n');
  518. rst.push('<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  519. var colStr = private_getCellIdxStr(xPos.length - 2);
  520. rst.push('<dimension ref="A1:' + colStr + '' + yPos.length + '"/>');
  521. rst.push('<sheetViews><sheetView tabSelected="1" workbookViewId="0">');
  522. rst.push('<selection sqref="A1:' + colStr + '1"/>');
  523. rst.push('</sheetView></sheetViews>');
  524. rst.push('<sheetFormatPr defaultRowHeight="13.5"/>');
  525. private_setCols();
  526. private_setSheetData();
  527. private_setMergedCells();
  528. rst.push('<phoneticPr fontId="1" type="noConversion"/>');
  529. rst.push('<pageMargins left="0.315" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
  530. //rst.push('<pageSetup paperSize="9" fitToWidth="0" fitToHeight="0" orientation="landscape" horizontalDpi="300" verticalDpi="300"/>');
  531. rst.push('<headerFooter alignWithMargins="0"/>');
  532. rst.push('</worksheet>');
  533. //rst.push('');
  534. return rst;
  535. }
  536. module.exports = {
  537. exportExcel: function (pageData, options) {
  538. var rptOptions = (options || {singlePage: false, fileName: 'report'});
  539. }
  540. ,testWriteContentTypes: function(sheets) {
  541. return writeContentTypes(sheets);
  542. }
  543. ,testWriteRootRels: function() {
  544. return writeRootRels();
  545. }
  546. ,testWriteApp: function(sheets) {
  547. return writeApp(sheets);
  548. }
  549. ,testWriteCore: function() {
  550. return writeCore();
  551. }
  552. ,testWriteXlWorkBook: function(sheets) {
  553. return writeXlWorkBook(sheets);
  554. }
  555. ,testWriteXlRels: function(sheets) {
  556. return writeXlRels(sheets);
  557. }
  558. ,testWriteSheets: function(pageData, sharedStrList, stylesObj){
  559. return writeSheets(pageData, sharedStrList, stylesObj);
  560. }
  561. ,testWriteSharedString: function(sharedStrList){
  562. return writeSharedString(sharedStrList);
  563. }
  564. ,testWriteTheme: function() {
  565. return writeTheme();
  566. }
  567. ,testWriteStyles: function(stylesObj) {
  568. return writeStyles(stylesObj);
  569. }
  570. }