|
@@ -12,22 +12,31 @@ const fsUtil = require('../public/fsUtil');
|
|
|
const dftHeadXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
|
|
|
const uuidV1 = require('uuid/v1');
|
|
|
|
|
|
-function writeContentTypes(sheets, isSinglePage) {
|
|
|
+function writeContentTypes(sheets, isSinglePage, hasSignature) {
|
|
|
const 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"/>');
|
|
|
+ if (hasSignature) {
|
|
|
+ rst.push('<Default Extension="png" ContentType="image/png"/>');
|
|
|
+ }
|
|
|
+ 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('<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"/>');
|
|
|
if (isSinglePage) {
|
|
|
rst.push('<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>');
|
|
|
+ if (hasSignature) {
|
|
|
+ rst.push('<Override PartName="/xl/drawings/drawing1.xml" ContentType="application/vnd.openxmlformats-officedocument.drawing+xml"/>');
|
|
|
+ }
|
|
|
} else {
|
|
|
for (let i = 0; i < sheets.length; i++) {
|
|
|
rst.push('<Override PartName="/xl/worksheets/sheet' + (i + 1) + '.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>');
|
|
|
+ if (hasSignature) {
|
|
|
+ rst.push('<Override PartName="/xl/drawings/drawing' + (i + 1) + '.xml" ContentType="application/vnd.openxmlformats-officedocument.drawing+xml"/>');
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
rst.push('<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>');
|
|
@@ -146,7 +155,7 @@ function writeTheme() {
|
|
|
const rst = fs.readFileSync(__dirname + '/excel_base_files/theme1.xml', 'utf8', 'r');
|
|
|
return rst;
|
|
|
}
|
|
|
-function writeStyles(stylesObj) {
|
|
|
+function writeStyles(stylesObj, hasSignature) {
|
|
|
const rst = [];
|
|
|
rst.push(dftHeadXml + '\r\n');
|
|
|
rst.push('<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">');
|
|
@@ -271,6 +280,16 @@ function writeStyles(stylesObj) {
|
|
|
rst.push('<cellStyles count="1"><cellStyle name="常规" xfId="0" builtinId="0"/></cellStyles>');
|
|
|
rst.push('<dxfs count="0"/>');
|
|
|
rst.push('<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleLight16"/>');
|
|
|
+ if (hasSignature) {
|
|
|
+ rst.push('<extLst>');
|
|
|
+ rst.push('<ext uri="{EB79DEF2-80B8-43e5-95BD-54CBDDF9020C}" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main">');
|
|
|
+ rst.push('<x14:slicerStyles defaultSlicerStyle="SlicerStyleLight1"/>');
|
|
|
+ rst.push('</ext>');
|
|
|
+ rst.push('<ext uri="{9260A510-F301-46a8-8635-F512D64BE5F5}" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main">');
|
|
|
+ rst.push('<x15:timelineStyles defaultTimelineStyle="TimeSlicerStyleLight1"/>');
|
|
|
+ rst.push('</ext>');
|
|
|
+ rst.push('</extLst>');
|
|
|
+ }
|
|
|
rst.push('</styleSheet>');
|
|
|
return rst;
|
|
|
}
|
|
@@ -311,7 +330,7 @@ function writeSharedString(sharedStrList) {
|
|
|
}
|
|
|
return rst;
|
|
|
}
|
|
|
-function writeSheets(pageData, paperSize, sharedStrList, stylesObj, isSinglePage, custSheetMergeBands) {
|
|
|
+function writeSheets(pageData, paperSize, sharedStrList, stylesObj, isSinglePage, custSheetMergeBands, hasSignature) {
|
|
|
const rst = [];
|
|
|
const private_pushDftFont = function() {
|
|
|
const font = {};
|
|
@@ -352,20 +371,22 @@ function writeSheets(pageData, paperSize, sharedStrList, stylesObj, isSinglePage
|
|
|
};
|
|
|
private_pushDftFont();
|
|
|
private_buildFirstDftStyle();
|
|
|
+ let sheetIdx = 0;
|
|
|
if (isSinglePage) {
|
|
|
- rst.push(writeSheet(pageData, null, paperSize, sharedStrList, stylesObj, null));
|
|
|
+ rst.push(writeSheet(pageData, null, paperSize, sharedStrList, stylesObj, null, hasSignature, sheetIdx));
|
|
|
} else {
|
|
|
for (let i = 0; i < pageData.items.length; i++) {
|
|
|
let appointedMergeBand = null;
|
|
|
if (custSheetMergeBands && custSheetMergeBands.length > i) {
|
|
|
appointedMergeBand = custSheetMergeBands[i];
|
|
|
}
|
|
|
- rst.push(writeSheet(pageData, pageData.items[i], paperSize, sharedStrList, stylesObj, appointedMergeBand));
|
|
|
+ rst.push(writeSheet(pageData, pageData.items[i], paperSize, sharedStrList, stylesObj, appointedMergeBand, hasSignature, sheetIdx));
|
|
|
+ sheetIdx++;
|
|
|
}
|
|
|
}
|
|
|
return rst;
|
|
|
}
|
|
|
-function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, appointedMergeBand) {
|
|
|
+function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, appointedMergeBand, hasSignature, sheetIdx) {
|
|
|
const rst = [];
|
|
|
const xPos = [];
|
|
|
let yPos = [];
|
|
@@ -490,46 +511,10 @@ function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, ap
|
|
|
}
|
|
|
return rst;
|
|
|
};
|
|
|
- let private_checkBorder = function(cell, border, sheetBorder) {
|
|
|
- let rst = true, borderLineWidths = [], sheetBorderLineWidths = [];
|
|
|
- borderLineWidths.push(border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]);
|
|
|
- borderLineWidths.push(border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]);
|
|
|
- borderLineWidths.push(border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]);
|
|
|
- borderLineWidths.push(border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]);
|
|
|
- if (sheetBorder[JV.PROP_LEFT] && sheetBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]) {
|
|
|
- sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_LEFT, true));
|
|
|
- } else {
|
|
|
- sheetBorderLineWidths.push(0);
|
|
|
- }
|
|
|
- if (sheetBorder[JV.PROP_RIGHT] && sheetBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]) {
|
|
|
- sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_RIGHT, true));
|
|
|
- } else {
|
|
|
- sheetBorderLineWidths.push(0);
|
|
|
- }
|
|
|
- if (sheetBorder[JV.PROP_TOP] && sheetBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]) {
|
|
|
- sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_TOP, false));
|
|
|
- } else {
|
|
|
- sheetBorderLineWidths.push(0);
|
|
|
- }
|
|
|
- if (sheetBorder[JV.PROP_BOTTOM] && sheetBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]) {
|
|
|
- sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_BOTTOM, false));
|
|
|
- } else {
|
|
|
- sheetBorderLineWidths.push(0);
|
|
|
- }
|
|
|
- for (let i = 0; i < 4; i++) {
|
|
|
- if (borderLineWidths[i] != sheetBorderLineWidths[i]) {
|
|
|
- rst = false;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- return rst;
|
|
|
- };
|
|
|
- let private_chkAndGetMergeLine = function(cell, sheetBorder, borderStr, needFurtherChk) {
|
|
|
- let rst = 0,
|
|
|
- // mergeBorder = (sheetData)?sheetData[JV.PROP_PAGE_MERGE_BORDER]:pageData[JV.BAND_PROP_MERGE_BAND],
|
|
|
- mergeBorder = currentMergeBorder,
|
|
|
- mergeBand = pageData[JV.BAND_PROP_MERGE_BAND]
|
|
|
- ;
|
|
|
+ const private_chkAndGetMergeLine = function(cell, sheetBorder, borderStr, needFurtherChk) {
|
|
|
+ let rst = 0;
|
|
|
+ const mergeBorder = currentMergeBorder;
|
|
|
+ let mergeBand = pageData[JV.BAND_PROP_MERGE_BAND];
|
|
|
if (appointedMergeBand !== null) {
|
|
|
mergeBand = appointedMergeBand;
|
|
|
}
|
|
@@ -537,15 +522,15 @@ function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, ap
|
|
|
rst = sheetBorder[borderStr][JV.PROP_LINE_WEIGHT];
|
|
|
}
|
|
|
if (currentPageMergePos) {
|
|
|
- let side = currentPageMergePos[borderStr];
|
|
|
+ const side = currentPageMergePos[borderStr];
|
|
|
if (side.indexOf(cell[JV.PROP_AREA][borderStr]) >= 0) {
|
|
|
if (needFurtherChk) {
|
|
|
- let topSide = currentPageMergePos[JV.PROP_TOP];
|
|
|
- let bottomSide = currentPageMergePos[JV.PROP_BOTTOM];
|
|
|
+ const topSide = currentPageMergePos[JV.PROP_TOP];
|
|
|
+ const bottomSide = currentPageMergePos[JV.PROP_BOTTOM];
|
|
|
for (let i = 0; i < topSide.length; i++) {
|
|
|
if (cell[JV.PROP_AREA][JV.PROP_TOP] >= topSide[i]) {
|
|
|
if (cell[JV.PROP_AREA][JV.PROP_BOTTOM] <= bottomSide[i]) {
|
|
|
- let destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
|
|
|
+ const destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
|
|
|
rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
|
|
|
break;
|
|
|
}
|
|
@@ -554,13 +539,13 @@ function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, ap
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
- let destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
|
|
|
+ const destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
|
|
|
rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
if (cell[JV.PROP_AREA][borderStr] === mergeBorder[borderStr]) {
|
|
|
- let destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
|
|
|
+ const destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
|
|
|
if (needFurtherChk) {
|
|
|
if (cell[JV.PROP_AREA][JV.PROP_TOP] >= mergeBorder[JV.PROP_TOP] &&
|
|
|
cell[JV.PROP_AREA][JV.PROP_BOTTOM] <= mergeBorder[JV.PROP_BOTTOM]) {
|
|
@@ -573,8 +558,44 @@ function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, ap
|
|
|
}
|
|
|
return parseInt(rst);
|
|
|
};
|
|
|
- let private_getIniBorder = function() {
|
|
|
- let rst = {};
|
|
|
+ const private_checkBorder = function(cell, border, sheetBorder) {
|
|
|
+ let rst = true;
|
|
|
+ const borderLineWidths = [];
|
|
|
+ const sheetBorderLineWidths = [];
|
|
|
+ borderLineWidths.push(border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]);
|
|
|
+ borderLineWidths.push(border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]);
|
|
|
+ borderLineWidths.push(border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]);
|
|
|
+ borderLineWidths.push(border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]);
|
|
|
+ if (sheetBorder[JV.PROP_LEFT] && sheetBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]) {
|
|
|
+ sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_LEFT, true));
|
|
|
+ } else {
|
|
|
+ sheetBorderLineWidths.push(0);
|
|
|
+ }
|
|
|
+ if (sheetBorder[JV.PROP_RIGHT] && sheetBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]) {
|
|
|
+ sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_RIGHT, true));
|
|
|
+ } else {
|
|
|
+ sheetBorderLineWidths.push(0);
|
|
|
+ }
|
|
|
+ if (sheetBorder[JV.PROP_TOP] && sheetBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]) {
|
|
|
+ sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_TOP, false));
|
|
|
+ } else {
|
|
|
+ sheetBorderLineWidths.push(0);
|
|
|
+ }
|
|
|
+ if (sheetBorder[JV.PROP_BOTTOM] && sheetBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]) {
|
|
|
+ sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_BOTTOM, false));
|
|
|
+ } else {
|
|
|
+ sheetBorderLineWidths.push(0);
|
|
|
+ }
|
|
|
+ for (let i = 0; i < 4; i++) {
|
|
|
+ if (borderLineWidths[i] != sheetBorderLineWidths[i]) {
|
|
|
+ rst = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return rst;
|
|
|
+ };
|
|
|
+ const private_getIniBorder = function() {
|
|
|
+ const rst = {};
|
|
|
rst[JV.PROP_LEFT] = {};
|
|
|
rst[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = 0;
|
|
|
rst[JV.PROP_RIGHT] = {};
|
|
@@ -585,19 +606,20 @@ function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, ap
|
|
|
rst[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = 0;
|
|
|
return rst;
|
|
|
};
|
|
|
- let private_getBorderId = function(cell) {
|
|
|
- let rst = 0, hasBorder = false;
|
|
|
+ const private_getBorderId = function(cell) {
|
|
|
+ let rst = 0;
|
|
|
+ let hasBorder = false;
|
|
|
if (!(stylesObj.borders)) {
|
|
|
stylesObj.borders = [];
|
|
|
}
|
|
|
- let sheetBorder = pageData[JV.NODE_STYLE_COLLECTION][cell.style];
|
|
|
- let mergedBorder = private_getIniBorder();
|
|
|
+ const sheetBorder = pageData[JV.NODE_STYLE_COLLECTION][cell.style];
|
|
|
+ const mergedBorder = private_getIniBorder();
|
|
|
mergedBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_LEFT, true);
|
|
|
mergedBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_RIGHT, true);
|
|
|
mergedBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_TOP, false);
|
|
|
mergedBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_BOTTOM, false);
|
|
|
for (let i = 0; i < stylesObj.borders.length; i++) {
|
|
|
- let border = stylesObj.borders[i];
|
|
|
+ const border = stylesObj.borders[i];
|
|
|
if (private_checkBorder(cell, border, mergedBorder)) {
|
|
|
hasBorder = true;
|
|
|
rst = i;
|
|
@@ -610,7 +632,7 @@ function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, ap
|
|
|
}
|
|
|
return rst;
|
|
|
};
|
|
|
- let private_checkControl = function(cellControl, sheetControl) {
|
|
|
+ const private_checkControl = function(cellControl, sheetControl) {
|
|
|
let rst = true;
|
|
|
for (let i = 0; i < JV.CONTROL_PROPS.length; i++) {
|
|
|
if (cellControl[JV.CONTROL_PROPS[i]] != sheetControl[JV.CONTROL_PROPS[i]]) {
|
|
@@ -620,26 +642,27 @@ function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, ap
|
|
|
}
|
|
|
return rst;
|
|
|
};
|
|
|
- let private_getStyleId = function(cell) {
|
|
|
- let rst = 1, hasStyle = false;
|
|
|
+ const private_getStyleId = function(cell) {
|
|
|
+ let rst = 1;
|
|
|
+ let hasStyle = false;
|
|
|
if (!(stylesObj.cellXfs)) stylesObj.cellXfs = [];
|
|
|
- let fontId = private_getFontId(cell);
|
|
|
+ const fontId = private_getFontId(cell);
|
|
|
let fontAngle = 0;
|
|
|
- if (typeof cell[JV.PROP_FONT] === "string") {
|
|
|
+ if (typeof cell[JV.PROP_FONT] === 'string') {
|
|
|
fontAngle = parseInt(pageData[JV.NODE_FONT_COLLECTION][cell[JV.PROP_FONT]].FontAngle);
|
|
|
} else {
|
|
|
fontAngle = parseInt(cell[JV.PROP_FONT].FontAngle);
|
|
|
}
|
|
|
|
|
|
- let borderId = private_getBorderId(cell);
|
|
|
+ const borderId = private_getBorderId(cell);
|
|
|
let cellControl = null;
|
|
|
- if (typeof cell[JV.PROP_CONTROL] === "string") {
|
|
|
+ if (typeof cell[JV.PROP_CONTROL] === 'string') {
|
|
|
cellControl = pageData[JV.NODE_CONTROL_COLLECTION][cell[JV.PROP_CONTROL]];
|
|
|
} else {
|
|
|
cellControl = cell[JV.PROP_CONTROL];
|
|
|
}
|
|
|
for (let i = 0; i < stylesObj.cellXfs.length; i++) {
|
|
|
- let sheetControl = stylesObj.cellXfs[i];
|
|
|
+ const sheetControl = stylesObj.cellXfs[i];
|
|
|
if (sheetControl.fontId === fontId && sheetControl.borderId === borderId) {
|
|
|
if (private_checkControl(cellControl, sheetControl)) {
|
|
|
rst = i;
|
|
@@ -649,7 +672,7 @@ function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, ap
|
|
|
}
|
|
|
}
|
|
|
if (!hasStyle) {
|
|
|
- let sheetControl = {};
|
|
|
+ const sheetControl = {};
|
|
|
sheetControl.fontId = fontId;
|
|
|
sheetControl.borderId = borderId;
|
|
|
sheetControl.fontAngle = fontAngle;
|
|
@@ -661,29 +684,29 @@ function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, ap
|
|
|
}
|
|
|
return rst;
|
|
|
};
|
|
|
- let private_setCols = function(){
|
|
|
- //remark: 1 excel unit width = 2.117 mm
|
|
|
+ const private_setCols = function() {
|
|
|
+ // remark: 1 excel unit width = 2.117 mm
|
|
|
rst.push('<cols>');
|
|
|
let w = 0;
|
|
|
for (let i = 1; i < xPos.length - 1; i++) {
|
|
|
- w = 1.0 * (xPos[i + 1] - xPos[i]) / DPI * 25.4 / 2.117;
|
|
|
+ w = (xPos[i + 1] - xPos[i]) / DPI * 25.4 / 2.117;
|
|
|
w = Math.round(w * 1000) / 1000;
|
|
|
- rst.push('<col min="' + i +'" max="' + i +'" width="' + w + '" customWidth="1"/>');
|
|
|
+ rst.push('<col min="' + i + '" max="' + i + '" width="' + w + '" customWidth="1"/>');
|
|
|
}
|
|
|
- rst.push('<col min="' + (xPos.length - 1) +'" max="' + (xPos.length - 1) +'" width="' + 10 + '" customWidth="1"/>');
|
|
|
+ rst.push('<col min="' + (xPos.length - 1) + '" max="' + (xPos.length - 1) + '" width="' + 10 + '" customWidth="1"/>');
|
|
|
rst.push('</cols>');
|
|
|
};
|
|
|
- let private_setMergedCells = function() {
|
|
|
- let cell, idxR, idxL, idxT, idxB, cnt = 0;
|
|
|
+ const private_setMergedCells = function() {
|
|
|
+ let cnt = 0;
|
|
|
rst.push('<mergeCells count="?">');
|
|
|
- let startIdx = rst.length - 1;
|
|
|
- let self_setMergedCells = function (theData, theYPos, offsetY) {
|
|
|
+ const startIdx = rst.length - 1;
|
|
|
+ const self_setMergedCells = function(theData, theYPos, offsetY) {
|
|
|
for (let i = 0; i < theData.cells.length; i++) {
|
|
|
- cell = theData.cells[i];
|
|
|
- idxR = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_RIGHT]);
|
|
|
- idxL = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_LEFT]);
|
|
|
- idxB = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_BOTTOM]);
|
|
|
- idxT = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_TOP]);
|
|
|
+ const cell = theData.cells[i];
|
|
|
+ const idxR = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_RIGHT]);
|
|
|
+ const idxL = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_LEFT]);
|
|
|
+ const idxB = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_BOTTOM]);
|
|
|
+ const idxT = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_TOP]);
|
|
|
if (idxR - idxL > 1 || idxB - idxT > 1) {
|
|
|
rst.push('<mergeCell ref="' + private_getCellIdxStr(idxL - 1) + (idxT + offsetY) + ':' + private_getCellIdxStr(idxR - 2) + (idxB - 1 + offsetY) + '"/>');
|
|
|
cnt++;
|
|
@@ -695,8 +718,8 @@ function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, ap
|
|
|
} else {
|
|
|
let osY = 0;
|
|
|
for (let i = 0; i < pageData.items.length; i++) {
|
|
|
- let shtItemData = pageData.items[i];
|
|
|
- let tmpPos = yMultiPos[i];
|
|
|
+ const shtItemData = pageData.items[i];
|
|
|
+ const tmpPos = yMultiPos[i];
|
|
|
self_setMergedCells(shtItemData, tmpPos, osY);
|
|
|
osY += tmpPos.length - 2;
|
|
|
}
|
|
@@ -704,43 +727,47 @@ function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, ap
|
|
|
rst[startIdx] = '<mergeCells count="' + cnt + '">';
|
|
|
rst.push('</mergeCells>');
|
|
|
};
|
|
|
- let private_setSheetData = function(){
|
|
|
- //remark: 1 excel unit height = 0.3612 mm
|
|
|
+ const private_setSheetData = function() {
|
|
|
+ // remark: 1 excel unit height = 0.3612 mm
|
|
|
rst.push('<sheetData>');
|
|
|
- let spanX = xPos.length - 2, cellIdx = 0, h = 0
|
|
|
- ;
|
|
|
- let self_setDataEx = function (theShtData, theYPos, offsetY) {
|
|
|
- let rows = [];
|
|
|
- //1. build full set of blank rows/cells
|
|
|
+ const spanX = xPos.length - 2;
|
|
|
+ let cellIdx = 0;
|
|
|
+ let h = 0;
|
|
|
+ const self_setDataEx = function(theShtData, theYPos, offsetY) {
|
|
|
+ const rows = [];
|
|
|
+ // 1. build full set of blank rows/cells
|
|
|
for (let i = 1; i < theYPos.length - 1; i++) {
|
|
|
- let rowObj = {};
|
|
|
- h = (theYPos[i+1] - theYPos[i]) / DPI * 25.4 / 0.3612;
|
|
|
+ const rowObj = {};
|
|
|
+ h = (theYPos[i + 1] - theYPos[i]) / DPI * 25.4 / 0.3612;
|
|
|
h = Math.round(h * 1000) / 1000;
|
|
|
rowObj.height = h;
|
|
|
rowObj.r = i + offsetY;
|
|
|
rowObj.items = [];
|
|
|
rows.push(rowObj);
|
|
|
for (let j = 1; j < xPos.length - 1; j++) {
|
|
|
- let colIdxStr = private_getCellIdxStr(j - 1);
|
|
|
- let cellObj = {};
|
|
|
+ const colIdxStr = private_getCellIdxStr(j - 1);
|
|
|
+ const cellObj = {};
|
|
|
cellObj.r = colIdxStr + (i + offsetY);
|
|
|
cellObj.s = 0;
|
|
|
cellObj.isBlank = true;
|
|
|
rows[i - 1].items.push(cellObj);
|
|
|
}
|
|
|
}
|
|
|
- //2. then fill up the cell style-ids and values
|
|
|
- let rowIdx1 = 0, colIdx1 = 0, rowIdx2 = 0, colIdx2 = 0, colIdxStr = '';
|
|
|
+ // 2. then fill up the cell style-ids and values
|
|
|
+ let rowIdx1 = 0;
|
|
|
+ let colIdx1 = 0;
|
|
|
+ let rowIdx2 = 0;
|
|
|
+ let colIdx2 = 0;
|
|
|
+ // let colIdxStr = '';
|
|
|
for (let cIdx = 0; cIdx < theShtData.cells.length; cIdx++) {
|
|
|
- let styleIdx = private_getStyleId(theShtData.cells[cIdx]);
|
|
|
- //colIdxStr = private_getCellIdxStr(j - 1);
|
|
|
+ const styleIdx = private_getStyleId(theShtData.cells[cIdx]);
|
|
|
rowIdx1 = theYPos.indexOf(theShtData.cells[cIdx][JV.PROP_AREA][JV.PROP_TOP]);
|
|
|
colIdx1 = xPos.indexOf(theShtData.cells[cIdx][JV.PROP_AREA][JV.PROP_LEFT]);
|
|
|
let cellObj = rows[rowIdx1 - 1].items[colIdx1 - 1];
|
|
|
cellObj.s = styleIdx;
|
|
|
cellObj.isBlank = false;
|
|
|
if (!(strUtil.isEmptyString(theShtData.cells[cIdx][JV.PROP_VALUE]))) {
|
|
|
- let valIdx = private_getSharedStrIdx(theShtData.cells[cIdx][JV.PROP_VALUE]);
|
|
|
+ const valIdx = private_getSharedStrIdx(theShtData.cells[cIdx][JV.PROP_VALUE]);
|
|
|
cellObj.v = valIdx;
|
|
|
}
|
|
|
|
|
@@ -757,11 +784,11 @@ function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, ap
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- //3. then fill up rst
|
|
|
+ // 3. then fill up rst
|
|
|
for (let i = 0; i < rows.length; i++) {
|
|
|
rst.push('<row r="' + (i + 1 + offsetY) + '" spans="1:' + spanX + '" ht="' + rows[i].height + '" customHeight="1">');
|
|
|
for (let j = 0; j < rows[i].items.length; j++) {
|
|
|
- let cellObj = rows[i].items[j];
|
|
|
+ const cellObj = rows[i].items[j];
|
|
|
if (cellObj.v === undefined) {
|
|
|
rst.push('<c r="' + cellObj.r + '" s="' + cellObj.s + '"/>');
|
|
|
} else {
|
|
@@ -772,22 +799,22 @@ function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, ap
|
|
|
}
|
|
|
rst.push('</row>');
|
|
|
}
|
|
|
- //4. maybe need to dispose the memory
|
|
|
- //...
|
|
|
+ // 4. maybe need to dispose the memory
|
|
|
+ // ...
|
|
|
};
|
|
|
if (sheetData) {
|
|
|
- //current sheet data
|
|
|
+ // current sheet data
|
|
|
currentPageMergePos = sheetData[JV.PAGE_SPECIAL_MERGE_POS];
|
|
|
currentMergeBorder = sheetData[JV.PROP_PAGE_MERGE_BORDER];
|
|
|
self_setDataEx(sheetData, yPos, 0);
|
|
|
} else {
|
|
|
- //total data in one sheet
|
|
|
+ // total data in one sheet
|
|
|
let cnt = 0;
|
|
|
for (let i = 0; i < pageData.items.length; i++) {
|
|
|
- let shtItemData = pageData.items[i];
|
|
|
+ const shtItemData = pageData.items[i];
|
|
|
currentPageMergePos = shtItemData[JV.PAGE_SPECIAL_MERGE_POS];
|
|
|
currentMergeBorder = shtItemData[JV.PROP_PAGE_MERGE_BORDER];
|
|
|
- let tmpPos = yMultiPos[i];
|
|
|
+ const tmpPos = yMultiPos[i];
|
|
|
cellIdx = 0;
|
|
|
self_setDataEx(shtItemData, tmpPos, cnt);
|
|
|
cnt += tmpPos.length - 2;
|
|
@@ -799,10 +826,10 @@ function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, ap
|
|
|
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">');
|
|
|
- let colStr = private_getCellIdxStr(xPos.length - 3);
|
|
|
+ const colStr = private_getCellIdxStr(xPos.length - 3);
|
|
|
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('<selection sqref="A1:' + colStr + '1"/>');
|
|
|
rst.push('<selection sqref="A1:A1"/>');
|
|
|
rst.push('</sheetView></sheetViews>');
|
|
|
rst.push('<sheetFormatPr defaultRowHeight="13.5"/>');
|
|
@@ -813,27 +840,76 @@ function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, ap
|
|
|
// rst.push('<pageMargins left="0.315" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
|
|
|
rst.push('<pageMargins left="' + (parseFloat(pageData[JV.NODE_PAGE_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]) * 0.39375) +
|
|
|
'" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
|
|
|
- let paperSizeIdx = JV.PAGES_SIZE_STR.indexOf(paperSize);
|
|
|
+ const paperSizeIdx = JV.PAGES_SIZE_STR.indexOf(paperSize);
|
|
|
let pStr = '';
|
|
|
if (paperSizeIdx >= 0) {
|
|
|
pStr = 'paperSize="' + JV.PAGES_SIZE_IDX[paperSizeIdx] + '"';
|
|
|
}
|
|
|
- let orientationStr = (pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][0] > pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1])?'landscape':'portrait';
|
|
|
+ let orientationStr = (pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][0] > pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1]) ? 'landscape' : 'portrait';
|
|
|
if (currentPageMergePos) {
|
|
|
- orientationStr = (currentPageMergePos[JV.NODE_PAGE_SIZE][0] > currentPageMergePos[JV.NODE_PAGE_SIZE][1])?'landscape':'portrait';
|
|
|
+ orientationStr = (currentPageMergePos[JV.NODE_PAGE_SIZE][0] > currentPageMergePos[JV.NODE_PAGE_SIZE][1]) ? 'landscape' : 'portrait';
|
|
|
}
|
|
|
rst.push('<pageSetup ' + pStr + ' fitToWidth="0" fitToHeight="0" orientation="' + orientationStr + '" />');
|
|
|
rst.push('<headerFooter alignWithMargins="0"/>');
|
|
|
+ if (hasSignature) {
|
|
|
+ rst.push('<drawing r:id="rId' + (sheetIdx + 1) + '"/>');
|
|
|
+ }
|
|
|
rst.push('</worksheet>');
|
|
|
return rst;
|
|
|
}
|
|
|
+function writeSheetRels(sheetIdx) {
|
|
|
+ const rst = [];
|
|
|
+ rst.push('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
|
|
|
+ rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
|
|
|
+ rst.push('<Relationship Id="rId' + (sheetIdx + 1) + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing" Target="../drawings/drawing' + (sheetIdx + 1) + '.xml"/>');
|
|
|
+ rst.push('</Relationships>');
|
|
|
+ return rst;
|
|
|
+}
|
|
|
+function writeImage(path, pic, baseDir) {
|
|
|
+ let rst = null;
|
|
|
+ if (pic) {
|
|
|
+ // 这里pic的数据就是base64的,不能是其他
|
|
|
+ rst = base64ToStream(pic);
|
|
|
+ } else if (path) {
|
|
|
+ const filePath = baseDir + '/app' + path;
|
|
|
+ rst = fs.readFileSync(filePath);
|
|
|
+ }
|
|
|
+ return rst;
|
|
|
+}
|
|
|
+function writeDrawings(pageData, signKeyArr, signPathArr, isSinglePage) {
|
|
|
+ const rst = [];
|
|
|
+ let sheetIdx = 0;
|
|
|
+ if (isSinglePage) {
|
|
|
+ rst.push(writeDrawing(pageData, null, signKeyArr, signPathArr, sheetIdx));
|
|
|
+ } else {
|
|
|
+ for (let i = 0; i < pageData.items.length; i++) {
|
|
|
+ rst.push(writeDrawing(pageData, pageData.items[i], signKeyArr, signPathArr, sheetIdx));
|
|
|
+ sheetIdx++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return rst;
|
|
|
+}
|
|
|
+function writeDrawing(pageData, sheetData, signKeyArr, signPathArr, sheetIdx) {
|
|
|
+ //
|
|
|
+}
|
|
|
+function writeDrawingsRels(amt) {
|
|
|
+ const rst = [];
|
|
|
+ rst.push('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
|
|
|
+ rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
|
|
|
+ for (let idx = 0; idx < amt; idx++) {
|
|
|
+ rst.push('<Relationship Id="rId' + (idx + 1) + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/image' + (idx + 1) + '.png"/>');
|
|
|
+ }
|
|
|
+ rst.push('</Relationships>');
|
|
|
+ return rst;
|
|
|
+}
|
|
|
|
|
|
function mergeProperties(orgObj, newObj) {
|
|
|
- let orgPropArr = [], newPropArr = [];
|
|
|
- for (let p in orgObj) {
|
|
|
+ const orgPropArr = [];
|
|
|
+ const newPropArr = [];
|
|
|
+ for (const p in orgObj) {
|
|
|
orgPropArr.push(p);
|
|
|
}
|
|
|
- for (let p in newObj) {
|
|
|
+ for (const p in newObj) {
|
|
|
newPropArr.push(p);
|
|
|
}
|
|
|
for (let i = 0; i < newPropArr.length; i++) {
|
|
@@ -842,111 +918,189 @@ function mergeProperties(orgObj, newObj) {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+function checkAndSetSignatureCache(pageData, signKeyArr, signPathArr) {
|
|
|
+ // 备注:电子签名是以图形的方式处理,一页可以有多个签名,多页的签名基本是引用同样的图片,在这里先处理一下,后期统一引用。
|
|
|
+ // 另:以后的图片(在电子签名(signature_cells)以外的图片)会单独处理(如计算图等)
|
|
|
+ let rst = false;
|
|
|
+ for (const page of pageData.items) {
|
|
|
+ if (page[JV.PROP_SIGNATURE_CELLS] && page[JV.PROP_SIGNATURE_CELLS].length > 0) {
|
|
|
+ for (const signature of page[JV.PROP_SIGNATURE_CELLS]) {
|
|
|
+ if (signKeyArr.indexOf(signature.signature_name) < 0) {
|
|
|
+ signKeyArr.push(signature.signature_name);
|
|
|
+ const signPath = { path: null, pic: null };
|
|
|
+ signPathArr.push(signPath);
|
|
|
+ if (signature.pic) {
|
|
|
+ signPath.pic = signature.pic;
|
|
|
+ rst = true;
|
|
|
+ } else if (signature.path) {
|
|
|
+ signPath.path = signature.path;
|
|
|
+ rst = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return rst;
|
|
|
+}
|
|
|
+function base64ToStream(dataurl) {
|
|
|
+ // 将base64转换为流
|
|
|
+ const arr = dataurl.split(',');
|
|
|
+ const bstr = atob(arr[1]);
|
|
|
+ let n = bstr.length;
|
|
|
+ const u8arr = new Uint8Array(n);
|
|
|
+ while (n--) {
|
|
|
+ u8arr[n] = bstr.charCodeAt(n);
|
|
|
+ }
|
|
|
+ return u8arr;
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
module.exports = {
|
|
|
- exportExcel: function (pageData, paperSize, fName, options, custSheetNames, custSheetMergeBands, callback) {
|
|
|
- let rptOptions = ({singlePage: false, fileName: 'report'});
|
|
|
+ exportExcel: function(pageData, paperSize, fName, options, custSheetNames, custSheetMergeBands, baseDir, callback) {
|
|
|
+ const rptOptions = ({ singlePage: false, fileName: 'report' });
|
|
|
if (options === 'true' || options === true) {
|
|
|
rptOptions.singlePage = true;
|
|
|
}
|
|
|
- let isSinglePage = rptOptions.singlePage;
|
|
|
- let sheets = [];
|
|
|
+ const isSinglePage = rptOptions.singlePage;
|
|
|
+ const sheets = [];
|
|
|
+ const signKeyArr = [];
|
|
|
+ const signPathArr = [];
|
|
|
+ // const hasSignature = checkAndSetSignatureCache(pageData, signKeyArr, signPathArr);
|
|
|
+ const hasSignature = false;
|
|
|
if (isSinglePage) {
|
|
|
- sheets.push({sheetName: '全部页'});
|
|
|
+ sheets.push({ sheetName: '全部页' });
|
|
|
} else {
|
|
|
if (custSheetNames && custSheetNames.length === pageData.items.length) {
|
|
|
for (let i = 0; i < pageData.items.length; i++) {
|
|
|
- sheets.push({sheetName: custSheetNames[i]});
|
|
|
+ sheets.push({ sheetName: custSheetNames[i] });
|
|
|
}
|
|
|
} else {
|
|
|
for (let i = 0; i < pageData.items.length; i++) {
|
|
|
- sheets.push({sheetName: '第' + (i + 1) + '页'});
|
|
|
+ sheets.push({ sheetName: '第' + (i + 1) + '页' });
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- //1.
|
|
|
+ // 1.
|
|
|
let file = '[Content_Types].xml';
|
|
|
- let data = writeContentTypes(sheets, isSinglePage);
|
|
|
- let zip = new JSZip();
|
|
|
- zip.file(file, data.join(''), {compression: 'DEFLATE'});
|
|
|
- //2.
|
|
|
- let zip_rels = zip.folder('_rels');
|
|
|
+ let data = writeContentTypes(sheets, isSinglePage, hasSignature);
|
|
|
+ const zip = new JSZip();
|
|
|
+ zip.file(file, data.join(''), { compression: 'DEFLATE' });
|
|
|
+ // 2.
|
|
|
+ const zip_rels = zip.folder('_rels');
|
|
|
file = '.rels';
|
|
|
data = writeRootRels();
|
|
|
- zip_rels.file(file, data.join(''), {compression: 'DEFLATE'});
|
|
|
- //3.
|
|
|
- let zip_docProps = zip.folder('docProps');
|
|
|
+ zip_rels.file(file, data.join(''), { compression: 'DEFLATE' });
|
|
|
+ // 3.
|
|
|
+ const zip_docProps = zip.folder('docProps');
|
|
|
file = 'app.xml';
|
|
|
data = writeApp(sheets, isSinglePage);
|
|
|
- zip_docProps.file(file, data.join(''), {compression: 'DEFLATE'});
|
|
|
+ zip_docProps.file(file, data.join(''), { compression: 'DEFLATE' });
|
|
|
file = 'core.xml';
|
|
|
data = writeCore();
|
|
|
- zip_docProps.file(file, data.join(''), {compression: 'DEFLATE'});
|
|
|
- //4.
|
|
|
- let zip_xl = zip.folder('xl');
|
|
|
+ zip_docProps.file(file, data.join(''), { compression: 'DEFLATE' });
|
|
|
+ // 4.
|
|
|
+ const zip_xl = zip.folder('xl');
|
|
|
file = 'workbook.xml';
|
|
|
data = writeXlWorkBook(sheets, isSinglePage);
|
|
|
- zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
|
|
|
- let zip_rels2 = zip_xl.folder('_rels');
|
|
|
+ zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
|
|
|
+ const zip_rels2 = zip_xl.folder('_rels');
|
|
|
file = 'workbook.xml.rels';
|
|
|
data = writeXlRels(sheets, isSinglePage);
|
|
|
- zip_rels2.file(file, data.join(''), {compression: 'DEFLATE'});
|
|
|
- //5.
|
|
|
- let zip_theme = zip_xl.folder('theme');
|
|
|
+ zip_rels2.file(file, data.join(''), { compression: 'DEFLATE' });
|
|
|
+ // 5.
|
|
|
+ const zip_theme = zip_xl.folder('theme');
|
|
|
file = 'theme1.xml';
|
|
|
data = writeTheme();
|
|
|
- zip_theme.file(file, data, {compression: 'DEFLATE'});
|
|
|
- //6.
|
|
|
- let zip_worksheets = zip_xl.folder('worksheets');
|
|
|
- let sharedStrList = [], stylesObj = {};
|
|
|
- data = writeSheets(pageData, paperSize, sharedStrList, stylesObj, isSinglePage, custSheetMergeBands);
|
|
|
+ zip_theme.file(file, data, { compression: 'DEFLATE' });
|
|
|
+ if (hasSignature) {
|
|
|
+ // 5.1
|
|
|
+ const zip_media = zip_xl.folder('media');
|
|
|
+ for (let signIdx = 0; signIdx < signPathArr.length; signIdx++) {
|
|
|
+ data = writeImage(signPathArr[signIdx].path, signPathArr[signIdx].pic, baseDir);
|
|
|
+ file = 'image' + (signIdx + 1) + '.png';
|
|
|
+ zip_media.file(file, data, { compression: 'DEFLATE' });
|
|
|
+ }
|
|
|
+ // 5.2
|
|
|
+ const zip_drawings = zip_xl.folder('drwaings');
|
|
|
+ data = writeDrawings(pageData, signKeyArr, signPathArr);
|
|
|
+ file = 'drawing1.xml.rels';
|
|
|
+ zip_drawings.file(file, data, { compression: 'DEFLATE' });
|
|
|
+ // 5.3
|
|
|
+ const zip_drawings_rels = zip_drawings.folder('_rels');
|
|
|
+ data = writeDrawingsRels(signPathArr.length);
|
|
|
+ file = 'drawing1.xml.rels';
|
|
|
+ zip_drawings_rels.file(file, data, { compression: 'DEFLATE' });
|
|
|
+ }
|
|
|
+ // 6.
|
|
|
+ const zip_worksheets = zip_xl.folder('worksheets');
|
|
|
+ const sharedStrList = [];
|
|
|
+ const stylesObj = {};
|
|
|
+ data = writeSheets(pageData, paperSize, sharedStrList, stylesObj, isSinglePage, custSheetMergeBands, hasSignature);
|
|
|
if (isSinglePage) {
|
|
|
for (let i = 0; i < 1; i++) {
|
|
|
file = 'sheet' + (i + 1) + '.xml';
|
|
|
- zip_worksheets.file(file, data[i].join(''), {compression: 'DEFLATE'});
|
|
|
+ zip_worksheets.file(file, data[i].join(''), { compression: 'DEFLATE' });
|
|
|
}
|
|
|
} else {
|
|
|
for (let i = 0; i < data.length; i++) {
|
|
|
file = 'sheet' + (i + 1) + '.xml';
|
|
|
- zip_worksheets.file(file, data[i].join(''), {compression: 'DEFLATE'});
|
|
|
+ zip_worksheets.file(file, data[i].join(''), { compression: 'DEFLATE' });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 7.
|
|
|
+ if (hasSignature) {
|
|
|
+ const zip_worksheets_rels = zip_xl.folder('_rels');
|
|
|
+ if (isSinglePage) {
|
|
|
+ for (let i = 0; i < 1; i++) {
|
|
|
+ data = writeSheetRels(0);
|
|
|
+ file = 'sheet' + (i + 1) + '.xml.rels';
|
|
|
+ zip_worksheets_rels.file(file, data[i].join(''), { compression: 'DEFLATE' });
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for (let i = 0; i < data.length; i++) {
|
|
|
+ data = writeSheetRels(0);
|
|
|
+ file = 'sheet' + (i + 1) + '.xml.rels';
|
|
|
+ zip_worksheets_rels.file(file, data[i].join(''), { compression: 'DEFLATE' });
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
file = 'sharedStrings.xml';
|
|
|
data = writeSharedString(sharedStrList);
|
|
|
- zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
|
|
|
+ zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
|
|
|
file = 'styles.xml';
|
|
|
- data = writeStyles(stylesObj);
|
|
|
- zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
|
|
|
+ data = writeStyles(stylesObj, hasSignature);
|
|
|
+ zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
|
|
|
|
|
|
if (fName) {
|
|
|
// let newName = '' + (new Date()).valueOf();
|
|
|
- let newName = uuidV1();
|
|
|
-
|
|
|
+ const newName = uuidV1();
|
|
|
// console.log('current path: ' + __dirname);
|
|
|
// const filePath = this.app.baseDir + '/app/public/download/';
|
|
|
-
|
|
|
- zip.generateNodeStream({type:'nodebuffer',streamFiles:true})
|
|
|
+ zip.generateNodeStream({ type: 'nodebuffer', streamFiles: true })
|
|
|
.pipe(fs.createWriteStream(__dirname.slice(0, __dirname.length - 13) + '/public/download/' + newName + '.xlsx'))
|
|
|
- .on('finish', function () {
|
|
|
- // JSZip generates a readable stream with a "end" event,
|
|
|
- // but is piped here in a writable stream which emits a "finish" event.
|
|
|
- console.log(newName + ".xlsx was written.");
|
|
|
- if (callback) callback(null, newName);
|
|
|
- }
|
|
|
- );
|
|
|
+ .on('finish', function() {
|
|
|
+ // JSZip generates a readable stream with a "end" event,
|
|
|
+ // but is piped here in a writable stream which emits a "finish" event.
|
|
|
+ console.log(newName + '.xlsx was written.');
|
|
|
+ if (callback) callback(null, newName);
|
|
|
+ });
|
|
|
} else {
|
|
|
- //return zip.generateNodeStream({type:'nodebuffer',streamFiles:true});
|
|
|
+ // return zip.generateNodeStream({type:'nodebuffer',streamFiles:true});
|
|
|
return zip;
|
|
|
}
|
|
|
},
|
|
|
|
|
|
- exportExcelInOneBook: function (pageDataArray, paperSize, fName, callback) {
|
|
|
- let me = this, newPageData = {};
|
|
|
- //1. 重新编排一下数据,把一份报表的pageData合并到一起作为一个Sheet输出(需要重新调整数据纵向坐标),多份报表数据就形成多个Sheet
|
|
|
- // -- 简单来说,就是重新包装数据
|
|
|
+ exportExcelInOneBook: function(pageDataArray, paperSize, fName, baseDir, callback) {
|
|
|
+ const me = this;
|
|
|
+ const newPageData = {};
|
|
|
+ // 1. 重新编排一下数据,把一份报表的pageData合并到一起作为一个Sheet输出(需要重新调整数据纵向坐标),多份报表数据就形成多个Sheet
|
|
|
+ // -- 简单来说,就是重新包装数据
|
|
|
try {
|
|
|
// 1.1 newPageData外围属性
|
|
|
- let newContrl = {}, newFont = {}, newStyle = {}, custMergeBands = [];
|
|
|
+ const newContrl = {};
|
|
|
+ const newFont = {};
|
|
|
+ const newStyle = {};
|
|
|
+ const custMergeBands = [];
|
|
|
for (let i = 0; i < pageDataArray.length; i++) {
|
|
|
mergeProperties(newContrl, pageDataArray[i][JV.NODE_CONTROL_COLLECTION]);
|
|
|
mergeProperties(newFont, pageDataArray[i][JV.NODE_FONT_COLLECTION]);
|
|
@@ -959,12 +1113,13 @@ module.exports = {
|
|
|
newPageData[JV.BAND_PROP_MERGE_BAND] = pageDataArray[0][JV.BAND_PROP_MERGE_BAND];
|
|
|
|
|
|
// 1.2 重新设置pageDataArray的各个cell的Top/Bottom坐标
|
|
|
- let sheetNames = [], newPagePos = [];
|
|
|
+ const sheetNames = [];
|
|
|
+ const newPagePos = [];
|
|
|
for (let i = 0; i < pageDataArray.length; i++) {
|
|
|
let offsetY = 0;
|
|
|
- let mergeBand = {};
|
|
|
+ const mergeBand = {};
|
|
|
custMergeBands.push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND]);
|
|
|
- //备注:不同的报表有可能有不同的边框,如封面表就是无边框的
|
|
|
+ // 备注:不同的报表有可能有不同的边框,如封面表就是无边框的
|
|
|
mergeBand[JV.PROP_LEFT] = [];
|
|
|
mergeBand[JV.PROP_RIGHT] = [];
|
|
|
mergeBand[JV.PROP_TOP] = [];
|
|
@@ -975,7 +1130,8 @@ module.exports = {
|
|
|
sheetNames.push(pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_MAIN_INFO_RPT_NAME]);
|
|
|
|
|
|
for (let j = 0; j < pageDataArray[i].items.length; j++) {
|
|
|
- let maxY = 0, minY = 100000;
|
|
|
+ let maxY = 0;
|
|
|
+ let minY = 100000;
|
|
|
if (pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS]) {
|
|
|
let pos = pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_TOP][0] + offsetY;
|
|
|
mergeBand[JV.PROP_TOP].push(pos);
|
|
@@ -1000,17 +1156,17 @@ module.exports = {
|
|
|
pageDataArray[i].items[j].cells[k][JV.PROP_AREA][JV.PROP_BOTTOM] += offsetY;
|
|
|
pageDataArray[i].items[j].cells[k][JV.PROP_AREA][JV.PROP_TOP] += offsetY;
|
|
|
}
|
|
|
- let bottomGap = Math.round( (pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1] - parseFloat(pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM]) / 2.54 ) * DPI) - maxY;
|
|
|
+ const bottomGap = Math.round( (pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1] - parseFloat(pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM]) / 2.54 ) * DPI) - maxY;
|
|
|
offsetY += (maxY - minY);
|
|
|
if (bottomGap > 10) {
|
|
|
offsetY += (bottomGap - 10);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- //2. newPageData的items属性
|
|
|
+ // 2. newPageData的items属性
|
|
|
newPageData.items = [];
|
|
|
for (let i = 0; i < pageDataArray.length; i++) {
|
|
|
- let pageItem = {};
|
|
|
+ const pageItem = {};
|
|
|
pageItem[JV.PROP_PAGE_SEQ] = i + 1;
|
|
|
pageItem[JV.PROP_CELLS] = [];
|
|
|
for (let j = 0; j < pageDataArray[i].items.length; j++) {
|
|
@@ -1022,11 +1178,11 @@ module.exports = {
|
|
|
pageItem[JV.PAGE_SPECIAL_MERGE_POS] = newPagePos[i];
|
|
|
newPageData.items.push(pageItem);
|
|
|
}
|
|
|
- //3. everything is ok, then call me
|
|
|
- me.exportExcel(newPageData, paperSize, fName, 'false', sheetNames, custMergeBands, callback);
|
|
|
+ // 3. everything is ok, then call me
|
|
|
+ me.exportExcel(newPageData, paperSize, fName, 'false', sheetNames, custMergeBands, baseDir, callback);
|
|
|
// fsUtil.writeObjToFile(newPageData, 'D:/GitHome/ConstructionOperation/tmp/combinedHeader.js');
|
|
|
} catch (e) {
|
|
|
console.log(e);
|
|
|
}
|
|
|
- }
|
|
|
-}
|
|
|
+ },
|
|
|
+};
|