새 창에서 데모를 열어 디자이너 열기
보고서 내보내기 및 가져오기
SpreadJS에서는 ReportSheet를 Excel, JSON 및 SJS 형식 등의 외부 파일로 내보낼 수 있습니다. 해당 파일 형식의 외부 파일을 가져올 수도 있습니다.
window.onload = async () => {
const loadingTip = addLoadingTip();
const { spread, designer } = createSpreadAndDesigner();
const res = await fetch('$DEMOROOT$/ko/sample/features/report-sheet/export-and-import/spread.json');
await spread.fromJSON(await res.json());
bindEvents(spread);
loadingTip.remove();
if (designer) {
designer.refresh();
}
}
function bindEvents(spread) {
const excelIo = new GC.Spread.Excel.IO();
document.getElementById('import-json').addEventListener('click', (e) => {
const input = document.createElement('input');
input.type = 'file';
input.accept = '.ssjson,.json';
input.onchange = (ei) => {
const file = ei.target.files[0];
if (file) {
if (!file.type || file.type === 'application/json') {
const reader = new FileReader();
reader.readAsText(file, "UTF-8");
reader.onload = function (evt) {
const fileString = evt.target.result;
const jsonObj = JSON.parse(fileString);
spread.fromJSON(jsonObj);
};
}
}
};
input.click();
});
document.getElementById('import-sjs').addEventListener('click', (e) => {
const input = document.createElement('input');
input.type = 'file';
input.accept = '.sjs';
input.onchange = (ei) => {
const file = ei.target.files[0];
if (file) {
spread.open(file, function (json) { }, function (event) {
console.log(event);
});
}
};
input.click();
});
document.getElementById('import-excel').addEventListener('click', (e) => {
const input = document.createElement('input');
input.type = 'file';
input.accept = '.xlsx';
input.onchange = (ei) => {
const file = ei.target.files[0];
if (file) {
excelIo.open(file, function (json) {
spread.fromJSON(json);
}, function (event) {
console.log(event);
});
}
};
input.click();
});
document.getElementById('export-json').addEventListener('click', function (e) {
let json = spread.toJSON();
let content = JSON.stringify(json);
let blob = new Blob([content], { type: "application/json;charset=utf-8" });
let fileNameNode = document.getElementById('json-name');
saveAs(blob, (fileNameNode.value || 'report') + ".json");
});
document.getElementById('export-sjs').addEventListener('click', (e) => {
let fileNameNode = document.getElementById('sjs-name');
spread.save(
(blob) => {
saveAs(blob, (fileNameNode.value || 'report') + ".sjs");
},
null
);
});
document.getElementById('export-excel').addEventListener('click', (e) => {
let fileNameNode = document.getElementById('excel-name');
spread.export(
(blob) => {
saveAs(blob, (fileNameNode.value || 'report') + ".xlsx");
},
null,
{
fileType: GC.Spread.Sheets.FileType.excel,
}
);
});
}
function createSpreadAndDesigner() {
const demoHost = document.getElementById('demo-host');
if (window !== top) {
return {
spread: new GC.Spread.Sheets.Workbook(demoHost, { sheetCount: 1 }),
}
} else {
document.getElementById("demo-host").style.width = "100%";
const designer = new GC.Spread.Sheets.Designer.Designer(demoHost, undefined, undefined, { sheetCount: 1 });
return {
designer,
spread: designer.getWorkbook(),
}
}
}
function addLoadingTip() {
const div = document.createElement('div');
div.style.position = 'absolute';
div.style.inset = '0';
div.style.display = 'flex';
div.style.alignItems = 'center';
div.style.justifyContent = 'center';
div.style.background = 'white';
div.style.zIndex = '100';
div.textContent = 'Loading data from server ...';
document.body.appendChild(div);
return div;
}
<!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$/ko/purejs/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<link rel="stylesheet" type="text/css"
href="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-designer/styles/gc.spread.sheets.designer.min.css">
<link rel="stylesheet" type="text/css" href="styles.css">
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets/dist/gc.spread.sheets.all.min.js"
type="text/javascript"></script>
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-print/dist/gc.spread.sheets.print.min.js"
type="text/javascript"></script>
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-shapes/dist/gc.spread.sheets.shapes.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-charts/dist/gc.spread.sheets.charts.min.js" type="text/javascript"></script>
<script
src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-reportsheet-addon/dist/gc.spread.report.reportsheet.min.js"
type="text/javascript"></script>
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-excelio/dist/gc.spread.excelio.min.js"
type="text/javascript"></script>
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-io/dist/gc.spread.sheets.io.min.js"
type="text/javascript"></script>
<script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script>
<script src="$DEMOROOT$/spread/source/js/FileSaver.js" type="text/javascript"></script>
<script>
const designerDependencyScripts = [
'$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-barcode/dist/gc.spread.sheets.barcode.min.js',
'$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-pdf/dist/gc.spread.sheets.pdf.min.js',
'$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-pivot-addon/dist/gc.spread.pivot.pivottables.min.js',
'$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-slicers/dist/gc.spread.sheets.slicers.min.js',
'$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-tablesheet/dist/gc.spread.sheets.tablesheet.min.js',
'$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-ganttsheet/dist/gc.spread.sheets.ganttsheet.min.js',
'$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-formula-panel/dist/gc.spread.sheets.formulapanel.min.js',
'$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-io/dist/gc.spread.sheets.io.min.js',
'$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-excelio/dist/gc.spread.excelio.min.js',
'$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-designer-resources-ko/dist/gc.spread.sheets.designer.resource.ko.min.js',
'$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-designer/dist/gc.spread.sheets.designer.all.min.js',
'$DEMOROOT$/spread/source/js/designer/license.js',
]
function appendScriptNode(src) {
const script = document.createElement('script');
script.src = src;
script.async = false;
script.type = 'text/javascript';
document.head.appendChild(script);
}
if (top === window) { // not in iframe
designerDependencyScripts.forEach(appendScriptNode);
}
</script>
<script src="app.js" type="text/javascript"></script>
</head>
<body>
<div class="sample-tutorial">
<div id="demo-host"></div>
<div id="optionContainer" class="optionContainer">
<div class="option-item">
<label><b>Export And Import</b></label>
</div>
<hr>
<input type="button" value="Import JSON" class="import-button" id="import-json">
<div class="option-item">
<input id="json-name" name="json-name" class="file-name" placeholder="file name">
<input type="button" value="Export JSON" class="export-button" id="export-json">
</div>
<input type="button" value="Import SJS" class="import-button" id="import-sjs">
<div class="option-item">
<input id="sjs-name" name="sjs-name" class="file-name" placeholder="file name">
<input type="button" value="Export SJS" class="export-button" id="export-sjs">
</div>
<input type="button" value="Import Excel" class="import-button" id="import-excel">
<div class="option-item">
<input id="excel-name" name="excel-name" class="file-name" placeholder="file name">
<input type="button" value="Export Excel" class="export-button" id="export-excel">
</div>
</div>
</div>
</body>
</html>
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
#demo-host {
width: 80%;
height: 100%;
overflow: hidden;
float: left;
}
.optionContainer {
width: 20%;
height: 100%;
overflow: hidden;
float: right;
}
.option-item{
height: 20px;
margin-bottom: 30px;
margin-top: 5px;
}
.import-button {
display: block;
margin-top: 20px;
width: 100px;
height: 25px;
}
.file-name {
display: inline-block;
width: 150px;
height: 20px;
}
.export-button {
display: inline-block;
width: 100px;
height: 25px;
}