SpreadJS 15
Features / TableSheet / TableSheet IO
In This Topic
    TableSheet IO
    In This Topic

    You can export a TableSheet in various different formats such as JSON, Excel, PDF, and even print it just like printing a spreadsheet.

    JSON Serialization

    TableSheet JSON serialization and deserialization are done using the workbook's toJSON and fromJSON methods. The JSON file includes information such as options and actionColumn and excludes dataView and dataView columns.

    It is recommended to use setDataView method after fromJSON.

    JavaScript
    Copy Code
    function saveJSON() {
        // Save JSON
        var json = spread.toJSON({ includeBindingSource: true, saveAsView: true });
        saveAs(new Blob([JSON.stringify(json)], { type: "text/plain;charset=utf-8" }), 'exportedJSON.ssjson');
    }
    
    function openJSON() {
        // Load JSON
        var file = document.getElementById("fileDemo").files[0];
        if (file) {
            var reader = new FileReader();
            reader.onload = function () {
                var json = JSON.parse(this.result);
                spread.fromJSON(json);
            };
            reader.readAsText(file);
        }
    
    }
    

    Export Excel Files

    Exporting TableSheet to Excel is done using the GC.Spread.Excel.IO save method. TableSheet is treated as a table in a worksheet and includes alternatingRowStyles and conditional format results when saveAsView is true and excludes icon set, data bar, and data validation.

    It is recommended to set includeBindingSource to true when exporting to Excel. It is optional to set saveAsView to true when exporting to Excel.

    JavaScript
    Copy Code
    function saveExcel() {
        // Save Excel
        var excelIo = new GC.Spread.Excel.IO();
        var json = spread.toJSON({ includeBindingSource: true, saveAsView: true });
        excelIo.save(json, function (blob) {
            saveAs(blob, "exportedExcel.xlsx");
        }, function (e) {
            console.log(e);
        });
    }
    

    Export PDF

    Exporting TableSheet to PDF is done using the workbook's savePDF method. PDF includes binding results and excludes icons, options, and actionColumn.

    For printing or exporting to PDF, users can customize the print information using the printInfo method.

    JavaScript
    Copy Code
    function savePDF() {
        // Save PDF
        spread.savePDF(function (blob) {
            saveAs(blob, "exportedPDF.pdf");
        }, function (error) {
            console.log(error);
        }, null, spread.getSheetCount() + spread.getActiveSheetTabIndex());
    }
    

    Print Tablesheet

    TableSheet printing is done using the workbook's print method. Print includes binding results and excludes icons, options, and actionColumn.

    JavaScript
    Copy Code
    function print() {
        // Print Spread
        spread.print();
    }