..//
// Save file to DB
$("#saveFileToDatabase").bind("click", function () {
var xlObj = $("#Spreadsheet").data("ejSpreadsheet"), exportProp = xlObj.XLExport.getExportProps(), filename = $("#exportFileName").val();
$.ajax({
type: "POST",
data: { fileName: filename, sheetModel: exportProp.model, sheetData: exportProp.data },
url: "/Home/SaveAndUpdateFileFromDatabase",
success: function (data) {
if ($("#ImportFileName").text().indexOf(filename.replace(".xlsx", "")) === -1)
$("#ImportFileName").append("<option value=" + filename + ">" + filename.replace(".xlsx", "") + "</option>");
alert("File Saved Successfully!");
}
});
});
// Open file from DB
$("#openFileFromDatabase").bind("click", function () {
var xlObj = $("#Spreadsheet").data("ejSpreadsheet"), fileName = $("#ImportFileName").val();
if (fileName.length) {
xlObj.showWaitingPopUp();
$.ajax({
type: "POST",
data: { filename: fileName },
url: "/Home/OpenFileFromDB",
success: function (data) {
xlObj.loadFromJSON(JSON.parse(data));
xlObj.hideWaitingPopUp();
$("#exportFileName").val(fileName);
}
});
}
});
..// |
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ReadAndUpdateDBData()
{
string fileName = "File2.xlsx";
MemoryStream dataStream = new MemoryStream();
SqlConnection sqlCon = new SqlConnection(connetionString);
SqlCommand sqlComm = new SqlCommand("SELECT * FROM [dbo].[Table] WHERE [FileName] = '" + fileName + "'", sqlCon);
sqlCon.Open();
SqlDataReader sqlDR = sqlComm.ExecuteReader();
if (sqlDR.Read())
{
dataStream = new MemoryStream((byte[])sqlDR.GetValue(1)); // Get Stream values from DB.
}
sqlDR.Close();
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
IWorkbook workbook = application.Workbooks.Open(dataStream);
IWorksheet sheet = workbook.Worksheets[0];
sheet.Cells[0].Text = "Spreadsheet"; // C# backend edit process by using XlsIo Library.
MemoryStream newDataStream = new MemoryStream();
workbook.Version = ExcelVersion.Excel2013;
workbook.Application.DefaultVersion = ExcelVersion.Excel2013;
workbook.SaveAs(newDataStream);
SqlCommand updateComm = new SqlCommand("UPDATE [dbo].[Table] SET FileData=@nFileData WHERE FileName=@nFileName", sqlCon);
updateComm.CommandTimeout = 0;
newDataStream.Position = 0;
byte[] dataBytes = new BinaryReader(newDataStream).ReadBytes(Convert.ToInt32(newDataStream.Length));
updateComm.Parameters.AddWithValue("@nFileName", fileName);
updateComm.Parameters.AddWithValue("@nFileData", dataBytes);
updateComm.ExecuteNonQuery();
sqlCon.Close();
return Content("Successfully updated");
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveAndUpdateFileFromDatabase(string fileName, string sheetModel, string sheetData)
{
Stream dataStream = Spreadsheet.Save(sheetModel, sheetData, ExportFormat.XLSX, ExcelVersion.Excel2013);
dataStream.Position = 0;
byte[] dataBytes = new BinaryReader(dataStream).ReadBytes(Convert.ToInt32(dataStream.Length));
SqlConnection sqlCon = new SqlConnection(connetionString);
sqlCon.Open();
SqlCommand selectComm = new SqlCommand("SELECT * FROM [dbo].[Table] WHERE [FileName] = '" + fileName + "'", sqlCon);
var hasFileInDB = selectComm.ExecuteScalar();
if (hasFileInDB == null)
{
SqlCommand sqlComm = new SqlCommand("INSERT INTO [dbo].[Table]([FileName], [FileData]) VALUES (@FileName, @FileData)", sqlCon);
sqlComm.Parameters.AddWithValue("@FileName", fileName);
sqlComm.Parameters.AddWithValue("@FileData", dataBytes);
sqlComm.ExecuteNonQuery();
}
else
{
SqlCommand updateComm = new SqlCommand("UPDATE [dbo].[Table] SET FileData=@nFileData WHERE FileName=@nFileName", sqlCon); // Update edited data to DB.
updateComm.CommandTimeout = 0;
updateComm.Parameters.AddWithValue("@nFileName", fileName);
updateComm.Parameters.AddWithValue("@nFileData", dataBytes);
updateComm.ExecuteNonQuery();
}
sqlCon.Close();
return Content(fileName);
}
//File Open from database
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult OpenFileFromDB(string filename)
{
ImportRequest importRequest = new ImportRequest();
SqlConnection sqlCon = new SqlConnection(connetionString);
SqlCommand sqlComm = new SqlCommand("SELECT * FROM [dbo].[Table] WHERE [FileName] = '" + filename + "'", sqlCon);
sqlCon.Open();
SqlDataReader sqlDR = sqlComm.ExecuteReader();
if (sqlDR.Read())
{
importRequest.FileStream = new MemoryStream((byte[])sqlDR.GetValue(1)); // Get binary values from DB.
}
sqlCon.Close();
return Content(Spreadsheet.Open(importRequest));
}
|
$("#openFileFromDatabase").bind("click", function () {
var xlObj = $("#Spreadsheet").data("ejSpreadsheet"), fileName = $("#ImportFileName").val();
if (fileName.length) {
xlObj.showWaitingPopUp();
$.ajax({
type: "POST",
data: { filename: fileName },
url: "/Home/OpenFileFromDB",
success: function (data) {
xlObj.loadFromJSON(JSON.parse(data));
xlObj.hideWaitingPopUp();
$("#exportFileName").val(fileName);
}
});
}
});
$("#saveFileToDatabase").bind("click", function () {
var xlObj = $("#Spreadsheet").data("ejSpreadsheet"), exportProp = xlObj.XLExport.getExportProps(), filename = $("#exportFileName").val();
$.ajax({
type: "POST",
data: { fileName: filename, sheetModel: exportProp.model, sheetData: exportProp.data },
url: "/Home/SaveAndUpdateFiletoDB",
success: function (data) {
if ($("#ImportFileName").text().indexOf(filename.replace(".xlsx", "")) === -1)
$("#ImportFileName").append("<option value=" + filename + ">" + filename.replace(".xlsx", "") + "</option>");
alert("File Saved Successfully!");
}
});
});
|
[AcceptVerbs(HttpVerbs.Post)]
public string SaveAndUpdateFiletoDB(string fileName, string sheetModel, string sheetData)
{
Stream dataStream = Spreadsheet.Save(sheetModel, sheetData, ExportFormat.XLSX, ExcelVersion.Excel2013);
dataStream.Position = 0;
ExcelEngine engine = new ExcelEngine();
IApplication application = engine.Excel;
IWorkbook workbook = application.Workbooks.Open(dataStream);
IWorksheet worksheet = workbook.Worksheets[0];
worksheet.Cells[0].Text = "Spreadsheet";
MemoryStream newStream = new MemoryStream();
workbook.Version = ExcelVersion.Excel2013;
workbook.Application.DefaultVersion = ExcelVersion.Excel2013;
// Reset stream position.
workbook.SaveAs(newStream);
ImportRequest ImpReq = new ImportRequest();
newStream.Position = 0;
ImpReq.FileStream = newStream;
// Convert Stream to string here.
string SpreadsheetData = Spreadsheet.Open(ImpReq);
SqlConnection sqlCon = new SqlConnection(connetionString);
sqlCon.Open();
SqlCommand selectComm = new SqlCommand("SELECT * FROM [dbo].[Table] WHERE [FileName] = '" + fileName + "'", sqlCon);
var hasFileInDB = selectComm.ExecuteScalar();
if (hasFileInDB == null)
{
SqlCommand sqlComm = new SqlCommand("INSERT INTO [dbo].[Table]([FileName], [FileData]) VALUES (@FileName, @FileData)", sqlCon);
sqlComm.Parameters.AddWithValue("@FileName", fileName);
// Saved Spreadsheet data as JSON in DB
sqlComm.Parameters.AddWithValue("@FileData", SpreadsheetData);
sqlComm.ExecuteNonQuery();
}
else
{
SqlCommand updateComm = new SqlCommand("UPDATE [dbo].[Table] SET FileData=@nFileData WHERE FileName=@nFileName", sqlCon); // Update edited data to DB.
updateComm.CommandTimeout = 0;
updateComm.Parameters.AddWithValue("@nFileName", fileName);
updateComm.Parameters.AddWithValue("@nFileData", SpreadsheetData);
updateComm.ExecuteNonQuery();
}
sqlCon.Close();
return fileName;
}
//File Open from database
[AcceptVerbs(HttpVerbs.Post)]
public string OpenFileFromDB(string filename)
{
ImportRequest importRequest = new ImportRequest();
SqlConnection sqlCon = new SqlConnection(connetionString);
SqlCommand sqlComm = new SqlCommand("SELECT * FROM [dbo].[Table] WHERE [FileName] = '" + filename + "'", sqlCon);
string JsonData = "";
sqlCon.Open();
SqlDataReader sqlDR = sqlComm.ExecuteReader();
if (sqlDR.Read())
{
// Retrive JSON from DB
JsonData = (string)sqlDR.GetValue(1);
}
sqlCon.Close();
return JsonData;
}
|