Articles in this section

How to Retrieve Workbook JSON in Console in ASP.NET MVC Spreadsheet?

This knowledge base article explains how to retrieve the Workbook JSON of the ASP.NET MVC Spreadsheet in a Console Application.

To achieve this, create a console application using the .NET 8.0 framework and add a FrameworkReference to Microsoft.AspNetCore.App in the .csproj file, as shown below

[ConsoleApp1.csproj]:

<Project Sdk="Microsoft.NET.Sdk">

 <PropertyGroup>
   <OutputType>Exe</OutputType>
   <TargetFramework>net8.0</TargetFramework>
   <ImplicitUsings>enable</ImplicitUsings>
   <Nullable>enable</Nullable>
 </PropertyGroup>

 <ItemGroup>
   <FrameworkReference Include="Microsoft.AspNetCore.App" />
   <PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
   <PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="5.0.17" />
   <PackageReference Include="Syncfusion.EJ2.Spreadsheet.AspNet.Core" Version="26.2.8" />
 </ItemGroup>

</Project> 

The Microsoft.AspNetCore.App FrameworkReference includes the package Microsoft.AspNetCore.Razor, which is necessary for processing the Excel files using the Syncfusion.EJ2.Spreadsheet.AspNet.Core package and also includes all the necessary ASP.NET Core-related packages and supports IFormFile in the console application.

Now, you need to open the Excel file as a stream and then convert the stream as a FormFile and pass it to the Open API of spreadsheet, which processes the Excel file and returns the spreadsheet-compatible Workbook JSON.

[Program.cs]:

using System;
using System.IO;
using Syncfusion.EJ2.Spreadsheet;
using Microsoft.AspNetCore.Http;

class Program
{
   static void Main(string[] args)
   {

       string fileName = "Sample";
       string projectDirectory = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.Parent.FullName;
       string documentPath = Path.Combine(projectDirectory, "Files", fileName + ".xlsx");
       try
       {
           OpenRequest open = new OpenRequest();

           // Open the file using a FileStream
           FileStream fs = new FileStream(documentPath, FileMode.Open, FileAccess.Read);

           byte[] tmpBytes = new byte[fs.Length];
           fs.Read(tmpBytes, 0, Convert.ToInt32(fs.Length));

           MemoryStream outputStream = new MemoryStream(tmpBytes);

           // Converting the stream to IFormFile
           IFormFile formFile = new FormFile(outputStream, 0, outputStream.Length, fileName, fileName + ".xlsx");
           open.File = formFile;

           // Open method will process the Excel file and returns the spreadsheet compatible workbook JSON.
           var result = Workbook.Open(open);

           // Close the FileStream
           fs.Close();

           Console.WriteLine(result);

       }
       catch (Exception ex)
       {
           Console.WriteLine($"An error occurred: {ex.Message}");
       }
   }
} 

Sample Link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/ConsoleApp11610340305

Output:

JSON_in_Console.png

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