统计标准WORD报表中编号和金额,常驻后台,写入XML文件.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Xml;
using System.Diagnostics;

namespace CreativeLearning
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.TabControl tabControl1;
        
private System.Windows.Forms.TabPage tabSource;
        
private System.Windows.Forms.TabPage tabDest;
        
private System.Windows.Forms.Label label1;
        
private System.Windows.Forms.Label label2;
        
private System.Windows.Forms.TextBox txtSource;
        
private System.Windows.Forms.TextBox txtProcessedFile;
        
private System.Windows.Forms.TextBox txtDest;
        
private System.Windows.Forms.Label label3;
        
private System.Windows.Forms.GroupBox groupEventLog;
        
private System.Windows.Forms.ListBox lstEvents;
        
private System.Windows.Forms.Button btnViewSummary;
        
private System.Windows.Forms.Button btnRefresh;
        
private System.Windows.Forms.Button btnOK;
        
private System.Windows.Forms.Button btnExit;
        
private System.Windows.Forms.CheckBox optGenerateLog;
        
private System.Windows.Forms.ErrorProvider errMessage;
        
private System.IO.FileSystemWatcher watchDir;
        
private System.ComponentModel.IContainer components;
        
private System.Windows.Forms.NotifyIcon icoNotify;
        
private System.Windows.Forms.ContextMenu mnuNotify;
        
private System.Windows.Forms.MenuItem menuItem1;
        
private System.Windows.Forms.MenuItem menuItem2;

        
private System.Drawing.Icon m_Ready = new System.Drawing.Icon("Ready.ico");
        
private System.Diagnostics.EventLog eventLog;
        
private System.Drawing.Icon m_Info = new Icon("Info.ico");
        
private Icon m_error = new Icon("error.ico");

        
public Form1()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();

            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
#region Windows 窗体设计器生成的代码
        
/// <summary>
        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
        
/// 此方法的内容。
        
/// </summary>

        private void InitializeComponent()
        
{
            
this.components = new System.ComponentModel.Container();
            
this.tabControl1 = new System.Windows.Forms.TabControl();
            
this.tabSource = new System.Windows.Forms.TabPage();
            
this.optGenerateLog = new System.Windows.Forms.CheckBox();
            
this.txtProcessedFile = new System.Windows.Forms.TextBox();
            
this.txtSource = new System.Windows.Forms.TextBox();
            
this.label2 = new System.Windows.Forms.Label();
            
this.label1 = new System.Windows.Forms.Label();
            
this.tabDest = new System.Windows.Forms.TabPage();
            
this.btnRefresh = new System.Windows.Forms.Button();
            
this.btnViewSummary = new System.Windows.Forms.Button();
            
this.groupEventLog = new System.Windows.Forms.GroupBox();
            
this.lstEvents = new System.Windows.Forms.ListBox();
            
this.label3 = new System.Windows.Forms.Label();
            
this.txtDest = new System.Windows.Forms.TextBox();
            
this.btnOK = new System.Windows.Forms.Button();
            
this.btnExit = new System.Windows.Forms.Button();
            
this.errMessage = new System.Windows.Forms.ErrorProvider();
            
this.watchDir = new System.IO.FileSystemWatcher();
            
this.icoNotify = new System.Windows.Forms.NotifyIcon(this.components);
            
this.mnuNotify = new System.Windows.Forms.ContextMenu();
            
this.menuItem1 = new System.Windows.Forms.MenuItem();
            
this.menuItem2 = new System.Windows.Forms.MenuItem();
            
this.eventLog = new System.Diagnostics.EventLog();
            
this.tabControl1.SuspendLayout();
            
this.tabSource.SuspendLayout();
            
this.tabDest.SuspendLayout();
            
this.groupEventLog.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(
this.watchDir)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(
this.eventLog)).BeginInit();
            
this.SuspendLayout();
            
// 
            
// tabControl1
            
