导出execl

/*******************************************************新建一个类写入下面代码********************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Reflection;
using System.Windows.Forms;

namespace DevDemo
{
    public class DataHelper
    {
         /**/

        /// <summary>
        /// List<T> 转换成 DataSet
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static DataSet ConvertToDataSet<T>(List<T> list)
        {
            if (list == null || list.Count <= 0)
            {
                return null;
            }

            DataSet ds = new DataSet();
            DataTable dt = new DataTable(typeof (T).Name);
            DataColumn column;
            DataRow row;

            PropertyInfo[] myPropertyInfo = typeof (T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (T t in list)
            {
                if (t == null)
                {
                    continue;
                }

                row = dt.NewRow();

                for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
                {
                    PropertyInfo pi = myPropertyInfo[i];

                    string name = pi.Name;

                    if (dt.Columns[name] == null)
                    {
                        column = new DataColumn(name, pi.PropertyType);
                        dt.Columns.Add(column);
                    }

                    row[name] = pi.GetValue(t, null);
                }

                dt.Rows.Add(row);
            }

            ds.Tables.Add(dt);

            return ds;
        }

        /**/

        /// <summary>
        /// List<T> 转换成 DataSet
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static DataSet ConvertToDataSetPro<T>(List<T> list)
        {
            //if (list == null || list.Count <= 0)
            //{
            //    return null;
            //}

            DataSet ds = new DataSet();
            DataTable dt = new DataTable(typeof (T).Name);
            DataColumn column;
            DataRow row;

            PropertyInfo[] myPropertyInfo = typeof (T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
            {
                PropertyInfo pi = myPropertyInfo[i];

                string name = pi.Name;

                if (!dt.Columns.Contains(name))
                {
                    column = new DataColumn(name, pi.PropertyType);
                    dt.Columns.Add(column);
                }
            }

            foreach (T t in list)
            {
                if (t == null)
                {
                    continue;
                }

                row = dt.NewRow();

                for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
                {
                    PropertyInfo pi = myPropertyInfo[i];

                    string name = pi.Name;

                    row[name] = pi.GetValue(t, null);
                }

                dt.Rows.Add(row);
            }
            dt.AcceptChanges();
            ds.Tables.Add(dt);
            dt.AcceptChanges();
            return ds;
        }

        public static Hashtable ConvertToHashtable<T>(T obj)
        {
            Hashtable ht = new Hashtable();
            Type entityType = Type.GetType(obj.GetType().AssemblyQualifiedName);
            PropertyInfo[] infos = entityType.GetProperties();
            for (int i = 0; i < infos.Length; i++)
            {
                ht.Add(infos[i].Name, infos[i].GetValue(obj, null));
            }
            return ht;
        }

        /// <summary>
        /// 取得
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public object FindFirst<T>(List<T> list, string name, object value)
        {
            PropertyInfo property = typeof (T).GetProperty(name);
            foreach (T t in list)
            {
                if (property.GetValue(t, null).Equals(value))
                {
                    return t;
                }
            }
            return null;
        }

        public object FindFirst<T>(List<T> list, params object[] args)
        {
            int length = args.Length/2;
            PropertyInfo[] propetrys = new PropertyInfo[length];
            object[] objs = new object[length];

            for (int i = 0; i < length; i++)
            {
                string key = Convert.ToString(args[2*i]);
                propetrys[i] = typeof (T).GetProperty(key);
                objs[i] = args[2*i + 1];
            }

            foreach (T t in list)
            {
                bool isEqual = true;
                for (int i = 0; i < length; i++)
                {
                    if (!propetrys[i].GetValue(t, null).Equals(objs[i]))
                    {
                        isEqual = false;
                        break;
                    }
                }
                if (isEqual)
                {
                    return t;
                }
            }
            return null;
        }

        public static List<T> FindAll<T>(List<T> list, string name, object value)
        {
            List<T> ls = new List<T>();
            PropertyInfo property = typeof (T).GetProperty(name);
            foreach (T t in list)
            {
                if (property.GetValue(t, null).Equals(value))
                {
                    ls.Add(t);
                }
            }
            return ls;
        }

        public List<T> FindAll<T>(List<T> list, params object[] args)
        {
            List<T> ls = new List<T>();
            int length = args.Length/2;
            PropertyInfo[] propetrys = new PropertyInfo[length];
            object[] objs = new object[length];

            for (int i = 0; i < length; i++)
            {
                string key = Convert.ToString(args[2*i]);
                propetrys[i] = typeof (T).GetProperty(key);
                objs[i] = args[2*i + 1];
            }

            foreach (T t in list)
            {
                bool isEqual = true;
                for (int i = 0; i < length; i++)
                {
                    if (!propetrys[i].GetValue(t, null).Equals(objs[i]))
                    {
                        isEqual = false;
                        break;
                    }
                }
                if (isEqual)
                {
                    ls.Add(t);
                }
            }
            return ls;
        }
       
        public List<T> FindAll<T>(List<T> list, string sort)
        {
            return null;
        }

        #region DataGrid导出文件


        public static string ShowSaveFileDialog(string title, string filter)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            string name = Application.ProductName;
            int n = name.LastIndexOf(".") + 1;
            if (n > 0) name = name.Substring(n, name.Length - n);
            dlg.Title = "Export To " + title;
            dlg.FileName = name;
            dlg.Filter = filter;
            if (dlg.ShowDialog() == DialogResult.OK) return dlg.FileName;
            return "";
        }

        public static void OpenFile(string fileName)
        {
            if (MessageBox.Show("是否打开文件?", "导出提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    System.Diagnostics.Process process = new System.Diagnostics.Process();
                    process.StartInfo.FileName = fileName;
                    process.StartInfo.Verb = "Open";
                    process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                    process.Start();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        #endregion
    }
   
}

/********************************************************页面导出事件出写入以下代码*********************************************/
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string fileName = DataHelper.ShowSaveFileDialog("Microsoft Excel Document", "Microsoft Excel|*.xls");
            if (fileName != "")
            {
                gridControl1.ExportToXls(fileName);
                DataHelper.OpenFile(fileName);
            }

        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值