好好研究这段代码

using System;

using System.Collections.Generic;

using System.Linq; using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Collections.ObjectModel;

using System.Xml.Linq;

using System.Runtime.InteropServices.Automation;

namespace SilverlightApplication51 {    

public partial class MainPage : UserControl    

 {        

            public MainPage()       

  {            

            InitializeComponent();

            Loaded += new RoutedEventHandler(MainPage_Loaded);   

      }

           void MainPage_Loaded(object sender, RoutedEventArgs e)      

   {         

            TweetList.ItemsSource = _tweets;

            Export.IsEnabled = AutomationFactory.IsAvailable;   

      }

        private ObservableCollection<Tweet> _tweets = new ObservableCollection<Tweet>();

        private void GetTweets_Click(object sender, RoutedEventArgs e)     

    {            

            WebClient client = new WebClient();

            client.DownloadStringCompleted += (s, ea) =>           

      {                    

                    XDocument doc = XDocument.Parse(ea.Result);

                    XNamespace ns = "http://www.w3.org/2005/Atom";

                    var items = from item in doc.Descendants(ns + "entry")                               

  select new Tweet()                               

  {                                    

                   Title = item.Element(ns + "title").Value,                                   

                   Image = new Uri((from XElement xe in item.Descendants(ns + "link")         

                   where xe.Attribute("type").Value == "image/png"                                                        

                   select xe.Attribute("href").Value).First<string>()),                                   

                   Link = new Uri((from XElement xe in item.Descendants(ns + "link")       

                   where xe.Attribute("type").Value == "text/html"                                                    

                   select xe.Attribute("href").Value).First<string>())         

                        };

                    foreach (Tweet t in items)              

       {                   

                    _tweets.Add(t);       

              }         

        };

            client.DownloadStringAsync(new Uri("http://search.twitter.com/search.atom?q=silverlight"));  

       }

        private void Export_Click(object sender, RoutedEventArgs e)      

   {            

if (_tweets.Count == 0)     

        {              

   MessageBox.Show("No tweets to export. Please get latest tweets first.");            

     return;         

    }

            try        

     {            

                dynamic excel = AutomationFactory.CreateObject("Excel.Application");

                excel.workbooks.Add();

                dynamic sheet = excel.ActiveSheet;

                int row = 1;

                // headers               

                dynamic linkHeaderCell = sheet.Cells[row, 1];          

                dynamic textHeaderCell = sheet.Cells[row, 2];

                linkHeaderCell.Value = "Url";              

                textHeaderCell.Value = "Message Text";

                // rows             

    foreach (Tweet t in _tweets)                

 {                

             row++;                   

             dynamic linkCell = sheet.Cells[row, 1];               

             dynamic textCell = sheet.Cells[row, 2];

             linkCell.Value = t.Link.ToString();      

             textCell.Value = t.Title;      

           }

                excel.Visible = true;

            }            

catch (Exception ex)           

  {                

 MessageBox.Show("Error automating Excel: " + ex.Message);       

      }

         }

     }

 }

你可能感兴趣的:(代码)