簡體   English   中英

如何使用格式將Excel工作表復制到電子郵件正文?

[英]How to copy an Excel worksheet to an e-mail body with formatting?

我正在服務器上創建一個控制台應用程序,它將把MS Excel文件的內容(帶有顏色和格式)復制到電子郵件的正文中。 我不是在嘗試附加工作表,而只是將其復制,以便業務用戶在手機/平板電腦/設備上查看電子郵件時,不需要安裝Excel應用程序即可查看報告。

在弄清楚如何在電子郵件正文中復制和顯示工作表時,我需要幫助。

目前,我有以下內容,但它僅復制實際的字符串數據,而沒有任何漂亮的格式:

public static void pullDataFromExcel(string fileName)
{
    string mySheetPath = @"C:\Users\lmilligan\Downloads\";
    MSExcel.Application excelApp = new MSExcel.Application();
    excelApp.DisplayAlerts = false;
    excelApp.Visible = true;
    MSExcel.Workbook book = excelApp.Workbooks.Open(mySheetPath + fileName);
    MSExcel.Worksheet sheet = book.ActiveSheet;
    book.RefreshAll();
    var data = "";
    foreach (MSExcel.Range row in sheet.UsedRange.Rows)
    {
        foreach (MSExcel.Range cell in row.Columns)
        {
            data += cell.Value + "  ";
        }
        data += "\n";
    }
    MSOutlook.Application olApp = new MSOutlook.Application();
    MailMessage mail = new MailMessage("email@myServer.com", "thisIsMe@myServer.com");
    SmtpClient client = new SmtpClient();
    client.Port = 25;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Host = "mail.myServer.net";
    mail.Subject = "AutoMailer test";
    mail.Body = Convert.ToString(sheet);
    client.Send(mail);

    book.Save();
    book.Close();
    excelApp.Quit();
}

static void Main(string[] args)
{
    pullDataFromExcel("mySheet.xlsx");

}

並不是您要找的確切答案,但是如果您可以使用PDF附件,那么在將活動工作表導出為包含格式的PDF文件之前,我已經使用了類似的方法:

Sub PDFMail()
  Dim IsCreated As Boolean
  Dim i As Long
  Dim PdfFile As String
  Dim OutlApp As Object

  ' Define PDF filename
  PdfFile = ActiveWorkbook.FullName
  i = InStrRev(PdfFile, ".")
  If i > 1 Then PdfFile = Left(PdfFile, i - 1)
  PdfFile = PdfFile & FName & ".pdf"

  ' Export activesheet as PDF
  With ActiveSheet
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  End With

  ' Use already open Outlook if possible
  On Error Resume Next
  Set OutlApp = GetObject(, "Outlook.Application")
  If Err Then
    Set OutlApp = CreateObject("Outlook.Application")
    IsCreated = True
  End If
  OutlApp.Visible = True
  On Error GoTo 0


  ' Prepare e-mail with PDF attachment
  With OutlApp.CreateItem(0)

    ' Prepare e-mail
    .Subject = "SUBJECT HERE"
    .To = "TO HERE"
    .CC = "CC HERE
    .BCC = "BCC HERE"
    .Body = "BODY TEXT HERE"
    .Attachments.Add PdfFile

    ' Try to send
    On Error Resume Next
    .Send
    Application.Visible = True
    If Err Then
      MsgBox "E-mail was not sent", vbExclamation
    Else
      MsgBox "E-mail successfully sent", vbInformation
    End If
    On Error GoTo 0

  End With

  ' Delete PDF file
  Kill PdfFile

  ' Quit Outlook if it was created by this code
  If IsCreated Then OutlApp.Quit

  ' Release the memory of object variable
  Set OutlApp = Nothing

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM