Outlook - 将电子邮件导出到 Excel - 每个文件夹作为新工作表 C#
Outlook - Exporting emails to Excel - Each folder as new worksheet C#
我有以下代码可以将电子邮件从共享邮箱的每个文件夹导出到 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();
}
提前致谢。
你只需要使用 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();
}
我有以下代码可以将电子邮件从共享邮箱的每个文件夹导出到 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();
}
提前致谢。
你只需要使用 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();
}