注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

gmd20的个人空间

// 编程和生活

 
 
 

日志

 
 

C# 中访问修改excel 的xls格式文件和xml文件,正则表达式  

2009-01-04 19:41:23|  分类: 程序设计 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

记录一下,便于以后去查找msdn,如果以后用到的话。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Xml ;
using System.Text.RegularExpressions ;
using System.IO ;


namespace MergeResultToWads
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
   private Excel.Application excel   ;
   private Excel.Workbook xls ;
    private Excel.Worksheet SpreadSheet ;

   private System.Windows.Forms.Button button1;
   private System.Windows.Forms.TextBox textBox1;
   /// <summary>
   /// Required designer variable.
   /// </summary>
   private System.ComponentModel.Container components = null;

   public Form1()
   {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();

    //
    // TODO: Add any constructor code after InitializeComponent call
    //
   }

   /// <summary>
   /// Clean up any resources being used.
   /// </summary>
   protected override void Dispose( bool disposing )
   {
    if( disposing )
    {
     if (components != null)
     {
      components.Dispose();
     }
    }
    base.Dispose( disposing );
   }

   #region Windows Form Designer generated code
   /// <summary>
   /// Required method for Designer support - do not modify
   /// the contents of this method with the code editor.
   /// </summary>
   private void InitializeComponent()
   {
    this.button1 = new System.Windows.Forms.Button();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(392, 392);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(112, 48);
    this.button1.TabIndex = 0;
    this.button1.Text = "开始处理";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    //
    // textBox1
    //
    this.textBox1.Location = new System.Drawing.Point(0, 0);
    this.textBox1.Multiline = true;
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(640, 360);
    this.textBox1.TabIndex = 1;
    this.textBox1.Text = "";
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(640, 463);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);

   }
   #endregion

   /// <summary>
   /// The main entry point for the application.
   /// </summary>
   [STAThread]
   static void Main()
   {
    Application.Run(new Form1());
   }
  
   private bool UpdateWads(string id , string name, string output)
   {
      string filename = "C:\\Documents and Settings\\widebright\\Desktop\\测试\\" + name + ".xml";
      
    if (! System.IO.File.Exists(filename))
    {
      textBox1.Text = textBox1.Text + filename + " 这个文件不存在\r\n";
      return false;
    }
           
            XmlDocument wadsDoc= new XmlDocument();
   wadsDoc.Load (filename);
            XmlNode lastEntry = wadsDoc.LastChild;
         
          
            XmlDocument entryDoc= new XmlDocument();
            entryDoc.Load ("C:\\Documents and Settings\\widebright\\Desktop\\newentry.xml");  
      string strEntry = entryDoc.InnerXml;
            strEntry =strEntry.Replace ("$name$",name);
      strEntry =strEntry.Replace ("$output$",output);
            entryDoc.InnerXml = strEntry;
         
       
      XmlNode newEntry =wadsDoc.ImportNode (entryDoc.FirstChild,true);
   
            lastEntry.AppendChild ( newEntry );
            //lastEntry.InnerXml= lastEntry.InnerXml + "\r\n" +strEntry + "\r\n";
           // wadsDoc.PreserveWhitespace = true;
           
            wadsDoc.Save("C:\\Documents and Settings\\widebright\\Desktop\\合并后的\\" + name + ".xml");

           
      return true;
   }

   private bool UpdateSpreadSheet(string id , string name)
   {

            bool isUpdated = false;
    for (int i =25 ;i <300;i ++ )
    {
       Excel.Range cell =( Excel.Range) SpreadSheet.Cells[i,4];
    
    
     if (cell.Value != null && cell.Value.ToString () == name )
     {
        string text= cell.Value.ToString() ;
        SpreadSheet.Cells[i,12] = id;
                    SpreadSheet.Cells[i,11] = "Pass";
      isUpdated = true;
      break;
     }

    }


    if (isUpdated == true)
    {
     textBox1.Text = textBox1.Text +
      "已更新状态 " + " " + name
      + "\r\n======================\r\n";

     return true;
    }
    else
    {
     textBox1.Text = textBox1.Text +
      "xls文件中没有这个脚本 " + " " + name
      + "\r\n======================\r\n";

     return false;
    }


        
   }

   private void ProcessOneTestCase(XmlNode testcase)
   {  


    string name = testcase.Attributes[0].InnerText ;
    string result = testcase.ChildNodes[0].InnerText ;
            string output = testcase.ChildNodes[1].InnerText ;
           
    string id ;

    Match match = Regex.Match(output, @"Ok. Added Test ID\s+(\d+)");
    
    if ( match.Success)
    {
     id= match.Groups[1].Value ;
     textBox1.Text = textBox1.Text +
      id + "     " + name
      + "\r\n--------------\r\n";
  
     UpdateSpreadSheet(id,name);
     UpdateWads(id,name,output);

    }
    else{
     textBox1.Text = textBox1.Text +
      "这个脚本运行失败      " + " " + name
      + "\r\n--------------\r\n";
   
    }

           
   }

   private void button1_Click(object sender, System.EventArgs e)
   {
   
     excel = new Excel.Application() ;
     xls = excel.Workbooks.Open(
     "C:\\Documents and Settings\\widebright\\Desktop\\GUI Test Status.xls", 0,
     false,Type.Missing ,Type.Missing ,Type.Missing ,
     Type.Missing ,Type.Missing ,Type.Missing ,Type.Missing ,Type.Missing ,Type.Missing ,Type.Missing ) ;
          
     SpreadSheet= ( Excel.Worksheet) xls.Sheets [1];
  
          excel.WindowState = Excel.XlWindowState.xlNormal;
          excel.Visible = true; //是不是让excel启动后可见

       
    XmlDocument resultDoc= new XmlDocument();
  
    try
    {
     resultDoc.Load ("C:\\Documents and Settings\\widebright\\Desktop\\template .xml");
    }
    catch (XmlException error){
        string text = error.Message;
    }
          
            XmlElement root = resultDoc.DocumentElement;
   
      XmlNode script= root.FirstChild ;
    do{
      ProcessOneTestCase ( script);
               script = script.NextSibling;
    }while (script != null) ;


    //xls.Save();
   
      xls.Close(true,"C:\\Documents and Settings\\widebright\\Desktop\\GUI Test Status.xls" ,Type.Missing );
     excel.Workbooks.Close ();
  
    excel.Quit ();
    xls = null;
    excel = null;
            

   }
}
}

  评论这张
 
阅读(795)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017