我尝试以pdf格式导出gridview并附加为附件 但是数据没有以pdf显示,但pdf中出现的列和行与 我的数据。This is the pdf I get as attachment in my gmail和附件发送邮件的代码如下所示
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
gvMisConduct.DataSource = Session["printdt"];
gvMisConduct.DataBind();
gvMisConduct.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(PageSize.A4, 10f, 10f, 10f, 10f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
mMailMessage.Body = "Sir," + Environment.NewLine + Environment.NewLine + Environment.NewLine;
mMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
// Set the format of the mail message body as HTML
mMailMessage.Attachments.Add(new Attachment(new MemoryStream(bytes), "GridViewPDF.pdf"));
// mMailMessage.IsBodyHtml = true;
// Set the priority of the mail message to normal
mMailMessage.Priority = System.Net.Mail.MailPriority.Normal;
// Instantiate a new instance of SmtpClient
System.Net.Mail.SmtpClient mSmtpClient = new System.Net.Mail.SmtpClient();
object MailSmtpServer = clsDBOperation.GetScalar("select value from Settings where Category='MailSmtpServer'");
if (MailSmtpServer == null)
{
lblMessage.Text = "Invalid MailSmtpServer";
return;
}
mSmtpClient.Host = MailSmtpServer.ToString();
mSmtpClient.Port = 25;
mSmtpClient.EnableSsl = true;
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(MailFromAddress.ToString(), PasswordDecode(MailPassword.ToString()));
mSmtpClient.UseDefaultCredentials = false;
mSmtpClient.Credentials = SMTPUserInfo;
// string[] to = txtTo.Text.Split(';');
object Email;
// Email = clsDBOperation.GetScalar("select (select Email from School_ParentMaster where School_ParentMaster.AccountID=Accounts.Parent) Email from Accounts where Accounts.ID='" + hdnAccountId.Value + "'");
Email = ToAddress;
if (Email != null)
{
// mMailMessage.To.Add(new System.Net.Mail.MailAddress(Email.ToString()));
mMailMessage.To.Add(ToAddress.ToString());
mSmtpClient.Send(mMailMessage);
}
// Send the mail message
// mSmtpClient.Send(mMailMessage);
lblMessage.Text = "Message has been sent";
// clearall();
// ScriptManager.RegisterStartupScript(this, typeof(string), "Mail", "window.close();", true);
lblMessage.Text = "Send successfully";
clearAll();
//Response.Redirect("Behaviour.aspx?Class=" + dlClass.SelectedItem.Text + "&Division=" + dlDivision.SelectedItem.Text + "&Type=Save", false);
}
}
}
}
如何以pdf格式显示内容?请帮忙