问题描述
导出之后郁闷了.好多的问题,求指教usingSystem;usingSystem.Windows;usingSystem.Windows.Data;usingSystem.Windows.Media;usingSystem.Windows.Controls;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Collections.ObjectModel;usingSystem.Linq;usingSystem.Text;usingSystem.IO;usingSystem.Reflection;usingSystem.Xml.Linq;usingMicrosoft.Win32;usingAspose.Cells;usingSystem.Data;classDataGridExtensions{publicvoidExport(DataGriddg){DataGridExtensionsda=newDataGridExtensions();da.ExportDataGrid(dg);}publicvoidExportDataGrid(DataGriddGrid){SaveFileDialogobjSFD=newSaveFileDialog(){DefaultExt="csv",Filter="CSVFiles(*.csv)|*.csv|ExcelXML(*.xmls)|*.xml|Allfiles(*.*)|*.*",FilterIndex=1};if(objSFD.ShowDialog()==true){stringstrFormat=objSFD.SafeFileName.Substring(objSFD.SafeFileName.IndexOf('.')+1).ToUpper();StringBuilderstrBuilder=newStringBuilder();if(dGrid.ItemsSource==null)return;List<string>lstFields=newList<string>();if(dGrid.HeadersVisibility==DataGridHeadersVisibility.Column||dGrid.HeadersVisibility==DataGridHeadersVisibility.All){/*for(inti=1;i<dGrid.Columns.Count();i++){lstFields.Add(FormatField(dGrid.Columns[i].Header.ToString(),strFormat));BuildStringOfRow(strBuilder,lstFields,strFormat);BuildStringOfRow(strBuilder,lstFields,strFormat);}*/foreach(DataGridColumndgcolindGrid.Columns){if(dgcol.Header==null){}else{lstFields.Add(FormatField(dgcol.Header.ToString(),strFormat));BuildStringOfRow(strBuilder,lstFields,strFormat);BuildStringOfRow(strBuilder,lstFields,strFormat);}}for(intj=2;j<dGrid.Items.Count-1;j++){objectdata=dGrid.Items[j];lstFields.Clear();foreach(DataGridColumncolindGrid.Columns){stringstrValue="";BindingobjBinding=null;if(colisDataGridBoundColumn)objBinding=(Binding)(colasDataGridBoundColumn).Binding;if(colisDataGridTemplateColumn){//Thisisatemplatecolumn...letusseetheunderlyingdependencyobjectDependencyObjectobjDO=(colasDataGridTemplateColumn).CellTemplate.LoadContent();FrameworkElementoFE=(FrameworkElement)objDO;FieldInfooFI=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!=""){PropertyInfopi=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();elsestrValue=objBinding.Converter.Convert(data,typeof(string),objBinding.ConverterParameter,objBinding.ConverterCulture).ToString();}}lstFields.Add(FormatField(strValue,strFormat));}BuildStringOfRow(strBuilder,lstFields,strFormat);}StreamWritersw=newStreamWriter(objSFD.OpenFile());if(strFormat=="XML"){//LetuswritetheheadersfortheExcelXMLsw.WriteLine("<?xmlversion="1.0"encoding="utf-8"?>");sw.WriteLine("<?mso-applicationprogid="Excel.Sheet"?>");sw.WriteLine("<Workbookxmlns="urn:schemas-microsoft-com:office:spreadsheet">");sw.WriteLine("<DocumentPropertiesxmlns="urn:schemas-microsoft-com:office:office">");sw.WriteLine("<Author>ArasuElango</Author>");sw.WriteLine("<Created>"+DateTime.Now.ToLocalTime().ToLongDateString()+"</Created>");sw.WriteLine("<LastSaved>"+DateTime.Now.ToLocalTime().ToLongDateString()+"</LastSaved>");sw.WriteLine("<Company>Atom8ITSolutions(P)Ltd.,</Company>");sw.WriteLine("<Version>12.00</Version>");sw.WriteLine("</DocumentProperties>");sw.WriteLine("<Worksheetss:Name="SilverlightExport"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();if(MessageBox.Show("数据成功导出,文件目录是:"+objSFD.ToString()+";是否立即查看")==MessageBoxResult.OK){};}else{return;}}}privatevoidBuildStringOfRow(StringBuilderstrBuilder,List<string>lstFields,stringstrFormat){switch(strFormat){case"XML":strBuilder.AppendLine("<Row>");strBuilder.AppendLine(String.Join("rn",lstFields.ToArray()));strBuilder.AppendLine("</Row>");break;case"CSV":strBuilder.AppendLine(String.Join(",",lstFields.ToArray()));break;}}privatestringFormatField(stringdata,stringformat){switch(format){case"XML":returnString.Format("<Cell><Datass:Type="String">{0}</Data></Cell>",data);case"CSV":returnString.Format(""{0}"",data.Replace(""",""""").Replace("n","").Replace("r",""));}returndata;}}
解决方案
解决方案二:
一楼,这个,我导出的时候会循环head,然后给导出,内容没有导出
解决方案三:
解决方案四:
我这边没有用datatable,怎么把datagrid转成datatable呢