发送附件中的xls文件 [英] Sending xls file in attachment
问题描述
嗨..
我的问题是我正在从数据库生成一些数据并将其保存在数据表中.
之后,我打算将其写入excel文件并下载.效果很好.
我的老板希望我添加一项功能,现在用户可以将数据邮寄给自己而不是下载数据.我正在使用相同的代码片段生成xcel文件并将其附加在邮件中.我有两个问题
1.附件为空的excel文件.
2.邮件发送后,它仍然会打开一个对话框,用于下载我现在不想要的文件.
这是我的代码,Plzzz帮我
Hi..
My prob is that I am generating some data from database and keeping it in data table.
After that i intend to write it into excel file and download it. it works fine.
My boss wants me to add a functionality where now a user can mail himself the data instead of downloading it. I am using same code snippet for genrating xcel file and attaching it in mail. there are two probs i am getting
1. The attachment comes empty excel file.
2. after mail goes it still opens a dialogue box for downloading the file which i dnt want now.
Here is my code,Plzzz help me
<pre lang="c#">
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();
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);
/******************** SEND Email TO Users **************************************/
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();
return i_result;
}
:-
推荐答案
请阅读 ^ ],它将向您展示如何从
获取数据 ItemArray和
请重新访问您的
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;
//IMP: Make sure you are getting data into dtResult
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); //Added 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);
/******************** SEND Email TO Users **************************************/
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(); //Need this line of code.
}
应该可以工作(需要提供有效的电子邮件地址):)
Should work (need to provide valid email address stuff):)
附件将为空:您仔细构建dataToExport
,检查它是否为空,然后忽略它.尝试以某种形式将其用作附件!
The attachment will be empty: you carefully build up dataToExport
,check it''s not empty, and then ignore it. Try using that as the attachment in some form!
这篇关于发送附件中的xls文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!