// 
            this.tabControl1.Controls.Add(this.tabSource);
            
this.tabControl1.Controls.Add(this.tabDest);
            
this.tabControl1.Location = new System.Drawing.Point(7232);
            
this.tabControl1.Name = "tabControl1";
            
this.tabControl1.SelectedIndex = 0;
            
this.tabControl1.Size = new System.Drawing.Size(488296);
            
this.tabControl1.TabIndex = 0;
            
// 
            
// tabSource
            
// 
            this.tabSource.Controls.Add(this.optGenerateLog);
            
this.tabSource.Controls.Add(this.txtProcessedFile);
            
this.tabSource.Controls.Add(this.txtSource);
            
this.tabSource.Controls.Add(this.label2);
            
this.tabSource.Controls.Add(this.label1);
            
this.tabSource.Location = new System.Drawing.Point(421);
            
this.tabSource.Name = "tabSource";
            
this.tabSource.Size = new System.Drawing.Size(480271);
            
this.tabSource.TabIndex = 0;
            
this.tabSource.Text = "Source Options";
            
// 
            
// optGenerateLog
            
// 
            this.optGenerateLog.Location = new System.Drawing.Point(56168);
            
this.optGenerateLog.Name = "optGenerateLog";
            
this.optGenerateLog.Size = new System.Drawing.Size(26424);
            
this.optGenerateLog.TabIndex = 4;
            
this.optGenerateLog.Text = "Generate event log for bad file format";
            
// 
            
// txtProcessedFile
            
// 
            this.txtProcessedFile.Location = new System.Drawing.Point(40112);
            
this.txtProcessedFile.Name = "txtProcessedFile";
            
this.txtProcessedFile.Size = new System.Drawing.Size(39221);
            
this.txtProcessedFile.TabIndex = 3;
            
this.txtProcessedFile.Text = "";
            
this.txtProcessedFile.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txtProcessedFile_KeyUp);
            
// 
            
// txtSource
            
// 
            this.txtSource.Location = new System.Drawing.Point(16830);
            
this.txtSource.Name = "txtSource";
            
this.txtSource.Size = new System.Drawing.Size(26421);
            
this.txtSource.TabIndex = 2;
            
this.txtSource.Text = "";
            
this.txtSource.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txtSource_KeyUp);
            
// 
            
// label2
            
// 
            this.label2.AutoSize = true;
            
this.label2.Location = new System.Drawing.Point(4080);
            
this.label2.Name = "label2";
            
this.label2.Size = new System.Drawing.Size(24017);
            
this.label2.TabIndex = 1;
            
this.label2.Text = "After processing, move source file to:";
            
// 
            
// label1
            
// 
            this.label1.AutoSize = true;
            
this.label1.Location = new System.Drawing.Point(4032);
            
this.label1.Name = "label1";
            
this.label1.Size = new System.Drawing.Size(10317);
            
this.label1.TabIndex = 0;
            
this.label1.Text = "Source Directory";
            
// 
            
// tabDest
            
// 
            this.tabDest.Controls.Add(this.btnRefresh);
            
this.tabDest.Controls.Add(this.btnViewSummary);
            
this.tabDest.Controls.Add(this.groupEventLog);
            
this.tabDest.Controls.Add(this.label3);
            
this.tabDest.Controls.Add(this.txtDest);
            
this.tabDest.Location = new System.Drawing.Point(421);
            
this.tabDest.Name = "tabDest";
            
this.tabDest.Size = new System.Drawing.Size(480271);
            
this.tabDest.TabIndex = 1;
            
this.tabDest.Text = "Destination Options";
            
// 
            
// btnRefresh
            
// 
            this.btnRefresh.Location = new System.Drawing.Point(328232);
            
this.btnRefresh.Name = "btnRefresh";
            
this.btnRefresh.Size = new System.Drawing.Size(11223);
            
this.btnRefresh.TabIndex = 6;
            
this.btnRefresh.Text = "Refresh Log";
            
