Please read the
DataRow.ItemArray[
^] which will show you how to fetch data from
the ItemArray and
Please revisit your
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=offline_data.xls");
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/ms-excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
HttpContext.Current.Response.Write(stringWrite.ToString())
as it looks to me no need as your sending a email.
Hope it helps :)
As you requested, please the comment next to code,
private void ATestMethod()
{
int i_result = 0;
DataTable dtResult = Fetching_Query_Data_In_Datatable(strDatatype, from, to, strHscode, strComb, strProd);
StringBuilder dataToExport = new StringBuilder();
dataToExport.Append("<table style=border:solid 1px blue;>");
dataToExport.Append("<tr>");
foreach (DataColumn dCol in dtResult.Columns)
{
dataToExport.Append("<td style=border:solid 1px blue;>");
dataToExport.Append(HttpContext.Current.Server.HtmlEncode(dCol.ColumnName));
dataToExport.Append("</td>");
}
dataToExport.Append("</tr>");
foreach (DataRow dRow in dtResult.Rows)
{
dataToExport.Append("<tr>");
foreach (object obj in dRow.ItemArray)
{
dataToExport.Append("<td style=border:solid 1px blue;>");
dataToExport.Append(HttpContext.Current.Server.HtmlEncode(obj.ToString()));
dataToExport.Append("</td>");
}
dataToExport.Append("</tr>");
}
dataToExport.Append("</table>");
if (!string.IsNullOrEmpty(dataToExport.ToString()))
{
string strSub = "Offline data";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=offline_data.xls");
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/ms-excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter(dataToExport);
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
HttpContext.Current.Response.Write(stringWrite.ToString());
SmtpClient mailcleint = new SmtpClient();
string SenderId = EmailTo.ToString().Trim();
if (SenderId != "")
{
string SenderName = "Team Cybex";
MailAddress MailRecepient = new MailAddress(EmailTo.ToString().Trim());
MailAddress MailSender = new MailAddress(SenderId, SenderName);
MailMessage MailMsg = new MailMessage(MailSender, MailRecepient);
MailMsg.Body = "Hello";
MailMsg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
MailMsg.IsBodyHtml = true;
var a = System.Net.Mail.Attachment.CreateAttachmentFromString(stringWrite.ToString(), "application/vnd.xls");
MailMsg.Attachments.Add(a);
MailMsg.Priority = MailPriority.Normal;
MailMsg.Subject = strSub.ToString();
SmtpClient mailclient = new SmtpClient();
mailclient.Credentials = new System.Net.NetworkCredential("myID", "my password");
mailclient.Host = "xxxx.xx.xx.xx";
mailclient.Send(MailMsg);
i_result = 1;
}
HttpContext.Current.Response.End();
}
Should work (need to provide valid email address stuff):)