使用C#将Outlook中的邮件内容分解并导入到Excel中

昨天接到一个需求,要从Outlook2003中读取某类用户调查邮件,将正文中用户所填的问题和答案进行分析,将内容导入Excel中。今天花了一天时间写好了,很简单的功能,代码贴出来给需要的人参考吧。
demo1.gif

代码如下:

ContractedBlock.gif ExpandedBlockStart.gif
None.gifusing System;
None.gif
using System.Drawing;
None.gif
using System.Collections;
None.gif
using System.ComponentModel;
None.gif
using System.Windows.Forms;
None.gif
None.gif
namespace MailAnalyze
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for Mainform.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Mainform : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private System.Windows.Forms.Button ConnectButton;
InBlock.gif        
private System.Windows.Forms.ListBox MailFolderList;
InBlock.gif        
private bool connOutlook = false;
InBlock.gif        
private System.Windows.Forms.TextBox FilterBox;
InBlock.gif        
private System.Windows.Forms.Label label1;
InBlock.gif        
private System.Windows.Forms.Label label2;
InBlock.gif        
private ArrayList maillist = new ArrayList();
InBlock.gif        
private System.Windows.Forms.TabControl tabControl1;
InBlock.gif        
private System.Windows.Forms.TabPage tabPage1;
InBlock.gif        
private System.Windows.Forms.TabPage tabPage2;
InBlock.gif        
private System.Windows.Forms.TextBox BodyBox;
InBlock.gif        
private System.Windows.Forms.Button ExportBtn;
InBlock.gif        
private System.Windows.Forms.ListView QuestionListView;
InBlock.gif        
private System.Windows.Forms.ColumnHeader columnHeader1;
InBlock.gif        
private System.Windows.Forms.ColumnHeader columnHeader3;
InBlock.gif        
private System.Windows.Forms.ColumnHeader columnHeader4;
InBlock.gif        
private System.Windows.Forms.ColumnHeader columnHeader5;
InBlock.gif        
private System.Windows.Forms.Button QuitBtn;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required designer variable.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        
public Mainform()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// Required for Windows Form Designer support
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            MailFolderList.Items.Clear();
InBlock.gif            label2.Text 
= "";
InBlock.gif
InBlock.gif            
//
InBlock.gif            
// TODO: Add any constructor code after InitializeComponent call
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Clean up any resources being used.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(components != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Windows Form Designer generated code#region Windows Form Designer generated code
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required method for Designer support - do not modify
InBlock.gif        
/// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.ConnectButton = new System.Windows.Forms.Button();
InBlock.gif            
this.MailFolderList = new System.Windows.Forms.ListBox();
InBlock.gif            
this.FilterBox = new System.Windows.Forms.TextBox();
InBlock.gif            
this.label1 = new System.Windows.Forms.Label();
InBlock.gif            
this.label2 = new System.Windows.Forms.Label();
InBlock.gif            
this.tabControl1 = new System.Windows.Forms.TabControl();
InBlock.gif            
this.tabPage1 = new System.Windows.Forms.TabPage();
InBlock.gif            
this.tabPage2 = new System.Windows.Forms.TabPage();
InBlock.gif            
this.BodyBox = new System.Windows.Forms.TextBox();
InBlock.gif            
this.ExportBtn = new System.Windows.Forms.Button();
InBlock.gif            
this.QuestionListView = new System.Windows.Forms.ListView();
InBlock.gif            
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
InBlock.gif            
this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
InBlock.gif            
this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
InBlock.gif            
this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
InBlock.gif            
this.QuitBtn = new System.Windows.Forms.Button();
InBlock.gif            
this.tabControl1.SuspendLayout();
InBlock.gif            
this.tabPage1.SuspendLayout();
InBlock.gif            
this.tabPage2.SuspendLayout();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// ConnectButton
InBlock.gif            
// 
InBlock.gif
            this.ConnectButton.Location = new System.Drawing.Point(2328);
InBlock.gif            
this.ConnectButton.Name = "ConnectButton";
InBlock.gif            
this.ConnectButton.Size = new System.Drawing.Size(12024);
InBlock.gif            
this.ConnectButton.TabIndex = 0;
InBlock.gif            
this.ConnectButton.Text = "Access Outlook";
InBlock.gif            
this.ConnectButton.Click += new System.EventHandler(this.ConnectButton_Click);
InBlock.gif            
// 
InBlock.gif            
// MailFolderList
InBlock.gif            
// 
InBlock.gif
            this.MailFolderList.HorizontalScrollbar = true;
InBlock.gif            
this.MailFolderList.Location = new System.Drawing.Point(840);
InBlock.gif            
this.MailFolderList.Name = "MailFolderList";
InBlock.gif            
this.MailFolderList.Size = new System.Drawing.Size(208381);
InBlock.gif            
this.MailFolderList.TabIndex = 1;
InBlock.gif            
this.MailFolderList.SelectedIndexChanged += new System.EventHandler(this.MailFolderList_SelectedIndexChanged);
InBlock.gif            
// 
InBlock.gif            
// FilterBox
InBlock.gif            
// 
InBlock.gif
            this.FilterBox.Location = new System.Drawing.Point(8010);
InBlock.gif            
this.FilterBox.Name = "FilterBox";
InBlock.gif            
this.FilterBox.Size = new System.Drawing.Size(13620);
InBlock.gif            
this.FilterBox.TabIndex = 2;
InBlock.gif            
this.FilterBox.Text = "";
InBlock.gif            
// 
InBlock.gif            
// label1
InBlock.gif            
// 
InBlock.gif
            this.label1.Location = new System.Drawing.Point(814);
InBlock.gif            
this.label1.Name = "label1";
InBlock.gif            
this.label1.Size = new System.Drawing.Size(6416);
InBlock.gif            
this.label1.TabIndex = 3;
InBlock.gif            
this.label1.Text = "Title Filter:";
InBlock.gif            
// 
InBlock.gif            
// label2
InBlock.gif            
// 
InBlock.gif
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif"8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
InBlock.gif            
this.label2.Location = new System.Drawing.Point(8424);
InBlock.gif            
this.label2.Name = "label2";
InBlock.gif            
this.label2.Size = new System.Drawing.Size(10016);
InBlock.gif            
this.label2.TabIndex = 4;
InBlock.gif            
this.label2.Text = "label2";
InBlock.gif            
// 
InBlock.gif            
// tabControl1
InBlock.gif            
// 
InBlock.gif
            this.tabControl1.Controls.Add(this.tabPage1);
InBlock.gif            
this.tabControl1.Controls.Add(this.tabPage2);
InBlock.gif            
this.tabControl1.Location = new System.Drawing.Point(23240);
InBlock.gif            
this.tabControl1.Name = "tabControl1";
InBlock.gif            
this.tabControl1.SelectedIndex = 0;
InBlock.gif            
this.tabControl1.Size = new System.Drawing.Size(440384);
InBlock.gif            
this.tabControl1.TabIndex = 5;
InBlock.gif            
// 
InBlock.gif            
// tabPage1
InBlock.gif            
// 
InBlock.gif
            this.tabPage1.Controls.Add(this.BodyBox);
InBlock.gif            
this.tabPage1.Location = new System.Drawing.Point(422);
InBlock.gif            
this.tabPage1.Name = "tabPage1";
InBlock.gif            
this.tabPage1.Size = new System.Drawing.Size(432358);
InBlock.gif            
this.tabPage1.TabIndex = 0;
InBlock.gif            
this.tabPage1.Text = "Mailbody";
InBlock.gif            
// 
InBlock.gif            
// tabPage2
InBlock.gif            
// 
InBlock.gif
            this.tabPage2.Controls.Add(this.QuestionListView);
InBlock.gif            
this.tabPage2.Location = new System.Drawing.Point(422);
InBlock.gif            
this.tabPage2.Name = "tabPage2";
InBlock.gif            
this.tabPage2.Size = new System.Drawing.Size(432358);
InBlock.gif            
this.tabPage2.TabIndex = 1;
InBlock.gif            
this.tabPage2.Text = "Question Analyze";
InBlock.gif            
// 
InBlock.gif            
// BodyBox
InBlock.gif            
// 
InBlock.gif
            this.BodyBox.Location = new System.Drawing.Point(00);
InBlock.gif            
this.BodyBox.Multiline = true;
InBlock.gif            
this.BodyBox.Name = "BodyBox";
InBlock.gif            
this.BodyBox.ScrollBars = System.Windows.Forms.ScrollBars.Both;
InBlock.gif            
this.BodyBox.Size = new System.Drawing.Size(432360);
InBlock.gif            
this.BodyBox.TabIndex = 0;
InBlock.gif            
this.BodyBox.Text = "";
InBlock.gif            
// 
InBlock.gif            
// ExportBtn
InBlock.gif            
// 
InBlock.gif
            this.ExportBtn.Location = new System.Drawing.Point(3688);
InBlock.gif            
this.ExportBtn.Name = "ExportBtn";
InBlock.gif            
this.ExportBtn.Size = new System.Drawing.Size(9623);
InBlock.gif            
this.ExportBtn.TabIndex = 6;
InBlock.gif            
this.ExportBtn.Text = "Export to Excel";
InBlock.gif            
this.ExportBtn.Click += new System.EventHandler(this.ExportBtn_Click);
InBlock.gif            
// 
InBlock.gif            
// QuestionListView
InBlock.gif            
// 
ExpandedSubBlockStart.gifContractedSubBlock.gif
            this.QuestionListView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] dot.gif{
InBlock.gif                                                                                               
this.columnHeader1,
InBlock.gif                                                                                               
this.columnHeader3,
InBlock.gif                                                                                               
this.columnHeader4,
ExpandedSubBlockEnd.gif                                                                                               
this.columnHeader5}
);
InBlock.gif            
this.QuestionListView.FullRowSelect = true;
InBlock.gif            
this.QuestionListView.GridLines = true;
InBlock.gif            
this.QuestionListView.Location = new System.Drawing.Point(00);
InBlock.gif            
this.QuestionListView.MultiSelect = false;
InBlock.gif            
this.QuestionListView.Name = "QuestionListView";
InBlock.gif            
this.QuestionListView.Size = new System.Drawing.Size(432360);
InBlock.gif            
this.QuestionListView.TabIndex = 0;
InBlock.gif            
this.QuestionListView.View = System.Windows.Forms.View.Details;
InBlock.gif            
// 
InBlock.gif            
// columnHeader1
InBlock.gif            
// 
InBlock.gif
            this.columnHeader1.Text = "MailID";
