C# 如何将Excel工作表复制到具有格式的电子邮件正文中?

C# 如何将Excel工作表复制到具有格式的电子邮件正文中?,c#,.net,oop,visual-studio-2013,html-email,C#,.net,Oop,Visual Studio 2013,Html Email,我正在服务器上创建一个控制台应用程序,它将MS Excel文件的内容(带有颜色和格式)复制到电子邮件正文中。我不想附加工作表,只是简单地复制它,这样当业务用户在手机/平板电脑/设备上查看电子邮件时,他们就不需要安装Excel应用程序来查看报告 我需要帮助了解如何在电子邮件正文中复制和显示工作表 目前我有以下内容,但它只复制实际的字符串数据,没有任何漂亮的格式: public static void pullDataFromExcel(string fileName) { string m

我正在服务器上创建一个控制台应用程序,它将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");

}
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

这不是您想要的确切答案,但是如果您可以使用PDF附件,那么我以前使用过类似的方法将活动工作表导出为PDF文件,包括格式:

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");

}
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

这不是您想要的确切答案,但是如果您可以使用PDF附件,那么我以前使用过类似的方法将活动工作表导出为PDF文件,包括格式:

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");

}
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

它需要在体内吗?作为附件,它可能更方便用户使用?您可以在HTML表格中显示数据,使其具有类似excel的格式。是的,它必须位于正文中,以便业务用户在其移动设备上查看报告时,不需要安装excel。pdf呢?您可以根据需要格式化数据,用户将能够完全按照您的预期查看数据。显然还有一点工作要做,但对用户来说是一个更好的“体验”..PDF将起作用-问题是将Excel报告作为电子邮件的正文。它需要在正文中吗?作为附件,它可能更方便用户使用?您可以在HTML表格中显示数据,使其具有类似excel的格式。是的,它必须位于正文中,以便业务用户在其移动设备上查看报告时,不需要安装excel。pdf呢?您可以根据需要格式化数据,用户将能够完全按照您的预期查看数据。显然还有一点工作要做,但对用户来说“体验”要好得多..PDF就行了-问题是将Excel报告作为电子邮件的主体。这当然是Excel文件中的原始VBA。我相信有经验丰富的c语言人员可以帮助您将其转换为服务器应用程序,或者您可以将VBA代码存储在excel文件中,并从控制台代码调用sub…?感谢您的输入!:-)这当然是Excel文件中的原始VBA。我相信有经验丰富的c语言人员可以帮助您将其转换为服务器应用程序,或者您可以将VBA代码存储在excel文件中,并从控制台代码调用sub…?感谢您的输入!:-)