Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Mon May 13, 2024 8:21 am

Hello,
I want to attach a WorkBook to an email as a stream.
My code this, but when I open the attached Excel file from the email, it says the file is corrupted and cannot be opened.
When I use `SaveToFile` instead of `SaveToStream`, the file opens fine.
How can I resolve this issue when using `SaveToStream`?

Code: Select all
                Workbook workbook = new Workbook();
                workbook.Version = ExcelVersion.Version2013;
                Worksheet worksheet = workbook.Worksheets[0];
                worksheet.InsertDataTable(tempDt, true, 1,1);
                worksheet.AllocatedRange.AutoFitColumns();

                MailMessage mail = new MailMessage();
                SmtpClient smtpServer = new SmtpClient(myserver);
                smtpServer.Port = 25;


                mail.From = new MailAddress("norply@ex.com");
                mail.To.Add(email);
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) {
                    Attachment att;
                    workbook.SaveToStream(ms, FileFormat.Version2013);
                    ms.Flush();
                    att = new Attachment(ms, $"{DateTime.Now.ToString("yyyyMMdd")}.xlsx");
                    mail.Attachments.Add(att);
                    smtpServer.Send(mail);
                }

Sungkyu_min
 
Posts: 35
Joined: Fri Jan 07, 2022 5:53 am

Mon May 13, 2024 10:05 am

Hi,

Thanks for your message.
I tested the code below using our latest version and the stream save opens fine. If you are not testing with our latest version of Spire.XLS and Spire.Email, please refer to the link below to download the latest version and test again as our new version contains more fixes. If there is still a problem, please provide your Excel file for us to investigate further. Thanks in advance!

Spire.XLS Pack(Hotfix) Version:14.4.7: https://www.e-iceblue.com/Download/down ... t-now.html
Spire.Email Pack(hot fix) Version:6.5.10: https://www.e-iceblue.com/Download/email-for-net.html

Code: Select all
Workbook workbook = new Workbook();
workbook.Version = ExcelVersion.Version2013;
workbook.LoadFromFile("sample.xlsx");

MailMessage mail = new MailMessage("From@domain.com", "To@domain.com");
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
    Attachment att;
    workbook.SaveToStream(ms, Spire.Xls.FileFormat.Version2013);
    ms.Flush();
    att = new Attachment(ms, $"{DateTime.Now.ToString("yyyyMMdd")}.xlsx");
    mail.Attachments.Add(att);
    mail.Save("AddAttachments.eml", MailMessageFormat.Eml);
}


Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 334
Joined: Mon Nov 07, 2022 8:10 am

Tue May 14, 2024 12:15 am

Hello.

The xls I'm using is the latest version, 14.4.7. However, I'm using System.Net.Mail instead of spire.Email.

It seems that I'm encountering errors when attaching files in stream format using System.Net.Mail and Spire.xls
the issue is not related to the data, as even when only inserting `worksheet["A1"].Text = "?asd;` instead of a datatable and saving, the same problem occurs.
Could you please check this?

Sungkyu_min
 
Posts: 35
Joined: Fri Jan 07, 2022 5:53 am

Tue May 14, 2024 10:05 am

Hi,

Thank you for your feedback.
We will investigate this issue further and get back to you as soon as possible!

Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 334
Joined: Mon Nov 07, 2022 8:10 am

Wed May 15, 2024 3:31 am

Hi,

Thanks for your patience.
After conducting thorough testing, I was able to replicate the issue you have encountered. However, when I utilize the code below to output the stream as a file, the XLSX file opens without any issues. Furthermore, the code I had previously shared also demonstrated that the file opens properly when it is attached to a draft email as a stream.
Based on this observation, I suspect that the problem you're experiencing might be due to data changes occurring during email transfer. You can verify this by employing the method I have outlined above.
If you have any alternative hypotheses or if you believe this to be a defect in our product, please do not hesitate to inform us. We appreciate your feedback and are committed to resolving any issues you may encounter. Thank you for your patience and support!

using (var fileStream = File.Create("test2.xlsx"))
{
ms.Seek(0, SeekOrigin.Begin);
ms.CopyTo(fileStream);
}

Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 334
Joined: Mon Nov 07, 2022 8:10 am

Return to Spire.XLS