wpf笔记(二)
2015-10-23 14:36
295 查看
将list导出为json
将datagrid导出为excel
将事件戳转换为datetime
FileStream fs = new FileStream("c:\\zz\\data.json", FileMode.Create); StreamWriter sw = new StreamWriter(fs, Encoding.Default); sw.Write(JsonConvert.SerializeObject(dataList)); sw.Close(); fs.Close();
将datagrid导出为excel
using System; using System.Windows; using System.Windows.Data; using System.Windows.Media; using System.Windows.Controls; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.IO; using System.Reflection; using System.Xml.Linq; using Microsoft.Win32; namespace Citi.Training.WPF.staticMethod { public static class ToExcel { public static void Export(this DataGrid dg) { ExportDataGrid(dg); } public static void ExportDataGrid(DataGrid dGrid) { SaveFileDialog objSFD = new SaveFileDialog() { DefaultExt = "csv", Filter = "CSV Files (*.csv)|*.csv|Excel XML (*.xml)|*.xml|All files (*.*)|*.*", FilterIndex = 1 }; if (objSFD.ShowDialog() == true) { string strFormat = objSFD.SafeFileName.Substring(objSFD.SafeFileName.IndexOf('.') + 1).ToUpper(); StringBuilder strBuilder = new StringBuilder(); if (dGrid.ItemsSource == null) return; List<string> lstFields = new List<string>(); if (dGrid.HeadersVisibility == DataGridHeadersVisibility.Column || dGrid.HeadersVisibility == DataGridHeadersVisibility.All) { foreach (DataGridColumn dgcol in dGrid.Columns) lstFields.Add(FormatField(dgcol.Header.ToString(), strFormat)); BuildStringOfRow(strBuilder, lstFields, strFormat); } foreach (object data in dGrid.ItemsSource) { lstFields.Clear(); foreach (DataGridColumn col in dGrid.Columns) { string strValue = ""; Binding objBinding = null; if (col is DataGridBoundColumn) objBinding = (Binding)(col as DataGridBoundColumn).Binding; if (col is DataGridTemplateColumn) { //This is a template column... let us see the underlying dependency object DependencyObject objDO = (col as DataGridTemplateColumn).CellTemplate.LoadContent(); FrameworkElement oFE = (FrameworkElement)objDO; FieldInfo oFI = oFE.GetType().GetField("TextProperty"); if (oFI != null) { if (oFI.GetValue(null) != null) { if (oFE.GetBindingExpression((DependencyProperty)oFI.GetValue(null)) != null) objBinding = oFE.GetBindingExpression((DependencyProperty)oFI.GetValue(null)).ParentBinding; } } } if (objBinding != null) { if (objBinding.Path.Path != "") { PropertyInfo pi = data.GetType().GetProperty(objBinding.Path.Path); if (pi != null) strValue = pi.GetValue(data, null).ToString(); } if (objBinding.Converter != null) { if (strValue != "") strValue = objBinding.Converter.Convert(strValue, typeof(string), objBinding.ConverterParameter, objBinding.ConverterCulture).ToString(); else strValue = objBinding.Converter.Convert(data, typeof(string), objBinding.ConverterParameter, objBinding.ConverterCulture).ToString(); } } lstFields.Add(FormatField(strValue, strFormat)); } BuildStringOfRow(strBuilder, lstFields, strFormat); } StreamWriter sw = new StreamWriter(objSFD.OpenFile()); if (strFormat == "XML") { //Let us write the headers for the Excel XML sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); sw.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>"); sw.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\">"); sw.WriteLine("<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">"); sw.WriteLine("<Author>Arasu Elango</Author>"); sw.WriteLine("<Created>" + DateTime.Now.ToLocalTime().ToLongDateString() + "</Created>"); sw.WriteLine("<LastSaved>" + DateTime.Now.ToLocalTime().ToLongDateString() + "</LastSaved>"); sw.WriteLine("<Company>Atom8 IT Solutions (P) Ltd.,</Company>"); sw.WriteLine("<Version>12.00</Version>"); sw.WriteLine("</DocumentProperties>"); sw.WriteLine("<Worksheet ss:Name=\"Silverlight Export\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">"); sw.WriteLine("<Table>"); } sw.Write(strBuilder.ToString()); if (strFormat == "XML") { sw.WriteLine("</Table>"); sw.WriteLine("</Worksheet>"); sw.WriteLine("</Workbook>"); } sw.Close(); } } private static void BuildStringOfRow(StringBuilder strBuilder, List<string> lstFields, string strFormat) { switch (strFormat) { case "XML": strBuilder.AppendLine("<Row>"); strBuilder.AppendLine(String.Join("\r\n", lstFields.ToArray())); strBuilder.AppendLine("</Row>"); break; case "CSV": strBuilder.AppendLine(String.Join(",", lstFields.ToArray())); break; } } private static string FormatField(string data, string format) { switch (format) { case "XML": return String.Format("<Cell><Data ss:Type=\"String\">{0}</Data></Cell>", data); case "CSV": return String.Format("\"{0}\"", data.Replace("\"", "\"\"\"").Replace("\n", "").Replace("\r", "")); } return data; } } }
将事件戳转换为datetime
public static System.DateTime ConvertIntDateTime(long d) { System.DateTime time = System.DateTime.MinValue; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); time = startTime.AddMilliseconds(d); return time; }
相关文章推荐
- C#及WPF获取本机所有字体和颜色的方法
- WPF MVVM示例讲解
- PowerShell中调用WPF生成炫酷窗口实例
- WPF实现类似360安全卫士界面的程序源码分享
- C#中WPF使用多线程调用窗体组件的方法
- WPF实现渐变淡入淡出的登陆窗口效果
- WPF的数据绑定详细介绍
- WPF绑定实例详解
- 关于WPF使用MultiConverter控制Button状态的详细介绍
- 如何搭建新的WPF项目框架
- 解析WPF实现音频文件循环顺序播放的解决方法
- WPF Toolkit - February 2010 Release
- 使用WPF创建一个Digg桌面应用
- Barcode Professional for WPF
- WPF数据表格支持vs2010
- Silverlight Gantt甘特图银光,WPF,甘特图,Silverlight,Gantt,任务甘特图,资源甘特图
- WPF甘特图开发包(含任务甘特图和资源甘特图)
- WPF设置窗体最前端显示
- PointFromScreen和PointFromScreen的用法和区别
- 刘铁猛-深入浅出WPF-系列资源汇总