App.component.html
<div>
<button class="e-btn" (click)="saveToServer()">Save Back to Server</button>
<button class="e-btn" (click)="LoadFromDataBase()">Load Data</button>
<label>File name:</label>
<input type="text" id="filename" value="Sample" placeholder="Specify file name">
</div>
App.component.ts
saveToServer() {
this.spreadsheetObj.saveAsJson().then(Json => (fetch('https://localhost:44376/Home/SaveFiletoDB', {
method: 'POST', // or 'PUT'
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name: (document.getElementById("filename") as HTMLInputElement).value, JSONData: JSON.stringify((Json as any).jsonObject) }),
})
.then((response) => response.json())
.then((data) => {
console.log(data);
})))
}
LoadFromDataBase() {
fetch('https://localhost:44376/Home/LoadFromDataBase', {
method: 'POST', // or 'PUT'
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name: (document.getElementById("filename") as HTMLInputElement).value }),
})
.then((response) => response.json())
.then((data) => {
this.spreadsheetObj.openFromJson({ file: data });
})
} |
public IActionResult SaveFiletoDB([FromBody]SaveOptions saveSettings)
{
//Save JSON to database
SqlConnection sqlCon = new SqlConnection(connectionString);
sqlCon.Open();
SqlCommand sqlComm = new SqlCommand("INSERT INTO [dbo].[SpreadsheetData]([filename], [data]) VALUES (@filename, @data)", sqlCon);
sqlComm.Parameters.AddWithValue("@filename", saveSettings.Name);
sqlComm.Parameters.AddWithValue("@data", saveSettings.JSONData);
sqlComm.ExecuteNonQuery();
sqlCon.Close();
return Content("Saved in Server");
}
[HttpPost]
public string LoadFromDataBase([FromBody]FileOptions file)
{
OpenRequest open = new OpenRequest();
//Load JSON from database
SqlConnection sqlCon = new SqlConnection(connectionString);
SqlCommand sqlComm = new SqlCommand("SELECT * FROM [dbo].[SpreadsheetData] WHERE [filename] = '" + file.Name + "'", sqlCon);
sqlCon.Open();
SqlDataReader sqlDR = sqlComm.ExecuteReader();
string JSONData = "";
while (sqlDR.Read())
{
JSONData = sqlDR.GetValue(1).ToString();
open.File = JSONData;
}
sqlCon.Close();
string data = JSONData ;
return data; // returning JSON data to spreadsheet control
}
|
public string Save(SaveSettings saveSettings)
{
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
try
{
// Convert Spreadsheet data as Stream
Stream fileStream = Workbook.Save<Stream>(saveSettings);
IWorkbook workbook = application.Workbooks.Open(fileStream);
var filePath = Startup._env.ContentRootPath.ToString() + "/Files/Sample1.xlsx";
FileStream outputStream = new FileStream(filePath, FileMode.Create);
workbook.SaveAs(outputStream);
return "Spreadsheet saved in server";
}
catch (Exception ex)
{
return "Failure";
}
} |
saveToServer() {
this.spreadsheetObj.saveAsJson().then((Json) =>
fetch(this.saveUrl, {
method: 'POST', // or 'PUT'
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
FileName: 'FileName',
JSONData: JSON.stringify((Json as any).jsonObject.Workbook),
ContentType: "Xlsx",
VersionType: "Xlsx",
}),
})
.then((response) => response.json())
.then((data) => {
console.log(data);
})
);
} |
public string Save([FromBody] SaveSettings saveSettings)
{
Console.Write("SaveFile");
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
try
{
// Convert Spreadsheet data as Stream
Stream fileStream = Workbook.Save<Stream>(saveSettings);
IWorkbook workbook = application.Workbooks.Open(fileStream);
FileStream outputStream = new FileStream(filePath, FileMode.Create);
workbook.SaveAs(outputStream);
return "Spreadsheet saved in server";
}
catch (Exception ex)
{
return "Failure";
}
return "Spreadsheet saved in server";
} |