zoukankan      html  css  js  c++  java
  • 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);
                }
            }
        }
    }
    
  • 相关阅读:
    Maximum Flow Exhaustion of Paths Algorithm
    ubuntu下安装java环境
    visualbox使用(二)
    vxworks一个超级奇怪的错误(parse error before `char')
    February 4th, 2018 Week 6th Sunday
    February 3rd, 2018 Week 5th Saturday
    February 2nd, 2018 Week 5th Friday
    February 1st, 2018 Week 5th Thursday
    January 31st, 2018 Week 05th Wednesday
    January 30th, 2018 Week 05th Tuesday
  • 原文地址:https://www.cnblogs.com/ouylvr0625/p/5466647.html
Copyright © 2011-2022 走看看