我有以下代码,该代码会将电子邮件从共享邮箱的每个文件夹导出到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 = "[email protected]";
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 = "[email protected]";
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();
}