Outlook-将电子邮件导出到Excel-每个文件夹作为新工作表C#

Outlook-将电子邮件导出到Excel-每个文件夹作为新工作表C#,outlook,vsto,outlook-addin,office-addins,excel-addins,Outlook,Vsto,Outlook Addin,Office Addins,Excel Addins,我有以下代码,可以将电子邮件从共享邮箱的每个文件夹导出到Excel文件。目前,它为每个文件夹创建一个Excel文件,但我想将其更改为为为每个文件夹创建一个工作表,并将所有工作表合并到一个Excel文件中 Outlook.Application application = Globals.ThisAddIn.Application; Outlook.NameSpace ns = application.GetNamespace("MAPI");

我有以下代码,可以将电子邮件从共享邮箱的每个文件夹导出到Excel文件。目前,它为每个文件夹创建一个Excel文件,但我想将其更改为为为每个文件夹创建一个工作表,并将所有工作表合并到一个Excel文件中

            Outlook.Application application = Globals.ThisAddIn.Application;
            Outlook.NameSpace ns = application.GetNamespace("MAPI");

            Excel.Application oApp = null;
            Excel.Workbook oWB = null;
            Excel.Worksheet oSheet = null;
            oApp = new Excel.Application();
            oWB = oApp.Workbooks.Add();
            string recipientName = "xxxxxx@XXXXXXX.com";
            Outlook.Recipient recip = ns.CreateRecipient(recipientName);
            recip.Resolve();
            if (recip.Resolved)
            {
                Outlook.MAPIFolder folderContacts = ns.GetSharedDefaultFolder(recip, Outlook.OlDefaultFolders.olFolderInbox);
                Outlook.Folder allFolder = folderContacts.Parent;
                    foreach (Outlook.Folder rfolder in allFolder.Folders)
                    {
                        if (rfolder.Name.Contains("In Progress"))
                        {
                            Outlook.UserProperties MailUserProperties = null;
                            Outlook.UserProperty MailUserProperty = null;

                            oSheet = (Excel.Worksheet)oWB.Worksheets.get_Item(1);
                            oSheet.Name = rfolder.Name;
                            Excel.Range last = oSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
                            DateTime startDate = new DateTime(2019, 10, 1);
                            DateTime lastDate = new DateTime(2019, 10, 5);
                            string filter = "[ReceivedTime] >= '" + startDate.ToString("g") + "' AND [ReceivedTime] <= '" + lastDate.ToString("g") + "'";
                            Outlook.Items restrictedMails = rfolder.Items.Restrict(filter);

                            int row = 2;
                            int column = 1;
                            Excel.Range range = (Excel.Range)oSheet.Cells[row, column];

                            oSheet.Cells[1, 1] = "Received Date";
                            oSheet.Cells[1, 2] = "Sender";
                            oSheet.Cells[1, 3] = "Subject";
                            oSheet.Cells[1, 4] = "Category";
                            try
                            {
                                for (int i = 1; i <= restrictedMails.Count; i++)
                                {
                                    Outlook.MailItem mail = restrictedMails[i];
                                    MailUserProperties = mail.UserProperties;
                                    range.Cells[i, 1] = mail.ReceivedTime;
                                    range.Cells[i, 2] = mail.Sender;
                                    range.Cells[i, 3] = mail.Subject;
                                    range.Cells[i, 4] = mail.Categories;
                                }
                                oSheet.UsedRange.Columns.AutoFit();
                            }

                            catch (System.Runtime.InteropServices.COMException ex)
                            {
                                Console.WriteLine(ex.ToString());
                            }
                        }
                    }

                SaveFileDialog saveFileDialogue = new SaveFileDialog();
            }
