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);
}
}
}
}

最新文章

  1. 安装SQL提示重启电脑失败,解决办法
  2. R in bioinformatic
  3. Windows下查看局域网内某台计算机的MAC地址
  4. purge mysql自带命令清除binlog
  5. FMDB简单使用
  6. 教你看懂网上流传的60行JavaScript代码俄罗斯方块游戏
  7. springmvc 定时器
  8. ECharts地图中tooltip提示框通过formatter分别显示多个数值
  9. mytbatis配置多数据源
  10. 【Python3爬虫】常见反爬虫措施及解决办法(一)
  11. wpf binging Class 双向绑定 需要实现的接口
  12. 加密解密DES之Android、IOS、C#实现
  13. db2look 工具
  14. mysql常见的错误代码
  15. c++——引用的使用
  16. “数学口袋精灵”第二个Sprint计划(第四天)
  17. java工程师需要学什么
  18. windows安装并破解navicat.
  19. sql server中的大数据的批量操作(批量插入,批量删除)
  20. springweb flux websocket

热门文章

  1. JavaScript学习笔记——1.了解JavaScript
  2. P4768 [NOI2018]归程
  3. Kbuild、Kconfig、make menuconfig、.config、Makefile之间的关系
  4. IIS反向代理
  5. 解决spring使用动态代理
  6. java修饰符的一些知识点
  7. Kibana6.x.x源码分析--启动时basePath
  8. js-eval运算符
  9. P1147 连续自然数和(思维题)
  10. python练习六十三:文件处理,读取文件内容,按内容生成文件