Open save in Vue Spreadsheet component
29 Mar 202324 minutes to read
The native data format for Spreadsheet is JSON
. When you open an excel file, it needs to be read and converted to client side Spreadsheet model. The converted client side Spreadsheet model is sent as JSON which is used to render Spreadsheet. Similarly, when you save the Spreadsheet, the client Spreadsheet model is sent to the server as JSON for processing and saved as Excel file formats. Server configuration
is used for this process.
Open
The Spreadsheet control opens an Excel document with its data, style, format, and more. To enable this feature, set allowOpen
as true
and assign service url to the openUrl
property.
User Interface:
In user interface you can open an Excel document by clicking File > Open
menu item in ribbon.
The following sample shows the Open
option by using the openUrl
property in the Spreadsheet control. You can also use the beforeOpen
event to trigger before opening an Excel file.
<template>
<ejs-spreadsheet :openUrl="openUrl" :allowOpen="true" :beforeOpen="beforeOpen"></ejs-spreadsheet>
</template>
<script>
import Vue from "vue";
import { SpreadsheetPlugin } from "@syncfusion/ej2-vue-spreadsheet";
Vue.use(SpreadsheetPlugin);
export default {
data: () => {
return {
openUrl: 'https://services.syncfusion.com/vue/production/api/spreadsheet/open',
}
},
methods: {
beforeOpen: function (args) {
// your code snippets here
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-spreadsheet/styles/material.css";
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-dropdowns/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-navigations/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-splitbuttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-grids/styles/material.css';
@import "../node_modules/@syncfusion/ej2-spreadsheet/styles/material.css";
</style>
Please find the below table for the beforeOpen event arguments.
Parameter | Type | Description |
---|---|---|
file | FileList or string or File | To get the file stream. FileList - contains length and item index. File - specifies the file lastModified and file name. |
cancel | boolean | To prevent the open operation. |
requestData | object | To provide the Form data. |
Open an external URL excel file while initial load
You can achieve to access the remote excel file by using the created
event. In this event you can fetch the excel file and convert it to a blob. Convert this blob to a file and open
this file by using Spreadsheet component open method.
<template>
<ejs-spreadsheet ref="spreadsheet" :openUrl="openUrl" :allowOpen="true" :created="created"></ejs-spreadsheet>
</template>
<script>
import Vue from "vue";
import { SpreadsheetPlugin } from "@syncfusion/ej2-vue-spreadsheet";
Vue.use(SpreadsheetPlugin);
export default {
data: () => {
return {
openUrl: 'https://services.syncfusion.com/vue/production/api/spreadsheet/open',
}
},
methods: {
created: function () {
fetch("https://js.syncfusion.com/demos/ejservices/data/Spreadsheet/LargeData.xlsx") // fetch the remote url
.then((response) => {
response.blob().then((fileBlob) => { // convert the excel file to blob
var file = new File([fileBlob], "Sample.xlsx"); //convert the blob into file
this.$refs.spreadsheet.open({ file: file }); // open the file into Spreadsheet
})
})
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-spreadsheet/styles/material.css";
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-dropdowns/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-navigations/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-splitbuttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-grids/styles/material.css';
@import "../node_modules/@syncfusion/ej2-spreadsheet/styles/material.css";
</style>
To add custom header during open
You can add your own custom header to the open action in the Spreadsheet. For processing the data, it has to be sent from server to client side and adding customer header can provide privacy to the data with the help of Authorization Token. Through the beforeOpen
event, the custom header can be added to the request during open action.
<template>
<ejs-spreadsheet
:openUrl="openUrl"
:allowOpen="true"
:beforeOpen="beforeOpen"
></ejs-spreadsheet>
</template>
<script>
import Vue from "vue";
import { SpreadsheetPlugin } from "@syncfusion/ej2-vue-spreadsheet";
Vue.use(SpreadsheetPlugin);
export default {
data: () => {
return {
openUrl:
"https://services.syncfusion.com/vue/production/api/spreadsheet/open",
};
},
methods: {
beforeOpen: function (args) {
args.requestData["headers"] = {
Authorization: "YOUR TEXT",
};
},
},
};
</script>
<style>
@import "https://ej2.syncfusion.com/vue/documentation/node_modules/@syncfusion/ej2-vue-spreadsheet/styles/material.css";
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/material.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-grids/styles/material.css";
@import "../node_modules/@syncfusion/ej2-spreadsheet/styles/material.css";
</style>
Open excel file into a read-only mode
You can open excel file into a read-only mode by using the openComplete
event. In this event, you must protect all the sheets and lock its used range cells by using protectSheet
and lockCells
methods
<template>
<ejs-spreadsheet ref="spreadsheet" :openUrl="openUrl" :allowOpen="true" :openComplete="openComplete"></ejs-spreadsheet>
</template>
<script>
import Vue from "vue";
import { SpreadsheetPlugin, getRangeAddress } from "@syncfusion/ej2-vue-spreadsheet";
Vue.use(SpreadsheetPlugin);
export default {
data: () => {
return {
openUrl: 'https://services.syncfusion.com/vue/production/api/spreadsheet/open',
}
},
methods: {
openComplete: function () {
let spreadsheet = this.$refs.spreadsheet.ej2Instances;
let sheets = spreadsheet.sheets;
for (let index = 0; index < sheets.length; index++) {
let name = spreadsheet.sheets[index].name;
let protectSetting = {
selectCells: true,
formatCells: false,
};
//To protect the sheet using sheet name
spreadsheet.protectSheet(name, protectSetting);
let address = getRangeAddress([
0,
0,
sheets[index].usedRange.rowIndex,
sheets[index].usedRange.colIndex,
]);
//To lock the used range cells
spreadsheet.lockCells(name + '!' + address, true);
}
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-spreadsheet/styles/material.css";
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-dropdowns/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-navigations/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-splitbuttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-grids/styles/material.css';
@import "../node_modules/@syncfusion/ej2-spreadsheet/styles/material.css";
</style>
Supported file formats
The following list of Excel file formats are supported in Spreadsheet:
- MS Excel (.xlsx)
- MS Excel 97-2003 (.xls)
- Comma Separated Values (.csv)
- Excel Macro-Enabled Workbook (.xlsm)
- Excel Binary Workbook(.xlsb)
Save
The Spreadsheet control saves its data, style, format, and more as Excel file document. To enable this feature, set allowSave
as true
and assign service url to the saveUrl
property.
User Interface:
In user interface, you can save Spreadsheet data as Excel document by clicking File > Save As
menu item in ribbon.
The following sample shows the Save
option by using the saveUrl
property in the Spreadsheet control. You can also use the beforeSave
event to trigger before saving the Spreadsheet as an Excel file.
<template>
<ejs-spreadsheet :saveUrl="saveUrl" :allowSave="true" :beforeSave="beforeSave">
<e-sheets>
<e-sheet>
<e-ranges>
<e-range :dataSource="dataSource"></e-range>
</e-ranges>
<e-columns>
<e-column :width="width1"></e-column>
<e-column :width="width2"></e-column>
<e-column :width="width2"></e-column>
<e-column :width="width1"></e-column>
<e-column :width="width2"></e-column>
<e-column :width="width3"></e-column>
</e-columns>
</e-sheet>
</e-sheets></ejs-spreadsheet>
</template>
<script>
import Vue from "vue";
import { SpreadsheetPlugin } from "@syncfusion/ej2-vue-spreadsheet";
import { data } from './data.js';
Vue.use(SpreadsheetPlugin);
export default {
data: () => {
return {
dataSource: data,
width1: 180,
width2: 130,
width3: 120,
saveUrl: 'https://services.syncfusion.com/vue/production/api/spreadsheet/save'
}
},
methods: {
beforeSave: function (args) {
// your code snippets here
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-spreadsheet/styles/material.css";
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-dropdowns/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-navigations/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-splitbuttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-grids/styles/material.css';
@import "../node_modules/@syncfusion/ej2-spreadsheet/styles/material.css";
</style>
Please find the below table for the beforeSave event arguments.
Parameter | Type | Description |
---|---|---|
url | string | Specifies the save url. |
fileName | string | Specifies the file name. |
saveType | SaveType | Specifies the saveType like Xlsx, Xls, Csv and Pdf. |
customParams | object | Passing the custom parameters from client to server while performing save operation. |
isFullPost | boolean | It sends the form data from client to server, when set to true. It fetches the data from client to server and returns the data from server to client, when set to false. |
needBlobData | boolean | You can get the blob data if set to true. |
cancel | boolean | To prevent the save operations. |
To send and receive custom params from client to server
Passing the custom parameters from client to server by using beforeSave
event.
<template>
<ejs-spreadsheet :saveUrl="saveUrl" :allowSave="true" :beforeSave="beforeSave">
<e-sheets>
<e-sheet>
<e-ranges>
<e-range :dataSource="dataSource"></e-range>
</e-ranges>
<e-columns>
<e-column :width="width1"></e-column>
<e-column :width="width2"></e-column>
<e-column :width="width2"></e-column>
<e-column :width="width1"></e-column>
<e-column :width="width2"></e-column>
<e-column :width="width3"></e-column>
</e-columns>
</e-sheet>
</e-sheets></ejs-spreadsheet>
</template>
<script>
import Vue from "vue";
import { SpreadsheetPlugin } from "@syncfusion/ej2-vue-spreadsheet";
import { data } from './data.js';
Vue.use(SpreadsheetPlugin);
export default {
data: () => {
return {
dataSource: data,
width1: 180,
width2: 130,
width3: 120,
saveUrl: 'https://services.syncfusion.com/vue/production/api/spreadsheet/save'
}
},
methods: {
beforeSave: function (args) {
args.customParams = { customParams: 'you can pass custom params in server side'}; // you can pass the custom params
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-spreadsheet/styles/material.css";
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-dropdowns/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-navigations/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-splitbuttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-grids/styles/material.css';
@import "../node_modules/@syncfusion/ej2-spreadsheet/styles/material.css";
</style>
Server side code snippets:
public IActionResult Save(SaveSettings saveSettings, string customParams)
{
Console.WriteLine(customParams); // you can get the custom params in controller side
return Workbook.Save(saveSettings);
}
To add custom header during save
You can add your own custom header to the save action in the Spreadsheet. For processing the data, it has to be sent from client to server side and adding customer header can provide privacy to the data with the help of Authorization Token. Through the fileMenuItemSelect
event, the custom header can be added to the request during save action.
<template>
<ejs-spreadsheet
ref="spreadsheet"
:saveUrl="saveUrl"
:allowSave="true"
:fileMenuItemSelect="fileMenuItemSelect"
>
<e-sheets>
<e-sheet>
<e-ranges>
<e-range :dataSource="dataSource"></e-range>
</e-ranges>
<e-columns>
<e-column :width="width1"></e-column>
<e-column :width="width2"></e-column>
<e-column :width="width2"></e-column>
<e-column :width="width1"></e-column>
<e-column :width="width2"></e-column>
<e-column :width="width3"></e-column>
</e-columns>
</e-sheet> </e-sheets
></ejs-spreadsheet>
</template>
<script>
import Vue from "vue";
import { SpreadsheetPlugin } from "@syncfusion/ej2-vue-spreadsheet";
import { createElement } from "@syncfusion/ej2-base";
import { data } from "./data.js";
Vue.use(SpreadsheetPlugin);
export default {
data: () => {
return {
dataSource: data,
width1: 180,
width2: 130,
width3: 120,
saveUrl:
"https://services.syncfusion.com/vue/production/api/spreadsheet/save",
};
},
methods: {
fileMenuItemSelect: function (args) {
var spreadsheet = this.$refs.spreadsheet;
if (args.item.text === "Microsoft Excel") {
args.cancel = true;
spreadsheet.saveAsJson().then((response) => {
var formData = new FormData();
formData.append(
"JSONData",
JSON.stringify(response.jsonObject.Workbook)
);
formData.append("fileName", "Sample");
formData.append("saveType", "Xlsx");
fetch(
"https://services.syncfusion.com/vue/production/api/spreadsheet/save",
{
method: "POST",
headers: { Authorization: "YOUR TEXT" },
body: formData,
}
).then((response) => {
response.blob().then((data) => {
var anchor = createElement("a", {
attrs: { download: "Sample.xlsx" },
});
var url = URL.createObjectURL(data);
anchor.href = url;
document.body.appendChild(anchor);
anchor.click();
URL.revokeObjectURL(url);
document.body.removeChild(anchor);
});
});
});
}
},
},
};
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-spreadsheet/styles/material.css";
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/material.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-grids/styles/material.css";
@import "../node_modules/@syncfusion/ej2-spreadsheet/styles/material.css";
</style>
To change the PDF orientation
By default, the PDF document is created in Portrait orientation. You can change the orientation of the PDF document by using the args.pdfLayoutSettings.orientation
argument settings in the beforeSave
event.
The possible values are:
- Portrait - Used to display content in a vertical layout.
- Landscape - Used to display content in a horizontal layout.
<template>
<ejs-spreadsheet :saveUrl="saveUrl" :allowSave="true" :beforeSave="beforeSave">
<e-sheets>
<e-sheet>
<e-ranges>
<e-range :dataSource="dataSource"></e-range>
</e-ranges>
<e-columns>
<e-column :width="width1"></e-column>
<e-column :width="width2"></e-column>
<e-column :width="width2"></e-column>
<e-column :width="width1"></e-column>
<e-column :width="width2"></e-column>
<e-column :width="width3"></e-column>
</e-columns>
</e-sheet>
</e-sheets></ejs-spreadsheet>
</template>
<script>
import Vue from "vue";
import { SpreadsheetPlugin } from "@syncfusion/ej2-vue-spreadsheet";
import { data } from './data.js';
Vue.use(SpreadsheetPlugin);
export default {
data: () => {
return {
dataSource: data,
width1: 180,
width2: 130,
width3: 120,
saveUrl: 'https://services.syncfusion.com/vue/production/api/spreadsheet/save'
}
},
methods: {
beforeSave: function (args) {
args.pdfLayoutSettings.orientation = 'Landscape'; // You can change the orientation of the PDF document
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-spreadsheet/styles/material.css";
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-dropdowns/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-navigations/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-splitbuttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-grids/styles/material.css';
@import "../node_modules/@syncfusion/ej2-spreadsheet/styles/material.css";
</style>
Supported file formats
The following list of Excel file formats are supported in Spreadsheet:
- MS Excel (.xlsx)
- MS Excel 97-2003 (.xls)
- Comma Separated Values (.csv)
- Portable Document Format (.pdf)
Methods
To save the Spreadsheet document as an xlsx, xls, csv, or pdf
file, by using save method should be called with the url
, fileName
and saveType
as parameters. The following code example shows to save the spreadsheet file as an xlsx, xls, csv, or pdf
in the button click event.
<template>
<div>
<ejs-dropdownbutton :items='items' :select='itemSelect'>Save</ejs-dropdownbutton>
<ejs-spreadsheet ref="spreadsheet">
<e-sheets>
<e-sheet>
<e-ranges>
<e-range :dataSource="dataSource"></e-range>
</e-ranges>
<e-columns>
<e-column :width="width1"></e-column>
<e-column :width="width2"></e-column>
</e-columns>
</e-sheet>
</e-sheets></ejs-spreadsheet>
<div>
</template>
<script>
import Vue from "vue";
import { SpreadsheetPlugin, getRangeIndexes } from "@syncfusion/ej2-vue-spreadsheet";
import { addClass, removeClass } from '@syncfusion/ej2-base';
import { DropDownButtonPlugin } from "@syncfusion/ej2-vue-splitbuttons";
import { defaultData } from './data.js';
Vue.use(SpreadsheetPlugin);
Vue.use(DropDownButtonPlugin);
export default {
data: () => {
return {
dataSource: defaultData,
width1: 130,
width2: 96,
items:[
{
text: "Save As xlsx"
},
{
text: "Save As xls"
},
{
text: "Save As csv"
},
{
text: "Save As pdf"
}
]
}
},
methods: {
itemSelect: function(args) {
var spreadsheet = this.$refs.spreadsheet;
if (args.item.text === 'Save As xlsx')
spreadsheet.save({url: 'https://services.syncfusion.com/vue/production/api/spreadsheet/save', fileName: "Sample", saveType: "Xlsx"});
if (args.item.text === 'Save As xls')
spreadsheet.save({url: 'https://services.syncfusion.com/vue/production/api/spreadsheet/save', fileName: "Sample", saveType: "Xls"});
if (args.item.text === 'Save As csv')
spreadsheet.save({url: 'https://services.syncfusion.com/vue/production/api/spreadsheet/save',fileName: "Sample", saveType: "Csv"});
if (args.item.text === 'Save As pdf')
spreadsheet.save({url: 'https://services.syncfusion.com/vue/production/api/spreadsheet/save',fileName: "Sample", saveType: "Pdf"});
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-spreadsheet/styles/material.css";
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-dropdowns/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-navigations/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-splitbuttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-grids/styles/material.css';
@import "../node_modules/@syncfusion/ej2-spreadsheet/styles/material.css";
</style>
Server Configuration
Import and export are processed in server-side
using Spreadsheet server library. The following code snippets shows server configuration using WebAPI
service,
[Route("api/[controller]")]
public class SpreadsheetController : Controller
{
//To open excel file
[AcceptVerbs("Post")]
[HttpPost]
[EnableCors("AllowAllOrigins")]
[Route("Open")]
public IActionResult Open(IFormCollection openRequest)
{
OpenRequest open = new OpenRequest();
open.File = openRequest.Files[0];
return Content(Workbook.Open(open));
}
//To save as excel file
[AcceptVerbs("Post")]
[HttpPost]
[EnableCors("AllowAllOrigins")]
[Route("Save")]
public IActionResult Save(SaveSettings saveSettings)
{
return Workbook.Save(saveSettings);
}
}
Server Dependencies
Open and save helper functions are shipped in the Syncfusion.EJ2.Spreadsheet package, which is available in Essential Studio and nuget.org
. Following list of dependencies required for Spreadsheet open and save operations.
- Syncfusion.EJ2
- Syncfusion.EJ2.Spreadsheet
- Syncfusion.Compression.Base
- Syncfusion.XlsIO.Base
And also refer this for more information.
Note
You can refer to our Vue Spreadsheet feature tour page for its groundbreaking feature representations. You can also explore our Vue Spreadsheet example to knows how to present and manipulate data.