excel 打开后的内容:
思路:
1、邮件发送excel 是作为附件发送出去的;
2、excel 预览是,必须另外点击预览按钮,并不能直接预览邮件内容然后在邮件主体内容展示出来
根据以上两点基本没法实现 邮件发送后邮件自带 预览功能。
伪方法:
1、在邮件生成时把邮件要预览的部分粘贴成 内容动态图片
2、在邮件生成后读取邮件里面的图片
3、把图片一高清方式设置按比例设置大小,这样图片大小是等比放大,这样不会变窄变长
4、邮件发送是把刚才获取的图片添加到邮件正文当中,从而达到类似预览的效果
步骤如下:
1、把excel 要发送出去的内容区域选中,复制后选中 连接的图片,生成内容是动态的图片2、在邮件生成后读取邮件里面的图片
#region Spire.XLS 获取img
Workbook workbook = new Workbook();
workbook.LoadFromFile(filePath);
//获取工作表
Worksheet sheet = workbook.Worksheets["汇总分析"];
Dictionary<string, string> keyValuesImg = new Dictionary<string, string>();
foreach (Spire.Xls.ExcelPicture picture in sheet.Pictures)
{/*Jpg:压缩,Png:无损压缩,Bmp:无压缩*/Image image = picture.Picture;MemoryStream ms = new MemoryStream();image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);picture.Picture.Save("C:\\Image" + picture.Name + ".PNG", System.Drawing.Imaging.ImageFormat.Png);picture.Picture.Save($@"{AppDomain.CurrentDomain.BaseDirectory}ExcelReport\OEE\OEE报表\Image" + picture.Name + ".PNG", System.Drawing.Imaging.ImageFormat.Png);if (!keyValuesImg.ContainsKey(picture.Name.Trim())){keyValuesImg.Add(picture.Name.Trim(), Convert.ToBase64String(ms.ToArray()));}//Image image = picture.Picture;//MemoryStream ms = new MemoryStream();设置质量//EncoderParameters encoderParameters = new EncoderParameters(1);//encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);//ImageCodecInfo jpgEncoder = GetImageEncoder(ImageFormat.Bmp);保存//image.Save(ms, jpgEncoder, encoderParameters);//baseimg = Convert.ToBase64String(ms.ToArray());//picture.Picture.Save("C:\\Image" + picture.Name + ".Bmp", jpgEncoder, encoderParameters);}
workbook.Dispose();#endregion
3、设置图片等比大小和质量
/*Jpg:压缩,Png:无损压缩,Bmp:无压缩*/
//设置质量
EncoderParameters encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
ImageCodecInfo jpgEncoder = GetImageEncoder(ImageFormat.Bmp);
4、把图片添加到邮件正文当中,并发送邮件
#region SendEmail/*创建 HTML 内容,将图片嵌入其中*/
string htmlBody = "<html><body>";
htmlBody += @"<p style='font-size:14px; font-weight:bold'>Dear All,</p>";
htmlBody += @"<p style='font-size:14px;'>  以下是当月设备OEE Report,请查阅<a href='https://xxxxxxx.com.hk/Home/Report?type=OEEReport'>【Dashboard】</a>,谢谢!</p>";
htmlBody += @"<p style='font-size:14px;'>  如需详细了解请查看附件明细数据,或 公共盘查询:<a href='file://filesvr02/Dashboard/OEE'>\\filesvr02\Dashboard\OEE</a></p>";
foreach (var entry in keyValuesImg)
{htmlBody += string.Format("<p><img src='data:image/png;base64,{0}' /></p>", entry.Value.ToString());
}
htmlBody += @"</body></html>";/*文件流发送*/
MemoryStream memoryStream = new MemoryStream(File.ReadAllBytes(filePath));
// Create a StreamAttachment from the memory stream
List<StreamAttachment> attachment = new List<StreamAttachment>()
{new StreamAttachment{stream = memoryStream,name = $@"OEE-Report-{DateTime.Now.AddDays(-1).ToString("M-d")}.xlsx",mediaTyp = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}
};
EmailHelper.SendToEmail(listemai1, listemai2, "OEE-Report", htmlBody.ToString(), true, attachment, "Dashboard");
memoryStream.Dispose();#endregion
效果: