如果你需要使用打印功能,你需要添加打印的 JS 文件引用到页面的头部,并且放置在Spread Sheets的引用之后。
例如:
然后你就可以通过调用 Spread 控件的 print 方法来使用打印功能。
它被定义为:
spread.print(sheetIndex?)
其中,可选参数 sheetIndex 指明哪一个表单需要打印 (缺省为打印整个表单)。
例如,你可以使用如下方法打印整个表单:
import { Component, NgModule, enableProdMode } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import '@grapecity-software/spread-sheets-resources-zh';
GC.Spread.Common.CultureManager.culture("zh-cn");
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { SpreadSheetsModule } from '@grapecity-software/spread-sheets-angular';
import '@grapecity-software/spread-sheets-print';
import GC from '@grapecity-software/spread-sheets';
import './styles.css';
@Component({
selector: 'app-component',
templateUrl: 'src/app.component.html'
})
export class AppComponent {
spread: GC.Spread.Sheets.Workbook;
hostStyle = {
width: 'calc(100% - 280px)',
height: '100%',
overflow: 'hidden',
float: 'left'
};
initSpread($event: any) {
this.spread = $event.spread;
let spread = this.spread;
let sheet = spread.getActiveSheet();
sheet.suspendPaint();
sheet.options.allowCellOverflow = true;
sheet.name("Demo");
sheet.addSpan(1, 1, 1, 1);
sheet.setValue(1, 1, "Month");
sheet.setValue(2, 1, "January");
sheet.setValue(3, 1, "February");
sheet.setValue(4, 1, "March");
sheet.setValue(5, 1, "April");
sheet.setValue(6, 1, "May");
sheet.setValue(7, 1, "June");
sheet.setValue(8, 1, "July");
sheet.setValue(9, 1, "August");
sheet.setValue(10, 1, "September");
sheet.setValue(11, 1, "October");
sheet.setValue(12, 1, "November");
sheet.setValue(13, 1, "December");
sheet.addSpan(1, 2, 1, 1);
sheet.setValue(1, 2, "Quantity");
sheet.setValue(2, 2, 60);
sheet.setValue(3, 2, 100);
sheet.setValue(4, 2, 110);
sheet.setValue(5, 2, 90);
sheet.setValue(6, 2, 112);
sheet.setValue(7, 2, 137);
sheet.setValue(8, 2, 80);
sheet.setValue(9, 2, 88);
sheet.setValue(10, 2, 118);
sheet.setValue(11, 2, 122);
sheet.setValue(12, 2, 130);
sheet.setValue(13, 2, 135);
sheet.addSpan(1, 3, 1, 1);
sheet.setValue(1, 3, "Price");
sheet.setValue(2, 3, 30.0);
sheet.setValue(3, 3, 45.0);
sheet.setValue(4, 3, 10.0);
sheet.setValue(5, 3, 99.9);
sheet.setValue(6, 3, 89.0);
sheet.setValue(7, 3, 150.0);
sheet.setValue(8, 3, 35.0);
sheet.setValue(9, 3, 59.9);
sheet.setValue(10, 3, 47.9);
sheet.setValue(11, 3, 55.0);
sheet.setValue(12, 3, 32.0);
sheet.setValue(13, 3, 20.0);
sheet.addSpan(1, 4, 1, 1);
sheet.setValue(1, 4, "Total");
sheet.setFormula(2, 4, "=C3*D3");
sheet.setFormula(3, 4, "=C4*D4");
sheet.setFormula(4, 4, "=C5*D5");
sheet.setFormula(5, 4, "=C6*D6");
sheet.setFormula(6, 4, "=C7*D7");
sheet.setFormula(7, 4, "=C8*D8");
sheet.setFormula(8, 4, "=C9*D9");
sheet.setFormula(9, 4, "=C10*D10");
sheet.setFormula(10, 4, "=C11*D11");
sheet.setFormula(11, 4, "=C12*D12");
sheet.setFormula(12, 4, "=C13*D13");
sheet.setFormula(13, 4, "=C14*D14");
// set column width
sheet.setColumnWidth(0, 50);
sheet.setColumnWidth(1, 100);
sheet.setColumnWidth(2, 60);
sheet.setColumnWidth(3, 60);
sheet.setColumnWidth(4, 90);
sheet.setColumnWidth(5, 50);
//set alignment
sheet.getRange(1, 1, 1, 4).hAlign(GC.Spread.Sheets.HorizontalAlign.center)
//set formatter
sheet.getRange(2, 3, 12, 1).formatter("$ #,##0");
sheet.getRange(2, 4, 12, 1).formatter("$ #,##0");
//set color
sheet.getRange(1, 1, 1, 4).backColor("#69a569").foreColor("white");
//set lineBorder
sheet.getRange(2, 1, 12, 4).setBorder(new GC.Spread.Sheets.LineBorder("#A8A9AD", GC.Spread.Sheets.LineStyle.dotted), { all: true });
this.addFigures(sheet);
sheet.resumePaint();
}
handelPrint() {
// used to adjust print range, should set with printInfo (refer custom print for detail)
this.spread.sheets[0].setText(31, 8, " ");
this.spread.print();
}
addFigures(sheet: GC.Spread.Sheets.Worksheet) {
sheet.getRange(20, -1, 1, -1).visible(false);
sheet.addSpan(4, 6, 6, 3);
sheet.setFormula(4, 6, '=PIESPARKLINE(E3:E7, "#e6f0e6","#c9dec9","#a3c8a3","#73ab73","#d1ffd1")');
sheet.addSpan(10, 6, 1, 3);
sheet.getCell(10, 6).text("Figure 1").hAlign(1);
}
}
@NgModule({
imports: [BrowserModule, SpreadSheetsModule],
declarations: [AppComponent],
exports: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
enableProdMode();
// Bootstrap application with hash style navigation and global services.
platformBrowserDynamic().bootstrapModule(AppModule);
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css"
href="$DEMOROOT$/zh/angular/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<!-- Polyfills -->
<script src="$DEMOROOT$/zh/angular/node_modules/core-js/client/shim.min.js"></script>
<script src="$DEMOROOT$/zh/angular/node_modules/zone.js/fesm2015/zone.min.js"></script>
<!-- SystemJS -->
<script src="$DEMOROOT$/zh/angular/node_modules/systemjs/dist/system.js"></script>
<script src="systemjs.config.js"></script>
<script>
// workaround to load 'rxjs/operators' from the rxjs bundle
System.import('rxjs').then(function (m) {
System.import('@angular/compiler');
System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators));
System.import('$DEMOROOT$/zh/lib/angular/license.ts');
System.import('./src/app.component');
});
</script>
</head>
<body>
<app-component></app-component>
</body>
</html>
<div class="sample-tutorial">
<gc-spread-sheets [hostStyle]="hostStyle" (workbookInitialized)="initSpread($event)">
</gc-spread-sheets>
<div class="options-container">
<div class="option-row">
<input type="button" value="Print" id="btnPrint" (click) = "handelPrint()" />
</div>
</div>
</div>
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
.sample-spreadsheets {
width: calc(100% - 280px);
height: 100%;
overflow: hidden;
float: left;
}
.options-container {
float: right;
width: 280px;
padding: 12px;
height: 100%;
box-sizing: border-box;
background: #fbfbfb;
overflow: auto;
}
.option-row {
font-size: 14px;
padding: 5px;
margin-top: 10px;
}
input {
padding: 8px 14px;
display: block;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
(function(global) {
System.config({
transpiler: 'ts',
typescriptOptions: {
tsconfig: true
},
meta: {
'typescript': {
"exports": "ts"
},
'*.css': { loader: 'css' }
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
'core-js': 'npm:core-js/client/shim.min.js',
'zone': 'npm:zone.js/fesm2015/zone.min.js',
'rxjs': 'npm:rxjs/dist/bundles/rxjs.umd.min.js',
'@angular/core': 'npm:@angular/core/fesm2022',
'@angular/common': 'npm:@angular/common/fesm2022/common.mjs',
'@angular/compiler': 'npm:@angular/compiler/fesm2022/compiler.mjs',
'@angular/platform-browser': 'npm:@angular/platform-browser/fesm2022/platform-browser.mjs',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/fesm2022/platform-browser-dynamic.mjs',
'@angular/common/http': 'npm:@angular/common/fesm2022/http.mjs',
'@angular/router': 'npm:@angular/router/fesm2022/router.mjs',
'@angular/forms': 'npm:@angular/forms/fesm2022/forms.mjs',
'jszip': 'npm:jszip/dist/jszip.min.js',
'typescript': 'npm:typescript/lib/typescript.js',
'ts': './plugin.js',
'tslib':'npm:tslib/tslib.js',
'css': 'npm:systemjs-plugin-css/css.js',
'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js',
'systemjs-babel-build': 'npm:systemjs-plugin-babel/systemjs-babel-browser.js',
'@grapecity-software/spread-sheets-print': 'npm:@grapecity-software/spread-sheets-print/index.js',
'@grapecity-software/spread-sheets': 'npm:@grapecity-software/spread-sheets/index.js',
'@grapecity-software/spread-sheets-resources-zh': 'npm:@grapecity-software/spread-sheets-resources-zh/index.js',
'@grapecity-software/spread-sheets-angular': 'npm:@grapecity-software/spread-sheets-angular/fesm2020/grapecity-software-spread-sheets-angular.mjs',
'@grapecity-software/jsob-test-dependency-package/react-components': 'npm:@grapecity-software/jsob-test-dependency-package/react-components/index.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
src: {
defaultExtension: 'ts'
},
rxjs: {
defaultExtension: 'js'
},
"node_modules": {
defaultExtension: 'js'
},
"node_modules/@angular": {
defaultExtension: 'mjs'
},
"@grapecity-software/spread-sheets-angular": {
defaultExtension: 'mjs'
},
'@angular/core': {
defaultExtension: 'mjs',
main: 'core.mjs'
}
}
});
})(this);