Uploading an Excel file using MemoryStream not parsing the file correctly in the code

I am trying to have a user upload an excel file, then my program needs to read that file and assign the values to an object. When I run this, the results are an unintelligible string in the first row / first column, and almost everything else is blank. I think it has something to do with reading the file, but I'm not sure. The file upload works on other file types.


Sorry. I don't know how to format on this site.




using (MemoryStream ms = new())

{
Stream stream = UploadedFile.OpenReadStream();
await stream.CopyToAsync(ms);
stream.Close();
ms.Position = 0;


using (ExcelEngine excelEngine = new ExcelEngine())
using (MemoryStream csvStream = new MemoryStream())
{
//Initialize application
IApplication app = excelEngine.Excel;


app.DefaultVersion = ExcelVersion.Xlsx;


IWorksheet worksheet;


IWorkbook workbook = excelEngine.Excel.Workbooks.Open(ms, ",");
if (SheetName == "")
{
worksheet = workbook.Worksheets[0];
}
else
{
worksheet = workbook.Worksheets[SheetName];
}




worksheet.SaveAs(csvStream, ",", System.Text.Encoding.Default);
csvStream.Position = 0;


var temp = worksheet["B3"].DisplayText;


using (TextFieldParser parser = new TextFieldParser(csvStream, System.Text.Encoding.Default))
{
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");


parser.ReadFields();


while (!parser.EndOfData)
{
var dataLine = parser.ReadFields();
FileLines.Add(dataLine);
}
}
}
}

7 Replies

UD UdhayaKumar Duraisamy Syncfusion Team August 25, 2022 03:00 AM UTC

Hi Marc,


We are validating the requirement. We will update the further details in two business days (26th August 2022).


Regards,

Udhaya Kumar D



UD UdhayaKumar Duraisamy Syncfusion Team August 26, 2022 12:40 PM UTC

Hi Marc,


We suggest you access the excel file rows and cell data from the memory stream as like below code example.


Find the code example:

  <SfUploader ID="UploadFiles" MaxFileSize=50000 AllowMultiple="false" AutoUpload="false">

                <UploaderEvents ValueChange="LoadFilesSf" />

            </SfUploader>

 

@code {

 

 

    public async void LoadFilesSf(UploadChangeEventArgs args)

    {

        Console.WriteLine("Upload triggered");

        if (args.Files.Count == 1)

        {

            var file = args.Files[0];

            using (ExcelEngine excelEngine = new ExcelEngine())

            {

                MemoryStream inputStream = file.Stream;

                // set the memmory stream position at 0 before pass the excelengine

                inputStream.Position = 0;

                IWorkbook workbook = excelEngine.Excel.Workbooks.Open(inputStream);

                workbook.Version = ExcelVersion.Xlsx;

                IWorksheet sheet = workbook.Worksheets[0];

                //Read value Cell A1 value

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

                //Assign the value in the Cell A5

                sheet.Range["A5"].Text = "PageBreak";

                //Saving the workbook

                FileStream outputStream = new FileStream("Output.xlsx", FileMode.Create);

                workbook.SaveAs(outputStream);

            }

        }

    }

}

 


Kindly try the above suggestion and let us know if this meets your requirement.


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Regards,

Udhaya Kumar D




MP Marc Perry August 26, 2022 02:28 PM UTC

When I try this I get an error on the `excelEngine.Excel.Workbooks.Open(inputStream` line that says "Synchronous reads are not supported". That is why I originally read the uploaded file stream into a MemoryStream object. Keep in mind that the type of the file is an IBrowserFile. It isn't located in a physical location.



MP Marc Perry August 26, 2022 02:31 PM UTC

I figured it out. I needed to use `IWorkbook workbook = excelEngine.Excel.Workbooks.Open(ms);` instead of `  IWorkbook workbook = excelEngine.Excel.Workbooks.Open(ms, ",");`



UD UdhayaKumar Duraisamy Syncfusion Team August 29, 2022 10:57 AM UTC

Hi Marc,


Thanks for the update. Please get back to us if you need any further assistance.


Regards,

Udhaya Kumar D



SZ Szoke replied to UdhayaKumar Duraisamy March 29, 2023 08:36 PM UTC

HI, 

in 21.1.35 version   the "UploadChangeEventArgs.Stream property will return null instead of the memory stream."


What could be a good solution from now on?

Thank you in advance for your help!



UD UdhayaKumar Duraisamy Syncfusion Team March 30, 2023 04:34 PM UTC

You can refer to the Syncfusion forum shared below regarding your requirement.

https://www.syncfusion.com/forums/181370/file-upload-stream-write-error?reply=SFpz23


Loader.
Up arrow icon