Outlook.Application=Globals.ThisAddIn.Application;
Outlook.NameSpace ns=application.GetNamespace(“MAPI”);
Excel.Application oApp=null;
Excel.Workbook oWB=null;
Excel.Worksheet oSheet=null;
oApp=新的Excel.Application();
oWB=oApp.Workbooks.Add();
字符串recipientName=”xxxxxx@XXXXXXX.com";
Outlook.Recipient recip=ns.CreateRecipient(recipientName);
recip.Resolve();
如果(交互解决)
{
Outlook.MapipFolder folderContacts=ns.GetSharedDefaultFolder(recip,Outlook.OldDefaultFolders.olFolderInbox);
Outlook.Folder allFolder=folderContacts.Parent;
foreach(allFolder.Folders中的Outlook.Folder rfolder)
{
if(rfolder.Name.Contains(“进行中”))
{
Outlook.UserProperties MailUserProperties=null;
Outlook.UserProperty MailUserProperty=null;
oSheet=(Excel.Worksheet)oWB.Worksheets.get_项(1);
oSheet.Name=rfolder.Name;
Excel.Range last=oSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell,Type.Missing);
DateTime startDate=新的日期时间(2019,10,1);
DateTime lastDate=新的日期时间(2019年10月5日);

string filter=“[ReceivedTime]>=”+startDate.ToString(“g”)+“”和[ReceivedTime]您只需使用
for
循环,而不是
foreach

            Outlook.Application application = Globals.ThisAddIn.Application;
            Outlook.NameSpace ns = application.GetNamespace("MAPI");

            Excel.Application oApp = null;
            Excel.Workbook oWB = null;
            Excel.Worksheet oSheet = null;
            oApp = new Excel.Application();
            oWB = oApp.Workbooks.Add();
            string recipientName = "xxxxxx@XXXXXXX.com";
            Outlook.Recipient recip = ns.CreateRecipient(recipientName);
            recip.Resolve();
            if (recip.Resolved)
            {
                Outlook.MAPIFolder folderContacts = ns.GetSharedDefaultFolder(recip, Outlook.OlDefaultFolders.olFolderInbox);
                Outlook.Folder allFolder = folderContacts.Parent;
                Outlook.Folders allFolders = allFolder.Folders;
                    for(int i =1; i<= allFolders.Count; i++)
                    {
                        Outlook.Folder rfolder = allFolders[i];

                        if (rfolder.Name.Contains("In Progress"))
                        {
                            Outlook.UserProperties MailUserProperties = null;
                            Outlook.UserProperty MailUserProperty = null;

                            oSheet = (Excel.Worksheet)oWB.Worksheets.Add();
                            ' or just check whether such worksheet exists before adding a new one


                            oSheet.Name = rfolder.Name;
                            Excel.Range last = oSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
                            DateTime startDate = new DateTime(2019, 10, 1);
                            DateTime lastDate = new DateTime(2019, 10, 5);
                            string filter = "[ReceivedTime] >= '" + startDate.ToString("g") + "' AND [ReceivedTime] <= '" + lastDate.ToString("g") + "'";
                            Outlook.Items restrictedMails = rfolder.Items.Restrict(filter);

                            int row = 2;
                            int column = 1;
                            Excel.Range range = (Excel.Range)oSheet.Cells[row, column];

                            oSheet.Cells[1, 1] = "Received Date";
                            oSheet.Cells[1, 2] = "Sender";
                            oSheet.Cells[1, 3] = "Subject";
                            oSheet.Cells[1, 4] = "Category";
                            try
                            {
                                for (int i = 1; i <= restrictedMails.Count; i++)
                                {
                                    Outlook.MailItem mail = restrictedMails[i];
                                    MailUserProperties = mail.UserProperties;
                                    range.Cells[i, 1] = mail.ReceivedTime;
                                    range.Cells[i, 2] = mail.Sender;
                                    range.Cells[i, 3] = mail.Subject;
                                    range.Cells[i, 4] = mail.Categories;
                                }
                                oSheet.UsedRange.Columns.AutoFit();
                            }

                            catch (System.Runtime.InteropServices.COMException ex)
                            {
                                Console.WriteLine(ex.ToString());
                            }
                        }
                    }

                SaveFileDialog saveFileDialogue = new SaveFileDialog();
            }

Outlook.Application=Globals.ThisAddIn.Application;
Outlook.NameSpace ns=application.GetNamespace(“MAPI”);
Excel.Application oApp=null;
Excel.Workbook oWB=null;
Excel.Worksheet oSheet=null;
oApp=新的Excel.Application();
oWB=oApp.Workbooks.Add();
字符串recipientName=”xxxxxx@XXXXXXX.com";
Outlook.Recipient recip=ns.CreateRecipient(recipientName);
recip.Resolve();
如果(交互解决)
{
Outlook.MapipFolder folderContacts=ns.GetSharedDefaultFolder(recip,Outlook.OldDefaultFolders.olFolderInbox);
Outlook.Folder allFolder=folderContacts.Parent;
Outlook.Folders allFolders=allFolder.Folders;

对于(int i=1;它工作得很好。非常感谢。我唯一要做的更改是将
Outlook.Folder rfolder=allFolders[i];
更改为
Outlook.MAPIFolder rfolder=allFolders[i];