123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- /**
- * Created by Tony on 2017/4/1.
- */
- var JV = require('../rpt_component/Jpc_ValueDefine');
- var fs = require('fs');
- var jpcCmnHelper = require('../rpt_component/helper/Jpc_Helper_Common');
- var DPI = jpcCmnHelper.getScreenDPI()[0];
- const dftHeadXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
- function writeContentTypes(sheets) {
- var rst = [];
- rst.push(dftHeadXml + '\r\n');
- rst.push('<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">');
- //...
- rst.push('<Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/>');
- rst.push('<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>');
- rst.push('<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>');
- rst.push('<Default Extension="xml" ContentType="application/xml"/>');
- rst.push('<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>');
- rst.push('<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>');
- rst.push('<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>');
- for (var i = 0; i < sheets.length; i++) {
- rst.push('<Override PartName="/xl/worksheets/' + sheets[i].sheetName + '.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>')
- }
- rst.push('<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>');
- rst.push('</Types>');
- return rst;
- }
- function writeRootRels(){
- var rst = [];
- rst.push(dftHeadXml + '\r\n');
- rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
- rst.push('<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>');
- rst.push('<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>');
- rst.push('<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>');
- rst.push('</Relationships>');
- return rst;
- }
- function writeApp(sheets) {
- var rst = [];
- rst.push(dftHeadXml + '\r\n');
- rst.push('<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">');
- rst.push('<Application>Microsoft Excel</Application>');
- rst.push('<DocSecurity>0</DocSecurity>');
- rst.push('<ScaleCrop>false</ScaleCrop>');
- rst.push('<HeadingPairs>');
- rst.push('<vt:vector size="2" baseType="variant">');
- rst.push('<vt:variant><vt:lpstr>¹¤×÷±í</vt:lpstr></vt:variant>');
- rst.push('<vt:variant><vt:i4>' + sheets.length + '</vt:i4></vt:variant>');
- rst.push('</vt:vector>');
- rst.push('</HeadingPairs>');
- rst.push('<TitlesOfParts>');
- rst.push('<vt:vector size="' + sheets.length + '" baseType="lpstr">');
- for (var i = 0; i < sheets.length; i++) {
- rst.push('<vt:lpstr>' + sheets[i].sheetName + '</vt:lpstr>')
- }
- rst.push('</vt:vector>');
- rst.push('</TitlesOfParts>');
- rst.push('<Company>SmartCost</Company>');
- rst.push('<LinksUpToDate>false</LinksUpToDate>');
- rst.push('<SharedDoc>false</SharedDoc>');
- rst.push('<HyperlinksChanged>false</HyperlinksChanged>');
- rst.push('<AppVersion>12.0000</AppVersion>');
- //rst.push('');
- rst.push('</Properties>');
- return rst;
- }
- function writeCore() {
- var rst = [];
- p_fillZero = function(val){
- var rst = val;
- if (val < 10) {
- rst = '0' + val;
- }
- return rst;
- };
- rst.push(dftHeadXml + '\r\n');
- 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">');
- rst.push('<dc:creator>SmartCost</dc:creator>');
- rst.push('<cp:lastModifiedBy>SmartCost</cp:lastModifiedBy>');
- var dt = new Date(), dtStr = dt.getFullYear() + '-' + p_fillZero(dt.getMonth()) + '-' + p_fillZero(dt.getDate()) + 'T' +
- p_fillZero(dt.getHours()) + ':' + p_fillZero(dt.getMinutes()) + ':' + p_fillZero(dt.getSeconds()) + 'Z';
- rst.push('<dcterms:created xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:created>');
- rst.push('<dcterms:modified xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:modified>');
- //rst.push('');
- rst.push('</cp:coreProperties>');
- return rst;
- }
- function writeXlWorkBook(sheets){
- var rst = [];
- rst.push(dftHeadXml + '\r\n');
- rst.push('<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
- rst.push('<fileVersion appName="xl" lastEdited="4" lowestEdited="4" rupBuild="4505"/>');
- rst.push('<workbookPr defaultThemeVersion="124226"/>');
- rst.push('<bookViews><workbookView xWindow="360" yWindow="345" windowWidth="14655" windowHeight="4305"/></bookViews>');
- rst.push('<sheets>');
- for (var i = 0; i < sheets.length; i++) {
- rst.push('<sheet name="' + sheets[i].sheetName + '" sheetId="' + (i + 1) + 'r:id="rId' + (i + 1) + '1"/>')
- }
- rst.push('</sheets>');
- rst.push('<calcPr calcId="124519"/>');
- //rst.push('');
- rst.push('</workbook>');
- return rst;
- }
- function writeXlRels(sheets){
- var rst = [], idx = 1;
- rst.push(dftHeadXml + '\r\n');
- rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
- for (var i = 0; i < sheets.length; i++) {
- rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/' + sheets[i].sheetName + '.xml/>"')
- idx++;
- }
- rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>');
- idx++;
- rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>');
- idx++;
- rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/>');
- //rst.push('');
- rst.push('</Relationships>');
- return rst;
- }
- function writeTheme(){
- var rst = fs.readFileSync('./excel_base_files/theme1.xml', 'utf8', 'r');
- return rst;
- }
- function writeStyles(pageData){
- //
- }
- function writeSharedString(pageData){
- //
- }
- function writeSheets(pageData){
- var rst = [], sharedStrList = [];
- for (var i = 0; i < pageData.items.length; i++) {
- //
- }
- }
- function writeSheet(sheetData, sharedStrList, styleList){
- var rst = [], xPos = [0], yPos = [0], headerStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
- private_pre_analyze_pos = function(){
- var cell, pos;
- sheetData.cells.sort(function(cell1, cell2) {
- var rst = 0;
- if (cell1[JV.PROP_AREA][JV.PROP_TOP] > cell2[JV.PROP_AREA][JV.PROP_TOP]) {
- rst = 1;
- } else if (cell1[JV.PROP_AREA][JV.PROP_TOP] < cell2[JV.PROP_AREA][JV.PROP_TOP]) {
- rst = -1;
- } else {
- if (cell1[JV.PROP_AREA][JV.PROP_LEFT] > cell2[JV.PROP_AREA][JV.PROP_LEFT]) {
- rst = 1;
- } else if (cell1[JV.PROP_AREA][JV.PROP_LEFT] < cell2[JV.PROP_AREA][JV.PROP_LEFT]) {
- rst = -1;
- }
- }
- });
- for (var i = 0; i < sheetData.cells.length; i++) {
- cell = sheetData.cells[i];
- pos = cell[JV.PROP_AREA][JV.PROP_LEFT];
- if (xPos.indexOf(pos) < 0) xPos.push(pos);
- pos = cell[JV.PROP_AREA][JV.PROP_RIGHT];
- if (xPos.indexOf(pos) < 0) xPos.push(pos);
- pos = cell[JV.PROP_AREA][JV.PROP_TOP];
- if (yPos.indexOf(pos) < 0) yPos.push(pos);
- pos = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
- if (yPos.indexOf(pos) < 0) yPos.push(pos);
- }
- xPos.sort();
- yPos.sort();
- };
- private_getCellIdxStr = function(idx){
- var rst = 'A';
- if (idx < 26) {
- rst = headerStr[idx];
- } else if (idx < 26*26+26) {
- var ti = Math.floor(idx / 26), tj = idx % 26;
- rst = headerStr[ti - 1] + headerStr[tj];
- } else if (idx < 26*26*26+26) {
- var ti = Math.floor(idx / (26*26)), tj = Math.floor((idx - ti * 26*26) / 26), tk = idx % 26;
- rst = headerStr[ti - 1] + headerStr[tj-1] + headerStr[tk];
- }
- return rst;
- };
- private_getSharedStrIdx = function(val) {
- var rst = sharedStrList.indexOf(val);
- if (rst < 0) {
- sharedStrList.push(val);
- rst = sharedStrList.length - 1;
- }
- return rst;
- };
- private_getStyleIdx = function(cell) {
- var rst = 1;
- //
- return rst;
- };
- private_setCols = function(){
- rst.push('<cols>');
- var w = 0;
- for (var i = 1; i < xPos.length; i++) {
- w = 1.0 * (xPos[i] - xPos[i - 1]) / DPI;
- w = Math.round(w * 1000) / 1000;
- rst.push('<col min="1" max="1" width="' + w + '" customWidth="1"/>');
- }
- rst.push('</cols>');
- };
- private_setMergedCells = function() {
- var cell, idxR, idxL, idxT, idxB, cnt = 0;
- rst.push('<mergeCells count="?">');
- var startIdx = rst.length - 1;
- for (var i = 0; i < sheetData.cells.length; i++) {
- cell = sheetData.cells[i];
- idxR = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_RIGHT]);
- idxL = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_LEFT]);
- idxB = yPos.indexOf(cell[JV.PROP_AREA][JV.PROP_BOTTOM]);
- idxT = yPos.indexOf(cell[JV.PROP_AREA][JV.PROP_TOP]);
- if (idxR - idxL > 1 || idxB - idxT > 1) {
- rst.push('<mergeCell ref="' + private_getCellIdxStr(idxL - 1) + idxT + ':' + private_getCellIdxStr(idxR - 2) + (idxB - 1) + '"/>');
- cnt++;
- }
- }
- rst[startIdx] = '<mergeCells count="' + cnt + '">';
- rst.push('</mergeCells>');
- };
- private_setSheetData = function(){
- rst.push('<sheetData>');
- var spanX = xPos.length - 1, cellIdx = 0, h = 0, hasMoreCols = true;
- for (var i = 0; i < yPos.length - 1; i++) {
- if (i === 0) {
- h = 1.0 * (yPos[i]) / DPI;
- } else {
- h = 1.0 * (yPos[i] - yPos[i - 1]) / DPI;
- }
- h = Math.round(h * 1000) / 1000;
- rst.push('<row r="' + (i+1) + '" spans="1:' + spanX + '" ht="' + h + '" customHeight="1">');
- //then put the cells of this row
- var colIdxStr = '';
- for (var j = 0; j < xPos.length; j++) {
- colIdxStr = private_getCellIdxStr(j);
- if (hasMoreCols) {
- var ridx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
- } else {
- rst.push('<c r="' + colIdxStr + (i+1) + '" s="1"/>');
- }
- }
- rst.push('</row');
- }
- //sheetData.cells.length
- rst.push('</sheetData>');
- };
- private_pre_analyze_pos();
- rst.push(dftHeadXml + '\r\n');
- rst.push('<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
- var colStr = private_getCellIdxStr(xPos.length - 2);
- rst.push('<dimension ref="A1:' + colStr + '' + yPos.length + '"/>');
- rst.push('<sheetViews><sheetView tabSelected="1" workbookViewId="0">');
- rst.push('<selection sqref="A1:' + colStr + '1"/>');
- rst.push('</sheetView></sheetViews>');
- rst.push('<sheetFormatPr defaultRowHeight="13.5"/>');
- private_setCols
- private_setSheetData();
- private_setMergedCells();
- rst.push('<phoneticPr fontId="1" type="noConversion"/>');
- rst.push('<pageMargins left="0.315" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
- //rst.push('<pageSetup paperSize="9" fitToWidth="0" fitToHeight="0" orientation="landscape" horizontalDpi="300" verticalDpi="300"/>');
- rst.push('<headerFooter alignWithMargins="0"/>');
- rst.push('</worksheet>');
- //rst.push('');
- return rst;
- }
- module.exports = {
- exportExcel: function (pageData, options) {
- var rptOptions = (options || {singlePage: false, fileName: 'report'});
- }
- }
|