您可以使用工作表的 backgroundImage 方法为工作表设置背景图片。
window.onload = function() {
var spread = new GC.Spread.Sheets.Workbook(_getElementById('ss'), { sheetCount: 1 });
initSpread(spread);
};
function initSpread(spread) {
var pictureUrl = '';
spread.suspendPaint();
spread.options.backColor = 'white';
spread.options.grayAreaBackColor = 'gray';
spread.resumePaint();
_getElementById('file_input').addEventListener('change', function() {
if (typeof this.files[0] !== 'object') {
return;
}
var file = this.files[0];
if (!/image\/\w+/.test(file.type)) {
alert('The file must be an image!');
return false;
}
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function(e) {
pictureUrl = this.result;
};
});
_getElementById('setSpreadBackgroundImage').addEventListener('click', function() {
const sheet = spread.getActiveSheet();
sheet.backgroundImage(pictureUrl);
});
_getElementById('delSpreadBackgroundImage').addEventListener('click', function() {
const sheet = spread.getActiveSheet();
sheet.backgroundImage('');
});
}
function _getElementById(id){
return document.getElementById(id);
}
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta name="spreadjs culture" content="zh-cn" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-resources-zh/dist/gc.spread.sheets.resources.zh.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script>
<script src="app.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="sample-tutorial">
<div id="ss" class="sample-spreadsheets"></div>
<div class="options-container">
<div class="option-row">
<label >Set Background Image:</label>
<input type="file" name="image" id="file_input" />
</div>
<br>
<div class="option-row">
<input type="button" id="setSpreadBackgroundImage" value="Set" class="narrow-button" />
<input type="button" id="delSpreadBackgroundImage" value="Delete Background Image" />
</div>
<hr />
<label class="note"><u>Note</u>: remove background image to see this change.</label>
</div>
</div>
</body>
</html>
input[type="button"] {
width: 180px;
}
input[type="text"] {
padding: 4px;
margin-top: 4px;
width: 100%;
box-sizing: border-box;
}
label {
display: inline-block;
margin-bottom: 6px;
}
.note{
margin-top: 0px;
}
.colorLabel {
background-color: #F4F8EB;
}
.wide-label {
width: 260px;
}
input.narrow-button, .narrow-label {
width: 74px;
}
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
.sample-spreadsheets {
width: calc(100% - 300px);
height: 100%;
overflow: hidden;
float: left;
}
.options-container {
float: right;
width: 300px;
padding: 12px;
height: 100%;
box-sizing: border-box;
background: #fbfbfb;
overflow: auto;
}
.option-row:nth-child(1){
padding-bottom: 0px;
}
.option-row:nth-child(2){
margin-top: 0px;
padding-top: 0px;
}
.option-row:nth-child(3){
padding-bottom: 0px;
}
.option-row:nth-child(4){
margin-top: 0px;
padding-top: 0px;
}
.option-row {
font-size: 14px;
padding: 5px;
margin-top: 10px;
}
input {
padding: 4px 6px;
}
input[type=button] {
margin-top: 6px;
display: block;
}
hr {
border-color: #fff;
opacity: .2;
margin-top: 20px;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}