this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
            
// 
            
// btnViewSummary
            
// 
            this.btnViewSummary.Location = new System.Drawing.Point(32232);
            
this.btnViewSummary.Name = "btnViewSummary";
            
this.btnViewSummary.Size = new System.Drawing.Size(11223);
            
this.btnViewSummary.TabIndex = 5;
            
this.btnViewSummary.Text = "View Summary";
            
this.btnViewSummary.Click += new System.EventHandler(this.btnViewSummary_Click);
            
// 
            
// groupEventLog
            
// 
            this.groupEventLog.Controls.Add(this.lstEvents);
            
this.groupEventLog.Location = new System.Drawing.Point(3272);
            
this.groupEventLog.Name = "groupEventLog";
            
this.groupEventLog.Size = new System.Drawing.Size(416144);
            
this.groupEventLog.TabIndex = 4;
            
this.groupEventLog.TabStop = false;
            
this.groupEventLog.Text = "Event Log";
            
// 
            
// lstEvents
            
// 
            this.lstEvents.ItemHeight = 12;
            
this.lstEvents.Location = new System.Drawing.Point(1624);
            
this.lstEvents.Name = "lstEvents";
            
this.lstEvents.Size = new System.Drawing.Size(384100);
            
this.lstEvents.TabIndex = 0;
            
// 
            
// label3
            
// 
            this.label3.AutoSize = true;
            
this.label3.Location = new System.Drawing.Point(3232);
            
this.label3.Name = "label3";
            
this.label3.Size = new System.Drawing.Size(13417);
            
this.label3.TabIndex = 3;
            
this.label3.Text = "Destination Directory";
            
// 
            
// txtDest
            
// 
            this.txtDest.Location = new System.Drawing.Point(17630);
            
this.txtDest.Name = "txtDest";
            
this.txtDest.Size = new System.Drawing.Size(27221);
            
this.txtDest.TabIndex = 2;
            
this.txtDest.Text = "";
            
this.txtDest.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txtDest_KeyUp);
            
// 
            
// btnOK
            
// 
            this.btnOK.Location = new System.Drawing.Point(152344);
            
this.btnOK.Name = "btnOK";
            
this.btnOK.TabIndex = 1;
            
this.btnOK.Text = "OK";
            
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
            
// 
            
// btnExit
            
// 
            this.btnExit.Location = new System.Drawing.Point(384344);
            
this.btnExit.Name = "btnExit";
            
this.btnExit.TabIndex = 2;
            
this.btnExit.Text = "Exit";
            
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
            
// 
            
// errMessage
            
// 
            this.errMessage.ContainerControl = this;
            
// 
            
// watchDir
            
// 
            this.watchDir.EnableRaisingEvents = true;
            
this.watchDir.Filter = "*.doc";
            
this.watchDir.SynchronizingObject = this;
            
this.watchDir.Created += new System.IO.FileSystemEventHandler(this.watchDir_Created);
            
// 
            
// icoNotify
            
// 
            this.icoNotify.Text = "Creative";
            
this.icoNotify.Visible = true;
            
this.icoNotify.DoubleClick += new System.EventHandler(this.icoNotify_DoubleClick);
            
// 
            
// mnuNotify
            
// 
            this.mnuNotify.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                      
this.menuItem1,
                                                                                      
this.menuItem2}
);
            
// 
            
// menuItem1
            
// 
            this.menuItem1.Index = 0;
            
this.menuItem1.Text = "Configure Application";
            
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
            
// 
            
// menuItem2
            
// 
            this.menuItem2.Index = 1;
            
this.menuItem2.Text = "Exit";
            
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
            
// 
            
// eventLog
            
// 
            this.eventLog.SynchronizingObject = this;
            
// 
            
// Form1
            
// 
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
            
this.ClientSize = new System.Drawing.Size(624373);
            
this.Controls.Add(this.btnExit);
            
