<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<!-- disable IE compatible view -->
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="spreadjs culture" content="zh-cn" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" , user-scalable=no" />
<title>SpreadJS Demo : 导入导出Excel、导出PDF、打印</title>
<!-- CDN Reference -->
<link rel="icon" type="image/png" href="https://demo.grapecity.com.cn/SpreadJS/TutorialSample/icon.ico" />
<link rel=" stylesheet" type="text/css" href="https://cdn.grapecity.com.cn/spreadjs/css/basscss.min.css" />
<link rel="stylesheet" type="text/css" href="https://cdn.grapecity.com.cn/spreadjs/css/gc.spread.sheets.excel2013white.12.0.6.css" />
<script type="text/javascript" src="https://cdn.grapecity.com.cn/spreadjs/scripts/gc.spread.sheets.all.12.0.6.min.js"></script>
<script type="text/javascript" src="https://cdn.grapecity.com.cn/spreadjs/scripts/resources/zh/gc.spread.sheets.resources.zh.12.0.6.min.js"></script>
<script type="text/javascript" src="https://cdn.grapecity.com.cn/spreadjs/scripts/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="https://demo.grapecity.com.cn/SpreadJS/TutorialSample/js/spread/license.js"></script>
<script type="text/javascript" src="gc.spread.excelio.12.0.0.min.js"></script>
<script type="text/javascript" src="model1.js"></script>
<script type="text/javascript" src="exceldata.js"></script>
<script type="text/javascript" src="gc.spread.sheets.charts.12.0.0.min.js"></script>
<script type="text/javascript" src="File.Saver.js"></script>
<script type="text/javascript" src="gc.spread.sheets.print.12.0.0.min.js"></script>
<script type="text/javascript" src="gc.spread.sheets.pdf.12.0.0.min.js"></script>
<script type="text/javascript" src="fonts.js"></script>
<!-- <script type="text/javascript" src="js/model.js"></script> -->
<!-- <script type="text/javascript" src="data/data.ssjson"></script> -->
<!-- Local Reference
<link rel="icon" type="image/png" href="images/icon.ico" />
<link rel="stylesheet" type="text/css" href="css/basscss.min.css ">
<link rel="stylesheet" type="text/css" href=" css/gc.spread.sheets.excel2013white.12.0.6.css">
<script type="text/javascript" src="scripts/gc.spread.sheets.all.12.0.6.min.js"></script>
<script type="text/javascript" src="/scripts/gc.spread.sheets.resources.zh.12.0.6.min.js"></script>
<script type="text/javascript" src="scripts/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="https://demo.grapecity.com.cn/SpreadJS/TutorialSample/js/spread/license.js"></script>
-->
<!-- 页面内自定义 CSS-->
<style>
body {
background: rgb(250, 250, 250);
color: #333;
font-family: mtcorsva !important;
}
#ss {
border: 1px #ccc solid;
}
.container {
width: 95%;
background: rgb(250, 250, 250);
margin: 0 auto;
height: 600px;
}
.full-height {
height: 100%;
}
.left{
height: 100%;
overflow: auto;
}
</style>
</head>
<body>
<div class="container">
<div class="clearfix border-bottom">
<div class="col col-8">
<h3>SpreadJS Demo : 导入导出Excel、导出PDF、打印</h3>
</div>
<div class="col col-4 right-align align-middle">
<h5>
<ul class="list-reset">
<li class="inline-block mr1"><a class="btn" href="https://www.grapecity.com.cn/developer/spreadjs "
target="_blank ">产品官网</a></li>
<li class="inline-block mr1"><a class="" href="https://gcdn.grapecity.com.cn/showforum-185-1.html "
target="_blank ">论坛求助</a></li>
<li class="inline-block mr1"><a class="" href="https://demo.grapecity.com.cn/SpreadJS/TutorialSample/#/samples "
target="_blank ">更多示例</a></li>
</ul>
</h5>
</div>
</div>
<div class="full-height clearfix mt2">
<div class="col col-3 left">
<div class="inputContainer">
<input style='width: 173px;' type="file" id="fileDemo" class="input">
<input type="button" id="loadExcel" value="导入" class="button">
</div>
<div class="inputContainer">
<input id="exportFileName" value="export.xlsx" class="input">
<input type="button" id="saveExcel" value="导出" class="button">
</div>
<label>密码:
<input type="password" id="password">
</label>
<div>
<input type="button" id="pdf" value="导出PDF" class="button">
</div>
<div>
<input type="button" id="print" value="打印" class="button">
</div>
</div>
<div class="col col-9 full-height ">
<div id="ss" style="height:680px; font-family:simkai"></div>
<div id="ss1" style="display: none;"></div>
</div>
</div>
</div>
</body>
<script type="text/javascript ">
var spreadNS = GC.Spread.Sheets;
$(document).ready(function () {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
var sheet = spread.getActiveSheet();
// spread.fromJSON(model1);
var excelIo = new GC.Spread.Excel.IO();
/*-----------------导入Excel--------------------------*/
document.getElementById('loadExcel').onclick = function(){
var excelFile = document.getElementById('fileDemo').files[0];
var password = document.getElementById('password').value;
excelIo.open(excelFile,function(json){
var obj = json;
spread.fromJSON(obj);
},{password: password});
};
/*-------------------------导出Excel---------------------------------*/
document.getElementById('saveExcel').onclick = function() {
var fileName = document.getElementById('exportFileName').value;
var password = document.getElementById('password').value;
if (fileName.substr(-5, 5) !== '.xlsx') {
fileName += '.xlsx';
}
var json = spread.toJSON();
excelIo.save(json, function (blob) {
saveAs(blob, fileName);
}, function (e) {
console.log(e);
}, {password: password});
}
/*----------------------------- 导出PDF-------------------------------------*/
var fontsObj = fonts;
// 注册一个自定义字体
registerCustomFont(fontsObj);
setPrintInfo(spread);
var style = new GC.Spread.Sheets.Style();
style.font = "8pt simkai";
for (var i = 0; i < spread.getSheetCount(); i++) {
spread.sheets[i].setDefaultStyle(style,GC.Spread.Sheets.SheetArea.viewport);
}
function registerCustomFont(fontsObj) {
var fonts = {
normal: fontsObj["simkai.ttf"]
};
GC.Spread.Sheets.PDF.PDFFontsManager.registerFont('simkai', fonts);
}
document.getElementById('pdf').onclick = function(){
spread.savePDF(function(blob){
saveAs(blob,'download.pdf');
},
function (error) {
console.log(error);
},
{
title:'Test Title', // 导出PDF文件的参数
author:'Test Author',
subject:'TestSubject',
keywords:'Text Keywords',
creator:'test Creator'
});
}
function setPrintInfo(spread) {
var sheet = spread.getActiveSheet();
var printInfo = sheet.printInfo();
printInfo.showBorder(false);
printInfo.showGridLine(false);
printInfo.showColumnHeader(GC.Spread.Sheets.Print.PrintVisibilityType.hide);
printInfo.showRowHeader(GC.Spread.Sheets.Print.PrintVisibilityType.hide);
sheet.printInfo(printInfo);
}
/*--------------------------打印-------------------------*/
document.getElementById('print').onclick = function(sheetsindex) {
// spread.print(3);
printSetting(spread);
}
//打印设置
function printSetting( ) {
var sheet = spread.getActiveSheet();
var printInfo = sheet.printInfo();
//打印设置
printInfo.paperSize(new GC.Spread.Sheets.Print.PaperSize(GC.Spread.Sheets.Print.PaperKind.a4));
printInfo.margin({ top: 0, bottom: 0, left: 0, right: 0, header: 0, footer: 0 });
//是否打印控件的外边框
printInfo.showBorder(false);
//是否打印网格线(默认打印)
printInfo.showGridLine(false);
printInfo.zoomFactor(0.8);//缩放
printInfo.qualityFactor(1);//打印的质量因子
printInfo.orientation(GC.Spread.Sheets.Print.PrintPageOrientation.portrait);//打印的页面方向。portrait landscape
printInfo.centering(GC.Spread.Sheets.Print.PrintCentering.horizontal);//打印页面的居中方式。
printInfo.showColumnHeader(GC.Spread.Sheets.Print.PrintVisibilityType.hide);//获取或设置是否打印列标题。
printInfo.showRowHeader(GC.Spread.Sheets.Print.PrintVisibilityType.hide);
//设置是否以黑白打印
printInfo.blackAndWhite(true);
for (var i = 0; i < spread.getSheetCount(); i++) {
spread.sheets[i].printInfo(printInfo);
}
spread.print(2);
}
});
</script>
</html>