Silverlight 导出excel (二)

Silverlight 导出excel (二),第1张

概述  xaml: <Grid x:Name="LayoutRoot" Background="White">        <Grid.ColumnDefinitions>            <ColumnDefinition></ColumnDefinition>        </Grid.ColumnDefinitions>        <Grid.RowDefinitions>      

xaml:

<GrID x:name="LayoutRoot" Background="White">
       <GrID.ColumnDeFinitions>
           <ColumnDeFinition></ColumnDeFinition>
       </GrID.ColumnDeFinitions>
       <GrID.RowDeFinitions>
           <RowDeFinition Height="20"></RowDeFinition>
           <RowDeFinition Height="20"></RowDeFinition>
           <RowDeFinition></RowDeFinition>
       </GrID.RowDeFinitions>
       <button x:name="btnSave" GrID.Column="0" GrID.Row="0" Content="保存" Click="btnSave_Click" />
       <data:DataGrID GrID.Row="2" GrID.Column="0" name="dg" autoGenerateColumns="True"/>
   </GrID>

 

后台:

public MainPage()
      {
          InitializeComponent();
          this.Loaded += new RoutedEventHandler(MainPage_Loaded);
      }

      voID MainPage_Loaded(object sender,RoutedEventArgs e)
      {
          List<DataTest> Lists =new List<DataTest>();
          Lists.Add(new Datatest() { coloum1 = "213",coloum2 = "123213",coloum3 = "123213",coloum4 = "123" });
          Lists.Add(new Datatest() { coloum1 = "213",coloum4 = "123" });
          dg.ItemsSource = Lists;
      }

      private voID btnSave_Click(object sender,RoutedEventArgs e)
      {
          string data = AppCode.ExportDataGrID(true,this.dg,true);
          SavefileDialog sfd = new SavefileDialog();
          sfd.DefaultExt = "csv";
          sfd.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";

          sfd.FilterIndex = 1;

          if (sfd.ShowDialog() == true)
          {
              using (Stream stream = sfd.Openfile())
              {
                  //用utf8编码
                  Byte[] fileContent = System.Text.UTF8EnCoding.UTF8.GetBytes(data);
                  stream.Write(fileContent,fileContent.Length);
                  stream.Close();
              }
          }
      }

  }

/--------------------

  public class DataTest
  {
      public string coloum1
      {
          get;
          set;
      }
      public string coloum2
      {
          get;
          set;
      }
      public string coloum3
      {
          get;
          set;
      }
      public string coloum4
      {
          get;
          set;
      }

 

/-------------------

/// <summary>
       /// CSV格式化
       /// </summary>
       /// <param name="data">数据</param>
       /// <returns>格式化数据</returns>
       private static string FormatCSVFIEld(string data)
       {
           return String.Format("\"{0}\"",data.Replace("\"","\"\"\"").Replace("\n","").Replace("\r",""));
       }

       /// <summary>
       /// 导出DataGrID数据到Excel
       /// </summary>
       /// <param name="withheaders">是否需要表头</param>
       /// <param name="grID">DataGrID</param>
       /// <returns>Excel内容字符串</returns>
       public static string ExportDataGrID(bool withheaders,DataGrID grID)
       {
           string colPath; System.Reflection.PropertyInfo propInfo;
           System.windows.Data.Binding binding;
           System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
           System.Collections.IList source = (grID.ItemsSource as System.Collections.IList);
           if (source == null) return "";
           List<string> headers = new List<string>();
           grID.Columns.ToList().ForEach(col =>
           {
               if (col is DataGrIDBoundColumn)
               { headers.Add(FormatCSVFIEld(col.header.ToString())); }
           });
           strBuilder.Append(String.Join(",",headers.ToArray())).Append("\r\n");
           foreach (Object data in source)
           {
               List<string> csvRow = new List<string>();
               foreach (DataGrIDColumn col in grID.Columns)
               {
                   if (col is DataGrIDBoundColumn)
                   {
                       binding = (col as DataGrIDBoundColumn).Binding;
                       colPath = binding.Path.Path;
                       propInfo = data.GetType().GetProperty(colPath);
                       if (propInfo != null)
                       {
                           csvRow.Add(FormatCSVFIEld(propInfo.GetValue(data,null).ToString()));
                       }
                   }
               }
               strBuilder.Append(String.Join(",csvRow.ToArray())).Append("\r\n");
           }
           return strBuilder.ToString();
       }
       /// <summary>
       /// 导出DataGrID数据到Excel
       /// </summary>
       /// <param name="withheaders">是否需要表头</param>
       /// <param name="grID">DataGrID</param>
       /// <returns>Excel内容字符串</returns>
       public static string ExportDataGrID(bool withheaders,DataGrID grID,bool dataBind)
       {
           string colPath;
           System.Reflection.PropertyInfo propInfo;
           System.windows.Data.Binding binding;
           System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
           System.Collections.IList source = (grID.ItemsSource as System.Collections.IList);
           if (source == null) return "";
           List<string> headers = new List<string>();
           grID.Columns.ToList().ForEach(col =>
           {
               if (col is DataGrIDTextColumn)
               {
                   if (col.header != null)
                   {
                       headers.Add(FormatCSVFIEld(col.header.ToString()));
                   }
                   else
                   {
                       headers.Add(string.Empty);
                   }
               }
           });
           strBuilder.Append(String.Join(",headers.ToArray())).Append("\r\n");
           foreach (Object data in source)
           {
               List<string> csvRow = new List<string>();
               foreach (DataGrIDColumn col in grID.Columns)
               {
                   if (col is DataGrIDTextColumn)
                   {
                       FrameworkElement cellContent = col.GetCellContent(data);
                       TextBlock block = cellContent as TextBlock;
                       if (block != null)
                       {
                           csvRow.Add(FormatCSVFIEld(block.Text));
                       }
                   }
               }
               strBuilder.Append(String.Join(",csvRow.ToArray())).Append("\r\n");
           }
           return strBuilder.ToString();

       }

 

http://www.cnblogs.com/3_mu/articles/1651153.html

总结

以上是内存溢出为你收集整理的Silverlight 导出excel (二)全部内容,希望文章能够帮你解决Silverlight 导出excel (二)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1073251.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存