我发送此功能是为了在发送电子邮件时嵌入数据。所以我可以在电子邮件正文中调用此函数。
private static string getHTML(DataTable dt)
{
StringBuilder myBuilder = new StringBuilder();
myBuilder.AppendLine("see attached for the updated list.");
myBuilder.AppendLine("");
myBuilder.Append("<table border='1px' cellpadding='5' cellspacing='0' ");
myBuilder.Append("style='border: solid 1px Silver; font-size: x-small;'>");
myBuilder.Append("<tr align='left' valign='top'>");
foreach (DataColumn myColumn in dt.Columns)
{
myBuilder.Append("<td align='left' valign='top'>");
myBuilder.Append(myColumn.ColumnName);
myBuilder.Append("</td>");
}
myBuilder.Append("</tr>");
int a = 1;
foreach (DataRow myRow in dt.Rows)
{
myBuilder.Append("<tr align='left' valign='top'>");
foreach (DataColumn myColumn in dt.Columns)
{
myBuilder.Append("<td align='left' valign='top'>");
myBuilder.Append(myRow[myColumn.ColumnName].ToString());
myBuilder.Append("</td>");
}
myBuilder.Append("</tr>");
a++;
if (a > 4)
break;
}
myBuilder.Append("</table>");
return myBuilder.ToString();
}
但是当我尝试嵌入使用NPOI库(而不是Datatable)制作的excel文件时,我找不到合适的函数或任何东西来获取文件中的数据。
如何将sheet1数据嵌入电子邮件中?
这是我创建工作表的代码:
private void Email()
{
//get the data from database
DataTable data = GetData();
IWorkbook workbook;
workbook = new HSSFWorkbook();
ISheet sheet1 = workbook.CreateSheet("Sheet 1");
// [...]
}
这是发送电子邮件的代码:
private void email()
{
// [...]
using (var ms = new MemoryStream())
{
workbook.Write(ms);
ms.Position = 0;
using (MailMessage mm = new MailMessage())
{
mm.From = new MailAddress("abcd@gmail.com");
mm.Bcc.Add("abcd@gmail.com");
SmtpClient smtp = new SmtpClient();
mm.Subject = "Task List";
StringBuilder sb = new StringBuilder();
mm.Body = getHTML(data);
mm.Attachments.Add(new Attachment(ms, "Task.xls", "application/vnd.ms-excel"));
mm.IsBodyHtml = true;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
credentials.UserName = "abc@gmail.com";
credentials.Password = "1234";
smtp.UseDefaultCredentials = true;
smtp.Credentials = credentials;
smtp.Port = 587;
smtp.Send(mm);
}
}
}
workbook.RemoveSheetAt(0);
}
答案 0 :(得分:2)
答案 1 :(得分:1)
我认为您最好的选择是将WorkBook对象转换为html。您可以看到转化期望的结果in this article。您可以转换WorkBook的内容并将其嵌入到电子邮件中,如下所示:
private string ConvertXlsToHtml(IWorkbook workbook)
{
ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter();
// Set output parameters
excelToHtmlConverter.OutputColumnHeaders = false;
excelToHtmlConverter.OutputHiddenColumns = true;
excelToHtmlConverter.OutputHiddenRows = true;
excelToHtmlConverter.OutputLeadingSpacesAsNonBreaking = false;
excelToHtmlConverter.OutputRowNumbers = true;
excelToHtmlConverter.UseDivsToSpan = true;
// Process the Excel file
excelToHtmlConverter.ProcessWorkbook(workbook);
return excelToHtmlConverter.Document.ToString();
}
....
using (var ms = new MemoryStream())
{
workbook.Write(ms);
ms.Position = 0;
string mailBody = getHTML(data) + ConvertXlsToHtml(workbook);
using (MailMessage mm = new MailMessage())
{
mm.From = new MailAddress("abcd@gmail.com");
mm.Bcc.Add("abcd@gmail.com");
SmtpClient smtp = new SmtpClient();
mm.Subject = "Task List";
StringBuilder sb = new StringBuilder();
mm.Body = mailBody;
mm.Attachments.Add(new Attachment(ms, "Task.xls", "application/vnd.ms-excel"));
mm.IsBodyHtml = true;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
credentials.UserName = "abc@gmail.com";
credentials.Password = "1234";
smtp.UseDefaultCredentials = true;
smtp.Credentials = credentials;
smtp.Port = 587;
smtp.Send(mm);
}
}