[Script] function OnbeforeOpen(args) { if (args.itemType == "File") { args.cancel = true; var filename = args.model.selectedFolder + args.model.selectedItems[0]; $.ajax({ url: '@Url.Action("Openingfile", "FileExplorer")', type: "POST", dataType: "json", data: { filename: filename }, success: function (data) { console.log("success"); } }); } } [Controller] public void Openingfile(string filename) { Process cmd = new Process(); cmd.StartInfo.FileName = "cmd.exe"; cmd.StartInfo.RedirectStandardInput = true; cmd.StartInfo.RedirectStandardOutput = true; cmd.StartInfo.RedirectStandardError = true; cmd.StartInfo.CreateNoWindow = true; cmd.StartInfo.UseShellExecute = false; cmd.Start(); cmd.StandardInput.WriteLine(filename); cmd.StandardInput.Flush(); cmd.StandardInput.Close(); } |
@(Html.EJ().FileExplorer("fileExplorer")
.FileTypes("*.png, *.gif, *.jpg, *.jpeg, *.docx, *.pdf, *.xlsx, *.zip, *.rar")
.Layout(LayoutType.Tile)
.Path(@Url.Content("~/FileExplorerContent/"))
.AjaxAction(@Url.Action("FileActionDefault"))
.Width("100%").Height("600")
.IsResponsive(true)
.MinWidth("350px")
.ClientSideEvents(eve => eve.Create("onCreate").BeforeOpen("onBeforeOpen"))
)
<script>
function onCreate(args) {
this._viewMenuObj.option({
open: function (args) {
this.enableItem("Open");
}
});
}
function onBeforeOpen(args) {
if (args.itemType == "File" && !(/\.(bmp|dib|jpg|jpeg|jpe|jfif|gif|tif|tiff|png|ico)$/i).test(this._selectedFile)) {
var file = args.model.selectedFolder.replace("~", "..") + args.model.selectedItems[0];
window.open(file, args.model.selectedItems[0], '_blank', 'fullscreen=yes, menubar=yes, resizable=yes, titlebar=yes, toolbar=yes');
args.cancel = true;
}
}
function onclick() {
$.ajax({
url: '@Url.Action("RefreshFolders")',
type: "POST",
success: function (result) {
//alert("ok");
},
error: function (result) {
alert("error"+result);
}
});
}
</script>
How to open xls (excel) format using ejSpreadsheet?
Regards,
Anis
//… function onBeforeOpen(args) { if (args.itemType == "File" && !(/\.(bmp|dib|jpg|jpeg|jpe|jfif|gif|tif|tiff|png|ico)$/i).test(this._selectedFile)) { var file = args.model.selectedFolder.replace("~", "..") + args.model.selectedItems[0]; if (args.model.selectedItems[0].indexOf(".xlsx") > -1) //If it is excel file, action changed. file = '@Url.Action("ViewInSpreadsheet")?filePath=' + file; window.open(file, '_blank', 'fullscreen=yes, menubar=yes, resizable=yes, titlebar=yes, toolbar=yes'); args.cancel = true; } } //… |
public ActionResult Index() { return View(); } public ActionResult ViewInSpreadsheet(string filePath) { ViewData["FilePath"] = filePath; return PartialView("_ViewInSpreadsheet", ViewData); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult Import(ImportRequest importRequest) { if (!String.IsNullOrEmpty(importRequest.Url)) importRequest.Url = Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath, "") + importRequest.Url; return importRequest.SpreadsheetActions(); } |
@(Html.EJ().Spreadsheet<object>("Spreadsheet") //... .ImportSettings(import => { import.ImportUrl((string)ViewData["FilePath"]); import.ImportMapper("Import"); }) //... ) |
<input type="button" id="saveSpreadsheet" value="Save Spreadsheet" /> <input type="hidden" id="filePath" value="@ViewData["FilePath"]" /> @(Html.EJ().Spreadsheet<object>("Spreadsheet") //... ) <script type="text/javascript"> $(function () { $("#saveSpreadsheet").bind("click", function () { var xlObj = $("#Spreadsheet").data("ejSpreadsheet"), saveFilePath = $("#filePath").val(), exportProps = xlObj.XLExport.getExportProps(); $.ajax({ type: "POST", data: { sheetModel: exportProps.model, sheetData: exportProps.data, filePath: saveFilePath }, url: "/Home/SaveSpreadsheetOnBtnClick", success: function (data) { alert(data); } }); }); }); </script> |
[AcceptVerbs(HttpVerbs.Post)] public ActionResult SaveSpreadsheetOnBtnClick(string sheetModel, string sheetData, string filePath) { MemoryStream ms = new MemoryStream(); filePath = Path.Combine(Server.MapPath(filePath)); Stream sData = Spreadsheet.Save(sheetModel, sheetData, ExportFormat.XLSX, ExcelVersion.Excel2013); sData.Position = 0; //Reset stream position ExcelEngine excelEngine = new ExcelEngine(); IApplication application = excelEngine.Excel; IWorkbook workbook = application.Workbooks.Open(sData); workbook.SaveAs(filePath); return Content("File saved successfully!"); } |
<input type="hidden" id="filePath" value="@ViewData["FilePath"]" /> @(Html.EJ().Spreadsheet<object>("Spreadsheet") //... .ExportSettings(export => { export.ExcelUrl("SaveSpreadsheetToServer"); //... }) .ClientSideEvents(eve => { eve.OnExport("onExport"); }) ) <script type="text/javascript"> function onExport(args) { if (args.exportType == "Excel") args.customParams["filePath"] = $("#filePath").val(); } </script> @Html.Raw(ViewData["message"])@*For alert message from server*@ |
[AcceptVerbs(HttpVerbs.Post)] public ActionResult SaveSpreadsheetToServer(string sheetModel, string sheetData, string filePath) { ViewData["FilePath"] = filePath; MemoryStream ms = new MemoryStream(); filePath = Path.Combine(Server.MapPath(filePath)); Stream sData = Spreadsheet.Save(sheetModel, sheetData, ExportFormat.XLSX, ExcelVersion.Excel2013); sData.Position = 0;//Reset stream position ExcelEngine excelEngine = new ExcelEngine(); IApplication application = excelEngine.Excel; IWorkbook workbook = application.Workbooks.Open(sData); workbook.SaveAs(filePath); ViewData["message"] = "<script language='javascript' type='text/javascript'>alert('File Saved successfully!');</script>"; return PartialView("_ViewInSpreadsheet", ViewData); } |