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);
                }
            }
        }
    }
    
  • 相关阅读:
    cross apply / outer apply
    查看锁表
    常用的一些占位符
    如何将法向量转换到世界空间
    Essential Mathematics for Games and Interactive Applications
    下一本要看的书
    第五章第五节 四元数
    向量坐标转换、旋转矩阵以及视图转换
    怪诞行为学摘录
    unity3d发布apk资源打包和访问方式
  • 原文地址:https://www.cnblogs.com/ouylvr0625/p/5466647.html
Copyright © 2011-2022 走看看