Import data from Excel files

Greetings!

I'd like to use the <SfUploader> component to receive Excel files and use them to create objects based on it's data. There's no need to save the file, I just need to access the data.

looking into the documentation, I believe the best event to do this is the ValueChange. But the Stream property is deprecated.

Image_7169_1699368900215

The UploadStart works, but it needs a saveUrl and I don't want to save the file. 

Could you please give some help? What is the best way to get data from Excel files using the  <SfUploader> component?


1 Reply

MR Mallesh Ravi Chandran Syncfusion Team November 9, 2023 06:43 PM UTC

Hi Eduardo , 


This is breaking changes in our end. Previously, the UploadChangeEventArgs.Stream property would return the memory stream. However, in this release, the UploadChangeEventArgs.Stream property will return null instead of the memory stream. This Stream property in UploadFiles class has been deprecated and will no longer be used. It will be removed in a future version. You can create a stream manually by calling OpenReadStream method. Please find the example code and sample for your reference. Furthermore, in order to meet your requirements, we have also shared a simple sample and a video GIF for your reference.


<SfUploader AutoUpload="true">

    <UploaderEvents ValueChange="@OnChange"></UploaderEvents>

</SfUploader>

@code {

    private async Task OnChange(UploadChangeEventArgs args)

    {

        try

        {

            foreach (var file in args.Files)

            {

                var tempFilePath = Path.GetTempPath() + "\\" + Guid.NewGuid().ToString();

                Stream Stream = new FileStream(tempFilePath, FileMode.Create);

                await file.File.OpenReadStream(long.MaxValue).CopyToAsync(Stream);

                Stream.Close();

              

                using (Stream fileStream = new FileStream(tempFilePath, FileMode.Open))

                {

                    using (ExcelEngine excelEngine = new ExcelEngine())

                    {                  

                        IApplication application = excelEngine.Excel;                 

                        application.DefaultVersion = ExcelVersion.Xlsx;                    

                        IWorkbook workbook = application.Workbooks.Open(fileStream);

                        IWorksheet worksheet = workbook.Worksheets[0];

                        var value = worksheet.Range["A1"].Value;

                        Console.WriteLine(value);

                    }

                }

            }

        }

    }

}



Gif :




Furthermore, we have also shared the simple sample and video gif for your reference. 


Documentation :  https://blazor.syncfusion.com/documentation/release-notes/21.1.35?type=all#file-upload


Regards, 

Mallesh 



Attachment: Uploader_3a70ec35.zip

Loader.
Up arrow icon