InBlock.gif            
this.columnHeader1.Width = 42;
InBlock.gif            
// 
InBlock.gif            
// columnHeader3
InBlock.gif            
// 
InBlock.gif
            this.columnHeader3.Text = "Q No.";
InBlock.gif            
this.columnHeader3.Width = 40;
InBlock.gif            
// 
InBlock.gif            
// columnHeader4
InBlock.gif            
// 
InBlock.gif
            this.columnHeader4.Text = "Question";
InBlock.gif            
this.columnHeader4.Width = 280;
InBlock.gif            
// 
InBlock.gif            
// columnHeader5
InBlock.gif            
// 
InBlock.gif
            this.columnHeader5.Text = "Answer";
InBlock.gif            
// 
InBlock.gif            
// QuitBtn
InBlock.gif            
// 
InBlock.gif
            this.QuitBtn.Location = new System.Drawing.Point(6098);
InBlock.gif            
this.QuitBtn.Name = "QuitBtn";
InBlock.gif            
this.QuitBtn.Size = new System.Drawing.Size(6423);
InBlock.gif            
this.QuitBtn.TabIndex = 7;
InBlock.gif            
this.QuitBtn.Text = "Quit";
InBlock.gif            
this.QuitBtn.Click += new System.EventHandler(this.QuitBtn_Click);
InBlock.gif            
// 
InBlock.gif            
// Mainform
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleBaseSize = new System.Drawing.Size(513);
InBlock.gif            
this.ClientSize = new System.Drawing.Size(688442);
InBlock.gif            
this.Controls.Add(this.QuitBtn);
InBlock.gif            
this.Controls.Add(this.ExportBtn);
InBlock.gif            
this.Controls.Add(this.tabControl1);
InBlock.gif            
this.Controls.Add(this.label2);
InBlock.gif            
this.Controls.Add(this.label1);
InBlock.gif            
this.Controls.Add(this.FilterBox);
InBlock.gif            
this.Controls.Add(this.MailFolderList);
InBlock.gif            
this.Controls.Add(this.ConnectButton);
InBlock.gif            
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
InBlock.gif            
this.Name = "Mainform";
InBlock.gif            
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
InBlock.gif            
this.Text = "Site Survey Analysis Tool";
InBlock.gif            
this.tabControl1.ResumeLayout(false);
InBlock.gif            
this.tabPage1.ResumeLayout(false);
InBlock.gif            
this.tabPage2.ResumeLayout(false);
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void ConnectButton_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ConnectButton.Enabled 
= false;
InBlock.gif                Outlook.ApplicationClass outLookApp 
= new Outlook.ApplicationClass();
InBlock.gif                Outlook.NameSpace outlookNS 
= outLookApp.GetNamespace("MAPI");
InBlock.gif                Outlook.MAPIFolder selectFolder 
= outlookNS.PickFolder();
InBlock.gif                MailFolderList.Items.Clear();
InBlock.gif                maillist.Clear();
InBlock.gif                QuestionListView.Items.Clear();
InBlock.gif                BodyBox.Text 
= string.Empty;
InBlock.gif                
int i=0;
InBlock.gif                
foreach(object item in selectFolder.Items)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Outlook.MailItem mi 
= item as Outlook.MailItem;
InBlock.gif                    
if (FilterBox.Text.Trim()!=string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (mi.Subject.IndexOf(FilterBox.Text.Trim())>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            i
++;
InBlock.gif                            MailFolderList.Items.Add(i.ToString()
+"."+mi.Subject+mi.ReceivedTime.ToString());
InBlock.gif                            maillist.Add(mi);
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        i
++;
InBlock.gif                        MailFolderList.Items.Add(i.ToString()
+"."+mi.Subject+mi.ReceivedTime.ToString());
InBlock.gif                        maillist.Add(mi);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                label2.Text 
= "Total "+i.ToString()+" mails"
InBlock.gif                outLookApp 
= null;
InBlock.gif                connOutlook 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                connOutlook 
= false;
InBlock.gif                MessageBox.Show(
"Can't connect to Outlook!");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ConnectButton.Enabled 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// The main entry point for the application.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Application.Run(
new Mainform());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void MailFolderList_SelectedIndexChanged(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int midx = MailFolderList.SelectedIndex;
InBlock.gif                Outlook.MailItem mi 
= maillist[midx] as Outlook.MailItem;
InBlock.gif                BodyBox.Text 
= mi.Body;
InBlock.gif                QuestionListView.Items.Clear();
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    AnalyzeBody(midx
+1,mi);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    MessageBox.Show(
"The mailbody don't follow the format of Site Survey!");
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
"Can't find this mail.");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void AnalyzeBody(int midx,Outlook.MailItem mi)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string body = mi.Body;
InBlock.gif            body 
= body.Replace("\r\n","|");
InBlock.gif            
string[] linearr = body.Split('|');
InBlock.gif            
string currquestion = "";
InBlock.gif            
string currmainquest = "";
InBlock.gif            
string curranswer = "";
InBlock.gif            
string currqnum = string.Empty;
InBlock.gif            
int secqnum = 0;
InBlock.gif            
foreach(string line in linearr)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (line.IndexOf('Q')>-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//新的题目
InBlock.gif
                    if (line.StartsWith("Q"))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        currqnum 
= line.Substring(0,line.IndexOf(":"));
InBlock.gif                        currquestion 
= line.Remove(0,line.IndexOf(":")+1);
InBlock.gif                        secqnum 
= 0;
InBlock.gif                        
if (currquestion.IndexOf("Q")>-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            curranswer 
= "Q"+currquestion.Split('Q')[1];
InBlock.gif                            currquestion 
= currquestion.Split('Q')[0];
InBlock.gif                            
//保存这个题目
InBlock.gif
                            ListViewItem listitem = new ListViewItem();
InBlock.gif                            listitem.Text 
= midx.ToString();
InBlock.gif                            listitem.SubItems.Add(currqnum);
InBlock.gif                            listitem.SubItems.Add(currquestion);
InBlock.gif                            listitem.SubItems.Add(curranswer);
InBlock.gif
InBlock.gif                            QuestionListView.Items.Add(listitem);
InBlock.gif                            currqnum 
= "";
InBlock.gif                            currquestion 
= "";
InBlock.gif                            curranswer 
= "";
InBlock.gif
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            currmainquest 
= currquestion;
InBlock.gif                            curranswer 
= "";
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//有子题目,分别保存
InBlock.gif
                        secqnum++
InBlock.gif                        curranswer 
+= "Q"+line.Split('Q')[1];
InBlock.gif                        currquestion 
= currmainquest+"--"+line.Split('Q')[0];
InBlock.gif                        ListViewItem listitem 
= new ListViewItem();
InBlock.gif                        listitem.Text 
= midx.ToString();
InBlock.gif                        listitem.SubItems.Add(currqnum
+"_"+secqnum.ToString());
InBlock.gif                        listitem.SubItems.Add(currquestion);
InBlock.gif                        listitem.SubItems.Add(curranswer);
InBlock.gif                        QuestionListView.Items.Add(listitem);
InBlock.gif                        currquestion 
= "";
InBlock.gif                        curranswer 
= "";
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void ExportBtn_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (connOutlook)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string log = string.Empty;
InBlock.gif                    tabControl1.TabIndex 
= 0;
InBlock.gif                    ExportBtn.Enabled 
= false;
InBlock.gif                    ConnectButton.Enabled 
= false;
InBlock.gif
InBlock.gif                    ArrayList SurveyList 
= new ArrayList();
InBlock.gif
InBlock.gif                    
//导出记录到ArrayList中
InBlock.gif
                    if (maillist!=null && maillist.Count>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
for (int i=0;i<maillist.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            Outlook.MailItem mi 
= maillist[i] as Outlook.MailItem;
InBlock.gif                            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                AnalyzeBody(i
+1,mi);
InBlock.gif                                
//将listview中的内容导出
InBlock.gif
                                for (int j=0;j<QuestionListView.Items.Count;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    SurveyQ sq 
= new SurveyQ();
InBlock.gif                                    sq.Mailid 
= (i+1).ToString();
InBlock.gif                                    sq.Mailtime 
= mi.ReceivedTime.ToString();
InBlock.gif                                    sq.QNo 
= QuestionListView.Items[j].SubItems[1].Text;
InBlock.gif                                    sq.Question 
= QuestionListView.Items[j].SubItems[2].Text;
InBlock.gif                                    sq.Qanswer 
= QuestionListView.Items[j].SubItems[3].Text;
InBlock.gif                                    SurveyList.Add(sq);
ExpandedSubBlockEnd.gif                                }

InBlock.gif
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                log 
+= "Mail:ID "+(i+1).ToString()+" didn't match the survey format. Can't export!\r\n";
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif
InBlock.gif                    
//将生成的list输出到Excel
InBlock.gif
                    if (SurveyList.Count>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
string[,] SurveyArray = new string[SurveyList.Count,5];
InBlock.gif                        
for (int i=0;i<SurveyList.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            SurveyQ sq 
= SurveyList[i] as SurveyQ;
InBlock.gif                            SurveyArray[i,
0= sq.Mailid;
InBlock.gif                            SurveyArray[i,
1= sq.Mailtime;
InBlock.gif                            SurveyArray[i,
2= sq.QNo;
InBlock.gif                            SurveyArray[i,
3= sq.Question;
InBlock.gif                            SurveyArray[i,
4= sq.Qanswer;
ExpandedSubBlockEnd.gif                        }

InBlock.gif
InBlock.gif                        Excel.ApplicationClass excelApp 
= new Excel.ApplicationClass();
InBlock.gif                        Excel.Workbook workbook 
= excelApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
InBlock.gif                        Excel.Worksheet worksheet 
= (Excel.Worksheet)workbook.Worksheets[1];
InBlock.gif                        
//标题
InBlock.gif
                        Excel.Range titlerange = worksheet.get_Range("A1","E1");
InBlock.gif                        titlerange.Font.Bold 
= true;
ExpandedSubBlockStart.gifContractedSubBlock.gif                        titlerange.Value2 
= new string[5dot.gif{"Mail ID","Receive Time","Question No.","Question","Answer"};
InBlock.gif
InBlock.gif                        
//正文
InBlock.gif
                        Excel.Range workrange = worksheet.get_Range("A2","E"+(SurveyList.Count+1).ToString());
InBlock.gif                        workrange.Value2 
= SurveyArray;
InBlock.gif
InBlock.gif                        excelApp.Visible 
= true;
InBlock.gif
InBlock.gif                        titlerange 
= null;
InBlock.gif                        workrange 
= null;
InBlock.gif                        worksheet 
= null;
InBlock.gif                        workbook 
= null;
InBlock.gif                        excelApp 
= null;
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
if (log!=string.Empty)
InBlock.gif                            MessageBox.Show(log);
InBlock.gif                        
else
InBlock.gif                            MessageBox.Show(
"Export successfully!");
InBlock.gif                    ExportBtn.Enabled 
= true;
InBlock.gif                    ConnectButton.Enabled 
= true;
InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    MessageBox.Show(
"Can't Export!");
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
"Didn't connect to Outlook!");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void QuitBtn_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Application.Exit();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
internal class SurveyQ
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public string Mailid;
InBlock.gif            
public string Mailtime;
InBlock.gif            
public string QNo;
InBlock.gif            
public string Question;
InBlock.gif            
public string Qanswer;
InBlock.gif            
InBlock.gif            
public SurveyQ()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Mailid 
= "";
InBlock.gif                Mailtime 
= "";
InBlock.gif                QNo 
= "";
InBlock.gif                Question 
= "";
InBlock.gif                Qanswer 
= "";
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif    
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/abei108/archive/2006/06/06/418883.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值