Articles in this section
Category / Section

How to open and save an excel file using ASP.NET MVC Visual Basic service in the JavaScript Spreadsheet?

3 mins read

This knowledge base explains the way to save the spreadsheet data as excel file to server and load the excel into the spreadsheet.

Using JavaScript Spreadsheet you can use “openFromJson” and “saveAsJson” client-side methods to achieve this requirement. If you save the spreadsheet as an excel file to server, then you need to send the file name, JSON data, content type & version type from client to server. If you open the excel in spreadsheet, then you need to send file name from client to server.

[index.vbhtml]

@Imports Syncfusion.EJ2

   < button class="e-btn" id = "open" > Open < /button>

       < button class="e-btn" id = "save" > Save < /button>

           <div>
@Html.EJS().Spreadsheet("spreadsheet").OpenUrl("Home/Open").SaveUrl("Home/Save").Height("450px").Render()
</div>

   

[HomeController.vb]

Public Function LoadExcel(ByVal file As FileOptions) As String
   Dim excelEngine As ExcelEngine = New ExcelEngine()
   Dim ioWorkbook As IWorkbook
   Dim fs As FileStream = System.IO.File.Open(HttpContext.Server.MapPath("~/Files/") + file.FileName &amp; ".xlsx", FileMode.Open)
   ioWorkbook = excelEngine.Excel.Workbooks.Open(fs, ExcelOpenType.Automatic)
   Dim outputStream As MemoryStream = New MemoryStream()
   ioWorkbook.SaveAs(outputStream)
   Dim fileBase As HttpPostedFileBase = CType(New HttpPostedFile(outputStream.ToArray(), file.FileName &amp; ".xlsx"), HttpPostedFileBase)
   Dim files As HttpPostedFileBase() = New HttpPostedFileBase(0) {}
   files(0) = fileBase
   Dim open As OpenRequest = New OpenRequest()
   open.File = files
   fs.Close()
   outputStream.Dispose()

   Return workbook.Open(open)
End Function

Function Save(saveSettings As SaveSettings) As String
   Dim excelEngine As ExcelEngine = New ExcelEngine()
   Dim application As IApplication = excelEngine.Excel
   Try
       Dim basePath As String = HttpContext.Server.MapPath("~/Files/" &amp; saveSettings.FileName &amp; ".xlsx")
       Dim fileStream As Stream = Workbook.Save(Of Stream)(saveSettings)
       Dim workbook1 As IWorkbook = application.Workbooks.Open(fileStream)
       Dim file1 As FileStream = System.IO.File.Create(basePath)
       fileStream.Seek(0, SeekOrigin.Begin)
       fileStream.CopyTo(file1)
       file1.Close()
       file1.Dispose()
       fileStream.Close()
       fileStream.Dispose()
       workbook1.Close()
       Return "File Saved Successfully..!"
   Catch ex As Exception
       Return "Failure"
   End Try
End Function

Please refer the below reference to render spreadsheet in the ASP.NET MVC Visual Basic.
https://support.syncfusion.com/kb/article/12314/how-to-render-the-asp-net-mvc-spreadsheet-control-in-the-asp-net-mvc-visual-basic

Sample link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/VB_Sample1196338096

Output:

output.png

UG documentation:
https://ej2.syncfusion.com/aspnetmvc/documentation/spreadsheet/getting-started-mvc
https://ej2.syncfusion.com/aspnetmvc/documentation/spreadsheet/open-save

Conclusion
I hope you enjoyed learning about how to open and save an excel file using ASP.NET MVC Visual Basic service in the JavaScript Spreadsheet.

You can refer to our JavaScript Spreadsheet feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our JavaScript Spreadsheet example to understand how to create and manipulate data.

For current customers, you can check out our Document Processing Libraries from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied