首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

初学者有点有关问题

2013-07-09 
菜鸟有点问题using Systemusing System.Collections.Genericusing System.Linqusing System.Netusing

菜鸟有点问题
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.Windows.Data;
using System.IO;
namespace SilverlightApplication5
{
    public partial class MainPage : UserControl
    {

        public MainPage()
        {
            InitializeComponent();
            dataGrid1.ItemsSource = Users();
            PagedCollectionView pcv = new PagedCollectionView(Users());
            this.dataPager1.PageSize = 4;
            this.dataGrid1.ItemsSource = pcv;
            this.dataPager1.Source = pcv;


        }

        public List<Test> Users()
        {
            List<Test> ListUsers = new List<Test>  
         {  
            new Test{id=1,name="张三",sex="男",age=12},  
            new Test{id=2,name="李四",sex="女",age=15},  
            new Test{id=3,name="王五",sex="男",age=27}, 
            new Test{id=4,name="赵六",sex="女",age=24},
            new Test{id=5,name="张一",sex="女",age=24},
            new Test{id=6,name="张二",sex="男",age=24},
            new Test{id=7,name="张五",sex="男",age=24},
            new Test{id=8,name="张柳",sex="女",age=24},
            new Test{id=9,name="张琪",sex="女",age=24},
            new Test{id=10,name="张九",sex="男",age=24},
            new Test{id=11,name="赵10",sex="女",age=24},

         };
            return ListUsers;
        }

        private void dataPager1_PageIndexChanged(object sender, EventArgs e)
        {

        }
        public class Test
        {
            public int id { set; get; }


            public string name { set; get; }
            public string sex { set; get; }
            public int age { set; get; }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ExportExcel.ExportDataGridSaveAs(true, this.dataGrid1);

        }
        public static class ExportExcel
        {
            #region 导出DataGrid数据到Excel

            /// <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 DataGridTemplateColumn)
                    {
                        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 DataGridTemplateColumn)
                        {
                            FrameworkElement cellContent = col.GetCellContent(data);
                            TextBlock block = null;
                            if (cellContent.GetType() == typeof(Grid))
                            {
                                block = cellContent.FindName("TempTextblock") as TextBlock;
                            }
                            else
                            {
                                block = cellContent as TextBlock;
                            }
                            if (block != null)
                            {


                                csvRow.Add(FormatCSVField(block.Text));
                            }
                        }
                    }
                    strBuilder.Append(String.Join(",", csvRow.ToArray())).Append("\r\n");
                    //strBuilder.Append(String.Join(",", csvRow.ToArray())).Append("\t");
                }
                return strBuilder.ToString();
            }
            /// <summary>
            /// 导出DataGrid数据到Excel为CVS文件
            /// 使用utf8编码 中文是乱码  改用Unicode编码
            /// 
            /// </summary>
            /// <param name="withHeaders">是否带列头</param>
            /// <param name="grid">DataGrid</param>
            public static void ExportDataGridSaveAs(bool withHeaders, DataGrid grid)
            {
                string data = ExportDataGrid(true, grid);
                SaveFileDialog sfd = new SaveFileDialog()
                {
                    DefaultExt = "csv",
                    Filter = "CSV Files (*.csv)|*.csv|All files (*.*)|*.*",
                    FilterIndex = 1
                };
                if (sfd.ShowDialog() == true)
                {
                    using (Stream stream = sfd.OpenFile())
                    {


                        using (StreamWriter writer = new StreamWriter(stream, System.Text.UnicodeEncoding.Unicode))
                        {
                            data = data.Replace(",", "\t");
                            writer.Write(data);
                            writer.Close();
                        }
                        stream.Close();
                    }
                }
            }

            #endregion 导出DataGrid数据到Excel

        }

    }
}
为啥我的程序在网页上有显示 导出到excel的时候就是空白页呢?导出的代码是从网上找的 现在看代码头疼
[解决办法]
白屏的可能有很多可能吧!· 你试试 调试模式下有没有什么异常。

热点排行