Hi Team,
I am creating CSV file using memory stream which is working fine.
Now I want to create Excel file instead of CSV file.
Below is my working code, My requirement is instead of csv file need to create excel file.
using (var memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
CreateZipEntry("result.csv", sb.ToString(), archive);
CreateZipEntry("generated-at-" + DateTime.UtcNow.ToString("yyyy-MM-dd-HH-mm-ss") + "-UTC.txt", "", archive);
}
using (var fileStream = new FileStream(@"D:\Relation-Data.zip", FileMode.Create))
{
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
}
}
Gone through many options, however not been successful in any option.
Please help me to write data into an Excel file. this is a very urgent task for me.
I would like to use the Interop library, Please post a piece of code for the same.
Thanks,
Lalitha.C
Jignesh KumarPosted Nov 2, 2023, 4:03 AM
Hi Lalitha,
Please use below one which will help you out,
Marvin ReidPosted Nov 1, 2023, 4:00 PM
You can try leveraging the DocumentConverter class to convert to Excel which does support passing a MemoryStream. Here is an example that can get you started.
https://www.leadtools.com/help/sdk/tutorials/dotnet-console-convert-files-with-the-document-converter.html
Lalitha ChevuruPosted May 8, 2023, 6:00 AM
Thank you Leon.
My data is in string builder, how to convert that data into stream - please help me on this, I am really stuck with this issue.
I tried to convert from string to stream using below code
byte[] byteArray = Encoding.ASCII.GetBytes(sb.ToString());
MemoryStream stream = new MemoryStream(byteArray);
Workbook workbook = new Workbook();
workbook.LoadFromStream(stream);
workbook.SaveToFile("D:/ReadStream.xlsx", ExcelVersion.Version2013);
while writing into excel it is throwing "Cannot read that as a zip file" error.
Thanks,
Lalitha.C
Leon DPosted May 8, 2023, 1:51 AM
You can install free spire.xls through nuget (https://www.nuget.org/packages/FreeSpire.XLS/), then refer to the below code sample to write data from the memory stream to an Excel file: