List转为DataTable并可以导出为Excel

using com.jd120.Core.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ExcelTest = Microsoft.Office.Interop.Excel;

namespace ouylvr.Excel.Test
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public List<Student> lstStudent = new List<Student>();
        DataTable dt;

        public MainWindow()
        {
            InitializeComponent();

            lstStudent.Add(new Student() { name = "Mark", address = "美国", age = 19 });
            lstStudent.Add(new Student() { name = "Tom", address = "英国", age = 18 });
            lstStudent.Add(new Student() { name = "Tom", address = "英国", age = 18 });
            lstStudent.Add(new Student() { name = "Tom", address = "英国", age = 18 });
            lstStudent.Add(new Student() { name = "Tom", address = "英国", age = 18 });
            lstStudent.Add(new Student() { name = "Tom", address = "英国", age = 18 });
            lstStudent.Add(new Student() { name = "Tom", address = "英国", age = 18 });


            lstStudent.Sort(delegate(Student x, Student y)
            {
                return x.ToString().CompareTo(y.ToString());
            });

            lvBook.ItemsSource = lstStudent;
            dt = ListToDataTable.ToDataTable(lstStudent);
            dt = Tools.ConvertToDataTable(lstStudent);
        }

        public class ListToDataTable
        {
            public static DataTable ToDataTable<T>(List<T> items)
            {
                DataTable dataTable = new DataTable(typeof(T).Name);
                PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
                foreach (PropertyInfo prop in Props)
                {
                    dataTable.Columns.Add(prop.Name);
                }
                foreach (T item in items)
                {
                    var values = new object[Props.Length];
                    for (int i = 0; i < Props.Length; i++)
                    {
                        values[i] = Props[i].GetValue(item, null);
                    }
                    dataTable.Rows.Add(values);
                }
                return dataTable;
            }
        }

        public class Student
        {
            public string name { get; set; }
            public string address { get; set; }
            public int age { get; set; }
        }

        private void btnDaoChu_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ExcelTest.Application excelApp = new ExcelTest.Application();
                excelApp.Workbooks.Add();
                ExcelTest._Worksheet workSheet = excelApp.ActiveSheet;

                int row = 0;
                foreach (var column in dt.Columns)
                {
                    workSheet.Cells[1, ((char)('A' + row)).ToString()] = column.ToString();
                    row++;
                }

                row = 1;

                for (int x = 0; x < dt.Rows.Count; x++)
                {
                    row++;
                    for (int y = 0; y < dt.Columns.Count; y++)
                    {
                        workSheet.Cells[row, ((char)('A' + y)).ToString()] = dt.Rows[x][y];
                    }
                }
                 if(!File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)))
                 {
                     workSheet.SaveAs(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + DateTime.Now.ToString("yyyyMMdd"));
                     MessageBox.Show("已经保存在桌面上", "导出成功");
                 } 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/ouylvr0625/p/5466647.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值