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);
}
}
}
}
Hi Marc,
We are validating the requirement. We will update the further details in two business days (26th August 2022).
Regards,
Udhaya Kumar D
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
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.
I figured it out. I needed to use `IWorkbook workbook = excelEngine.Excel.Workbooks.Open(ms);` instead of ` IWorkbook workbook = excelEngine.Excel.Workbooks.Open(ms, ",");`
Hi Marc,
Thanks for the update. Please get back to us if you need any further assistance.
Regards,
Udhaya Kumar D
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!
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