|
@@ -3,6 +3,7 @@
|
|
|
*/
|
|
|
var JV = require('../rpt_component/Jpc_ValueDefine');
|
|
|
var fs = require('fs');
|
|
|
+var JSZip = require("jszip");
|
|
|
var strUtil = require('../../../public/stringUtil');
|
|
|
var jpcCmnHelper = require('../rpt_component/helper/Jpc_Helper_Common');
|
|
|
var DPI = jpcCmnHelper.getScreenDPI()[0];
|
|
@@ -21,7 +22,7 @@ function writeContentTypes(sheets) {
|
|
|
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="/xl/worksheets/sheet' + (i + 1) + '.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>');
|
|
@@ -46,7 +47,7 @@ function writeApp(sheets) {
|
|
|
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:lpstr>工作表</vt:lpstr></vt:variant>');
|
|
|
rst.push('<vt:variant><vt:i4>' + sheets.length + '</vt:i4></vt:variant>');
|
|
|
rst.push('</vt:vector>');
|
|
|
rst.push('</HeadingPairs>');
|
|
@@ -96,7 +97,7 @@ function writeXlWorkBook(sheets){
|
|
|
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('<sheet name="' + sheets[i].sheetName + '" sheetId="' + (i + 1) + '" r:id="rId' + (i + 1) + '"/>')
|
|
|
}
|
|
|
rst.push('</sheets>');
|
|
|
rst.push('<calcPr calcId="124519"/>');
|
|
@@ -109,7 +110,7 @@ function writeXlRels(sheets){
|
|
|
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/>"')
|
|
|
+ rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet' + (i + 1) + '.xml"/>')
|
|
|
idx++;
|
|
|
}
|
|
|
rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>');
|
|
@@ -143,7 +144,7 @@ function writeStyles(stylesObj){
|
|
|
rst.push('<charset val="' + font.charset + '"/>');
|
|
|
rst.push('</font>');
|
|
|
}
|
|
|
- rst.push('<fonts>');
|
|
|
+ rst.push('</fonts>');
|
|
|
//2. push default fills
|
|
|
rst.push('<fills count="2"><fill><patternFill patternType="none" /></fill><fill><patternFill patternType="gray125" /></fill></fills>');
|
|
|
//3. push borders
|
|
@@ -160,10 +161,13 @@ function writeStyles(stylesObj){
|
|
|
};
|
|
|
for (var i = 0; i < stylesObj.borders.length; i++) {
|
|
|
var border = stylesObj.borders[i];
|
|
|
+ rst.push('<border>');
|
|
|
private_setBorder(border, JV.PROP_LEFT);
|
|
|
private_setBorder(border, JV.PROP_RIGHT);
|
|
|
private_setBorder(border, JV.PROP_TOP);
|
|
|
private_setBorder(border, JV.PROP_BOTTOM);
|
|
|
+ rst.push('<diagonal />');
|
|
|
+ rst.push('</border>');
|
|
|
}
|
|
|
rst.push('</borders>');
|
|
|
//4. push cellStyleXfs
|
|
@@ -206,6 +210,18 @@ function writeSharedString(sharedStrList){
|
|
|
}
|
|
|
function writeSheets(pageData, sharedStrList, stylesObj){
|
|
|
var rst = [];
|
|
|
+ private_pushDftFont = function(){
|
|
|
+ var font = {};
|
|
|
+ if (!(stylesObj.fonts)) {
|
|
|
+ stylesObj.fonts = [];
|
|
|
+ }
|
|
|
+ font[JV.FONT_PROPS[0]] = "宋体"; //font name
|
|
|
+ font.size = 11;
|
|
|
+ font.charset = 134;
|
|
|
+ font.colorIdx = "8";
|
|
|
+ stylesObj.fonts.push(font);
|
|
|
+ };
|
|
|
+ private_pushDftFont();
|
|
|
for (var i = 0; i < pageData.items.length; i++) {
|
|
|
rst.push(writeSheet(pageData, pageData.items[i], sharedStrList, stylesObj));
|
|
|
}
|
|
@@ -453,11 +469,7 @@ function writeSheet(pageData, sheetData, sharedStrList, stylesObj){
|
|
|
hasMoreCols = true, nextColIdx = -1,
|
|
|
nextRowIdx = yPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_TOP]);
|
|
|
for (var i = 1; i < yPos.length - 1; i++) {
|
|
|
- if (i === 0) {
|
|
|
- h = 1.0 * (yPos[i]) / DPI * 25.4 / 0.3612;
|
|
|
- } else {
|
|
|
- h = 1.0 * (yPos[i] - yPos[i - 1]) / DPI * 25.4 / 0.3612;
|
|
|
- }
|
|
|
+ h = 1.0 * (yPos[i+1] - yPos[i]) / DPI * 25.4 / 0.3612;
|
|
|
h = Math.round(h * 1000) / 1000;
|
|
|
rst.push('<row r="' + i + '" spans="1:' + spanX + '" ht="' + h + '" customHeight="1">');
|
|
|
//then put the cells of this row
|
|
@@ -475,8 +487,8 @@ function writeSheet(pageData, sheetData, sharedStrList, stylesObj){
|
|
|
hasMoreCols = false;
|
|
|
}
|
|
|
nextColIdx = xPos.indexOf(sheetData.cells[cellIdx][JV.PROP_AREA][JV.PROP_LEFT]);
|
|
|
- for (var j = 0; j < xPos.length; j++) {
|
|
|
- colIdxStr = private_getCellIdxStr(j);
|
|
|
+ for (var j = 1; j < xPos.length; j++) {
|
|
|
+ colIdxStr = private_getCellIdxStr(j - 1);
|
|
|
if (hasMoreCols) {
|
|
|
if (nextColIdx == j) {
|
|
|
var styleIdx = private_getStyleId(sheetData.cells[cellIdx]);
|
|
@@ -485,7 +497,7 @@ function writeSheet(pageData, sheetData, sharedStrList, stylesObj){
|
|
|
//should setup the right style instead!
|
|
|
} else {
|
|
|
var valIdx = private_getSharedStrIdx(sheetData.cells[cellIdx][JV.PROP_VALUE]);
|
|
|
- rst.push('<c r="' + colIdxStr + i + '" s="' + styleIdx + '">');
|
|
|
+ rst.push('<c r="' + colIdxStr + i + '" s="' + styleIdx + '" t="s">');
|
|
|
rst.push('<v>' + valIdx + '</v>');
|
|
|
rst.push('</c>');
|
|
|
}
|
|
@@ -540,6 +552,66 @@ function writeSheet(pageData, sheetData, sharedStrList, stylesObj){
|
|
|
module.exports = {
|
|
|
exportExcel: function (pageData, options) {
|
|
|
var rptOptions = (options || {singlePage: false, fileName: 'report'});
|
|
|
+ var sheets = [];
|
|
|
+ for (var i = 0; i < pageData.items.length; i++) {
|
|
|
+ sheets.push({sheetName: '第' + (i + 1) + '页'});
|
|
|
+ }
|
|
|
+ //1.
|
|
|
+ var file = '[Content_Types].xml';
|
|
|
+ var data = writeContentTypes(sheets);
|
|
|
+ var zip = new JSZip();
|
|
|
+ zip.file(file, data.join(''), {compression: 'DEFLATE'});
|
|
|
+ //2.
|
|
|
+ var zip_rels = zip.folder('_rels');
|
|
|
+ file = '.rels';
|
|
|
+ data = writeRootRels();
|
|
|
+ zip_rels.file(file, data.join(''), {compression: 'DEFLATE'});
|
|
|
+ //3.
|
|
|
+ var zip_docProps = zip.folder('docProps');
|
|
|
+ file = 'app.xml';
|
|
|
+ data = writeApp(sheets);
|
|
|
+ zip_docProps.file(file, data.join(''), {compression: 'DEFLATE'});
|
|
|
+ file = 'core.xml';
|
|
|
+ data = writeCore();
|
|
|
+ zip_docProps.file(file, data.join(''), {compression: 'DEFLATE'});
|
|
|
+ //4.
|
|
|
+ var zip_xl = zip.folder('xl');
|
|
|
+ file = 'workbook.xml';
|
|
|
+ data = writeXlWorkBook(sheets);
|
|
|
+ zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
|
|
|
+ var zip_rels2 = zip_xl.folder('_rels');
|
|
|
+ file = 'workbook.xml.rels';
|
|
|
+ data = writeXlRels(sheets);
|
|
|
+ zip_rels2.file(file, data.join(''), {compression: 'DEFLATE'});
|
|
|
+ //5.
|
|
|
+ var zip_theme = zip_xl.folder('theme');
|
|
|
+ file = 'theme1.xml';
|
|
|
+ data = writeTheme();
|
|
|
+ zip_theme.file(file, data, {compression: 'DEFLATE'});
|
|
|
+ //6.
|
|
|
+ var zip_worksheets = zip_xl.folder('worksheets');
|
|
|
+ var sharedStrList = [], stylesObj = {};
|
|
|
+ data = writeSheets(pageData, sharedStrList, stylesObj);
|
|
|
+ for (var i = 0; i < data.length; i++) {
|
|
|
+ file = 'sheet' + (i + 1) + '.xml';
|
|
|
+ zip_worksheets.file(file, data[i].join(''), {compression: 'DEFLATE'});
|
|
|
+ }
|
|
|
+ file = 'sharedStrings.xml';
|
|
|
+ data = writeSharedString(sharedStrList);
|
|
|
+ zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
|
|
|
+ file = 'styles.xml';
|
|
|
+ data = writeStyles(stylesObj);
|
|
|
+ zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
|
|
|
+
|
|
|
+ zip.generateNodeStream({type:'nodebuffer',streamFiles:true})
|
|
|
+ //.pipe(fs.createWriteStream('../../../tmp/outExcel.xlsx'))
|
|
|
+ .pipe(fs.createWriteStream('../../../tmp/outExcel.zip'))
|
|
|
+ .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("outExcel.xlsx was written.");
|
|
|
+ }
|
|
|
+ );
|
|
|
}
|
|
|
,testWriteContentTypes: function(sheets) {
|
|
|
return writeContentTypes(sheets);
|