2017-03-14 102 views
1

我想创建一个C#控制台应用程序(或服务 - 不知道如何开发服务还)说:展望新的电子邮件到Excel列

1)知道,当一个新的电子邮件收件箱中收到> LOTALogs夹。此电子邮件由移动应用程序发送,包含附件和客户遇到的一些问题。

2)采用逗号分隔的新电子邮件内容,将内容解析并附加到已设置列的Excel工作表中。

我设法建立:

1)解析器:

public static string[] emailContentsArray() 
    { 
     string content = "Username = Customer1,User ID = 362592,Unit ID = 805618,Date = Mar 12, 2017,Device = Android LGE LG-H990,OS version = 7.0 (API 24),App version = 1.0.0.56,Description = some description,Message = some message"; 
     string[] contentArray = content.Split(','); 

     // Case where date format includes another comma 
     if (contentArray.Length > 10) 
     { 
      // Parsing headers 
      contentArray[0] = contentArray[0].Substring(11); 
      contentArray[1] = contentArray[1].Substring(10); 
      contentArray[2] = contentArray[2].Substring(10); 
      contentArray[3] = contentArray[3].Substring(7) + ", " + contentArray[4].Substring(1); 
      contentArray[4] = contentArray[5].Substring(9); 
      contentArray[5] = contentArray[6].Substring(13); 
      contentArray[6] = contentArray[7].Substring(14); 
      contentArray[7] = contentArray[8].Substring(14); 
      contentArray[8] = contentArray[9].Substring(10); 
      contentArray[9] = null; 

      for (int i = 0; i < contentArray.Length; i++) 
      { 
       Console.Write(contentArray[i] + ","); 
      } 
     } 
     //else 
     //{ 

     //} 
     return contentArray; 
    } 

2)访问的文件夹,并计数的项目数:)打开

public static string[] emailContent() 
    { 
     string[] content = null; 
     Microsoft.Office.Interop.Outlook.Application app = null; 
     Microsoft.Office.Interop.Outlook.NameSpace ns = null; 
     Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null; 
     Microsoft.Office.Interop.Outlook.MAPIFolder logFolder = null; 

     app = new Microsoft.Office.Interop.Outlook.Application(); 
     ns = app.GetNamespace("MAPI"); 
     inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); 
     logFolder = app.ActiveExplorer().CurrentFolder = inboxFolder.Folders["LOTALogs"]; 
     int itemCount = logFolder.Items.Count; 

     Console.WriteLine("\n\nFolder Name: {0}, Num Items: {1}\n", logFolder.Name, itemCount); 

     return content; 
    } 

3和打印电子表格的内容:

 Excel.Application xlApp = new Excel.Application(); 
     string path = "C:\\SomeUser\\BugReports"; 
     Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@path); 
     Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1]; 
     Excel.Range xlRange = xlWorksheet.UsedRange; 

     for (int i = 1; i <= xlRange.Row + xlRange.Rows.Count - 1; i++) 
     { 
      for (int j = 1; j <= xlRange.Column + xlRange.Columns.Count - 1; j++) 
      { 
       if (j == 1) 
        Console.Write("\r\n"); 

       if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null) 
        Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t"); 
      } 
     } 
     xlWorkbook.Save(); 
     xlWorkbook.Close(); 
     xlApp.Quit(); 

     Console.ReadLine(); 

我有点现在失去了:)

我还需要:

1)创建一个事件侦听器(我认为这就是它叫什么),所以我可以告诉邮件正文分析器去获取电子邮件内容。

2)从电子邮件中提取电子邮件正文。

得到这个使用

Console.WriteLine(logFolder.Items[1].Body); 

3)以电子邮件的内容,并把它添加到电子表格。

4)我应该创建一个Windows服务?

PS - 我不是开发人员,只是摆弄代码并尽可能地提高效率。当有技术解决方案即将出现时,我不想手动填写此电子表格。如果您对代码的效率更高以及模式有不同的建议,请发表评论意见。

回答

2

看起来很实在。我会避免使用这项服务,但这很大程度上取决于您的用户。除非您的客户真的想在整个过程中“失明”,否则会增加许多不必要的麻烦。

至于附加到电子表格...

int lastRow = xlWorksheet.UsedRange.Rows; 
Excel.Range xlRange = xlWorksheet.Cells[lastRow + 1, 1]; 
xlRange.Value = stuffFromInbox; 

如果你只添加一个项目到电子表格,这将正常工作。对于使用电子表格进行批量读/写操作(例如“打开并打印电子表格的内容”),将整个RangeValueValue2读取到object[,]会更有效率。然后遍历本地数组。