Witam,
Potrzebuję stworzyć pliki z rozszerzeniem .xlsx oraz .zip, następnie .xlsx wrzucić do archiwum i wysłać jako załącznik w mailu. Sam plik .xlsx udało mi się wysłać natomiast po dodaniu .xlsx do archiwum wysyłam plik .zip ważący 300 B z nieprawidłowym rozszerzeniem . Byłbym wdzięczny za pomoc.
var streamExcel = new MemoryStream();
var streamZip = new MemoryStream();
var mail = new MailMessage();
string fileName = "Podmiot.xlsx";
try
{
string[] _excelColumns = { "col1", "col2" };
using var workbook = new XLWorkbook();
IXLWorksheet worksheet = workbook.Worksheets.Add("Arkusz1");
int number = 1;
foreach (string excelColumn in _excelColumns)
{
worksheet.Cell(1, number).Value = excelColumn;
number++;
}
worksheet.Cell(2, 1).Value = model.col1;
worksheet.Cell(2, 2).Value = model.col2;
string handle = Guid.NewGuid().ToString();
workbook.SaveAs(streamExcel);
streamExcel.Position = 0;
var attachmentStreamExcel = new MemoryStream(streamExcel.ToArray());
var attachment = new Attachment(attachmentStreamExcel, "Podmiot" + ".xlsx", "application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet");
using (var zipArchive = new ZipArchive(streamZip, ZipArchiveMode.Create))
{
ZipArchiveEntry zipArchiveEntry = zipArchive.CreateEntry("Podmiot.xlsx");
using (var streamWriter = new StreamWriter(zipArchiveEntry.Open()))
{
streamWriter.Write(attachment.ContentStream.ReadByte());
}
}
MemoryStream attachmentStreamZip = new MemoryStream(streamZip.ToArray());
var mailAttachmet = new Attachment(attachmentStreamZip, "Podmiot" + ".zip", MediaTypeNames.Application.Zip);
mail.Attachments.Add(mailAttachmet);