2015-10-05 63 views
1

我在服務器上創建一個控制檯應用程序,該應用程序會將MS Excel文件(包含顏色和格式)的內容複製到電子郵件的正文中。我不是試圖附上工作表,而只是複製它,以便業務用戶在他們的手機/平板電腦/設備上查看電子郵件時,他們不需要安裝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 protected]", "[email protected]"); 
    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"); 

} 
+0

是否需要位於正文中?它可能更多_user friendly_作爲附件? – christiandev

+3

您可以在HTML表格中顯示數據以獲得類似於Excel的格式。 – markpsmith

+0

是Christiandev它必須位於主體中,以便業務用戶在移動設備上查看報告時,不需要安裝Excel。 –

回答

0

不是你正在尋找確切的答案,但如果你可以用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 
+0

這當然是Excel文件中的原始VBA。我相信有人對c#有更多的經驗可以幫助你如何將它轉化爲你的服務器應用程序,或者你可以將VBA代碼存儲在你的excel文件中並從控制檯代碼中調用子代碼。 –

+0

感謝您的輸入! :-) –

相關問題