web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Error in exporting data to memory stream

(0) ShareShare
ReportReport
Posted on by

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)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans