
Hi,
How to export data in table to memory stream, i tried like below.But it getting some error,I am unable to identify it.
dTable.TableName = "Open Opportunities";
var stream = Helper.ExportAllData(dTable, null) as MemoryStream;
System.Text.Encoding encoding1 = System.Text.Encoding.UTF8;
byte[] bytes = stream.ToArray();
Entity attachment = new Entity("activitymimeattachment");
attachment["subject"] = "Open Opportunities for the day";
attachment["filename"] = "Open_Opportunities_" + DateTime.Now.ToString("MM/dd/yyyy") + ".xlsx";
byte[] fileStream = bytes;
attachment["body"] = Convert.ToBase64String(fileStream);
attachment["mimetype"] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
attachment["attachmentnumber"] = 1;
attachment["objectid"] = new EntityReference(emailToAttach.LogicalName, emailToAttach.Id);
attachment["objecttypecode"] = "email";
service.Create(attachment);
stream.Close();
internal static Stream ExportAllData(DataTable dt, Stream stream = null)
{
using (var excelpackage = new ExcelPackage(stream ?? new MemoryStream()))
{
excelpackage.Workbook.Properties.Author = "CRM Administrator";
excelpackage.Workbook.Properties.Title = "Report";
excelpackage.Workbook.Properties.Comments = "This is an Exported Excel with CRM Data";
// set some extended property values
excelpackage.Workbook.Properties.Company = "xyz";
excelpackage.Workbook.Worksheets.Add("Report");
var worksheet = excelpackage.Workbook.Worksheets[1];
worksheet.Cells[1, 1].LoadFromDataTable(dt, true);
//Create an autofilter for the range
//worksheet.Cells["A1:D" + (dt.Rows.Count + 1)].AutoFilter = true;
worksheet.Cells.AutoFitColumns(); //Autofit columns for all cells
//Ok now format the values;
using (var range = worksheet.Cells[1, 1, 1, dt.Columns.Count])
{
range.Style.Font.Bold = true;
range.Style.Fill.PatternType = ExcelFillStyle.Solid;
range.Style.Fill.BackgroundColor.SetColor(Color.DarkBlue);
range.Style.Font.Color.SetColor(Color.White);
//range.Style.Locked = false;
range.Style.Border.Top.Style = ExcelBorderStyle.Thin;
range.AutoFilter = true;
}
// lets set the header text
worksheet.HeaderFooter.OddHeader.CenteredText = "Hudoud Alteqnia Daily Report";
// add the page number to the footer plus the total number of pages
worksheet.HeaderFooter.OddFooter.RightAlignedText =
string.Format("Page {0} of {1}", ExcelHeaderFooter.PageNumber, ExcelHeaderFooter.NumberOfPages);
// add the sheet name to the footer
worksheet.HeaderFooter.OddFooter.CenteredText = ExcelHeaderFooter.SheetName;
// add the file path to the footer
worksheet.HeaderFooter.OddFooter.LeftAlignedText = ExcelHeaderFooter.FilePath + ExcelHeaderFooter.FileName;
//Set worksheet protection attributes
worksheet.Protection.AllowInsertRows = false;
worksheet.Protection.AllowSort = true;
//worksheet.Protection.AllowSelectUnlockedCells = false;
worksheet.Protection.AllowAutoFilter = true;
//worksheet.Protection.IsProtected = true;
excelpackage.Save();
return excelpackage.Stream;
}
Please help it.
How to create attachment with data in excel to send mail.
*This post is locked for comments
I have the same question (0)