this.Controls.Add(this.btnOK);
            
this.Controls.Add(this.tabControl1);
            
this.Name = "Form1";
            
this.Text = "Creative Learning";
            
this.Load += new System.EventHandler(this.Form1_Load);
            
this.tabControl1.ResumeLayout(false);
            
this.tabSource.ResumeLayout(false);
            
this.tabDest.ResumeLayout(false);
            
this.groupEventLog.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(
this.watchDir)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(
this.eventLog)).EndInit();
            
this.ResumeLayout(false);

        }

        
#endregion


        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }


        
private void Form1_Load(object sender, System.EventArgs e)
        
{
            
// 初始化文本框内默认路径
            txtSource.Text = "D:\\Creative\\Source\\";
            txtProcessedFile.Text 
= "D:\\Creative\\Processed\\";
            txtDest.Text 
= "D:\\Creative\\Destination\\";
            optGenerateLog.Checked 
= true;
        }


        
private void btnOK_Click(object sender, System.EventArgs e)
        
{
            
if (!Directory.Exists(txtSource.Text))
            
{
                errMessage.SetError(txtSource, 
"Invalid source directory");
                txtSource.Focus();
                tabControl1.SelectedTab 
= tabSource;
                
return;
            }

            
else
            
{
                errMessage.SetError(txtSource, 
"");
            }

            
if (!Directory.Exists(txtProcessedFile.Text))
            
{
                errMessage.SetError(txtProcessedFile, 
"Invalid Processed File directory");
                txtProcessedFile.Focus();
                tabControl1.SelectedTab 
= tabSource;
                
return;
            }

            
else
            
{
                errMessage.SetError(txtProcessedFile, 
"");
            }

            
if (!Directory.Exists(txtDest.Text))
            
{
                errMessage.SetError(txtDest, 
"Invalid Destination directory");
                txtDest.Focus();
                tabControl1.SelectedTab 
= tabDest;
                
return;
            }

            
else
            
{
                errMessage.SetError(txtDest, 
"");
            }


            watchDir.Path 
= txtSource.Text;
            watchDir.EnableRaisingEvents 
= true;

            icoNotify.Icon 
= m_Ready;
            icoNotify.Visible 
= true;

            
this.ShowInTaskbar = false;
            
this.Hide();

        }


        
//一种动态智能判断的好方法,交互性很好
        private void txtSource_KeyUp(object sender, KeyEventArgs e)
        
{
            
if (Directory.Exists(txtSource.Text))
            
{
                txtSource.BackColor 
= Color.White;
            }

            
else
            
{
                txtSource.BackColor 
= Color.Pink;
            }

        }


        
private void txtProcessedFile_KeyUp(object sender, KeyEventArgs e)
        
{
            
if (Directory.Exists(txtProcessedFile.Text))
            
{
                txtProcessedFile.BackColor 
= Color.White;
            }

            
else
            
{
                txtProcessedFile.BackColor 
= Color.Pink;
            }

        }


        
private void txtDest_KeyUp(object sender, KeyEventArgs e)
        
{
            
if (Directory.Exists(txtDest.Text))
            
{
                txtDest.BackColor 
= Color.White;
            }

            
else
            
{
                txtDest.BackColor 
= Color.Pink;
            }

        }


        
private void menuItem1_Click(object sender, System.EventArgs e)
        
{
            icoNotify.Visible 
= false;
            
this.ShowInTaskbar = true;
            
this.Show();
        }


        
private void menuItem2_Click(object sender, System.EventArgs e)
        
{
            Application.Exit();
        }


        
private void icoNotify_DoubleClick(object sender, EventArgs e)
        
{
            icoNotify.Visible 
= false;
            
this.ShowInTaskbar = true;
            
this.Show();
        }


        
private void btnExit_Click(object sender, System.EventArgs e)
        
{
            Application.Exit();
        }


        
// 当有新文件进入时的事件
        private void watchDir_Created(object sender, FileSystemEventArgs e)
        
{
            watchDir.EnableRaisingEvents 
= false;
            icoNotify.Icon 
= m_Info;
            icoNotify.Text 
= "Processed:" + e.Name;

            Word.Application wdApp 
= new Word.ApplicationClass();
            Word.Document Doc 
= new Word.DocumentClass();
            
object filename = e.FullPath;

            
// 使用Missing对象来填充可选参数
            object optional = System.Reflection.Missing.Value;
            Doc 
= wdApp.Documents.Open(ref filename, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional);
            XmlTextWriter xmlWriter;
            xmlWriter 
= new XmlTextWriter(txtDest.Text + "Summary.xml"null);


            
try
            
{
                
// 学习下面的Word文档操作方法
                Word.Range wdRange;
                wdRange 
= Doc.Paragraphs[2].Range;
                
string strMemo, strAmount;
                
int intParacount;
                strMemo 
= wdRange.Text;
                strMemo 
= strMemo.Substring(154);
                intParacount 
= Doc.Paragraphs.Count;
                intParacount 
-= 2;
                wdRange 
= Doc.Paragraphs[intParacount].Range;
                
object count = "-1";
                
object wdCharacter = "1";
                wdRange.MoveEnd(
ref wdCharacter, ref count);
                strAmount 
= wdRange.Text;
                strAmount 
= strAmount.Substring(23);
            
                xmlWriter.Formatting 
= Formatting.Indented;
                xmlWriter.WriteDocType(
"Sales"nullnullnull);
                xmlWriter.WriteComment(
"Summary of sales at Creative Learning");
                xmlWriter.WriteStartElement(
"Sales");
                xmlWriter.WriteStartElement(Convert.ToString(DateTime.Today));
                xmlWriter.WriteElementString(
"Memo", strMemo);
                xmlWriter.WriteElementString(
"Amount", strAmount);
                xmlWriter.WriteEndElement();
                xmlWriter.WriteEndElement();

                icoNotify.Icon 
= m_Ready;
            }

            
catch (Exception catchException)
            
{
                icoNotify.Icon 
= m_error;
                icoNotify.Text 
= "Error in " + e.Name;
                
if (optGenerateLog.Checked == true)
                
{
                    eventLog.WriteEntry(e.Name 
+ "" + catchException.Message);
                }

            }

            
finally
            
{
                
// 把放在缓冲区的内容写入基础流(即Stream),放置内容丢失
                xmlWriter.Flush();
                xmlWriter.Close();
                wdApp.Quit(
ref optional, ref optional, ref optional);
                wdApp 
= null;
                watchDir.EnableRaisingEvents 
= true;
            }

            
            tryagain:
                
try
                
{
                    File.Move(e.FullPath, txtProcessedFile.Text 
+ e.Name);
                }

                
catch
                
{
                    
goto tryagain;
                }


        }


        
private void btnRefresh_Click(object sender, System.EventArgs e)
        
{
            
// 利用EventLog控件写入系统日志
            lstEvents.Items.Clear();
            eventLog.Log 
= "Application";
            eventLog.MachineName 
= ".";
            
foreach (EventLogEntry logEntry in eventLog.Entries)
            
{
                
if (logEntry.Source == "CreativeLearning")
                
{
                    lstEvents.Items.Add(logEntry.Message);
                }

            }

        }


        
private void btnViewSummary_Click(object sender, System.EventArgs e)
        
{
            StreamReader strRead;
            
try
            
{
                strRead 
= new StreamReader(txtDest.Text + "Summary.xml");
                MessageBox.Show(strRead.ReadToEnd(), txtDest.Text 
+ "Summary.xml", MessageBoxButtons.OK, MessageBoxIcon.Information);
                strRead.Close();
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(
"An error has returned: " + ex.Message);
            }

        }

    }

}

posted on 2005-08-13 09:17  wddavid  阅读(544)  评论(0)    收藏  举报