private void btnExport_Click(object sender, RoutedEventArgs e) { string extension = "xls"; SaveFileDialog dialog = new SaveFileDialog() { DefaultExt = extension, Filter = String.Format("{1} files (.{0})|.{0}|All files (.)|.", extension, "Excel"), FilterIndex = 1 }; if (dialog.ShowDialog() == true) { var col0Visibility = radGridView.Columns[0].IsVisible; radGridView.Columns[0].IsVisible = false;//ستون هایی که نمی خواهیم در اکسل دیده شوند var opt = new GridViewExportOptions() { Format = ExportFormat.Html, ShowColumnHeaders = true, ShowColumnFooters = true, ShowGroupFooters = false, }; using (Stream stream = dialog.OpenFile()) { radGridView.Export(stream, opt); } radGridView.Columns[0].IsVisible = col0Visibility;//این ستون در بالا مخفی شده بود، حالا به حالت اول باز گردانده می شود } }
public void Export(object parameter) { var grid = parameter as RadGridView; if (grid != null) { grid.InitializingExcelMLStyles -= InitializingExcelMLStyles; grid.InitializingExcelMLStyles += InitializingExcelMLStyles; grid.ElementExporting -= this.ElementExporting; grid.ElementExporting += this.ElementExporting; string extension = "xls"; var format = ExportFormat.ExcelML; var dialog = new SaveFileDialog(); dialog.DefaultExt = extension; dialog.Filter = String.Format("Excel files (*.{0})|*.{0}|All files (*.*)|*.*", extension); dialog.FilterIndex = 1; if (dialog.ShowDialog() == true) { using (var stream = dialog.OpenFile()) { var exportOptions = new GridViewExportOptions(); exportOptions.Format = format; exportOptions.ShowColumnFooters = true; exportOptions.ShowColumnHeaders = true; exportOptions.ShowGroupFooters = true; grid.Export(stream, exportOptions); } } } }
public static void ExcelExport(RadGridView gv) { if (gv.HasItems) { SaveFileDialog dialog = new SaveFileDialog(); dialog.DefaultExt = "xls"; dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", "xls", "Excel"); dialog.FilterIndex = 1; if (dialog.ShowDialog() == true) { using (Stream stream = dialog.OpenFile()) { GridViewExportOptions exportOptions = new GridViewExportOptions(); exportOptions.Format = ExportFormat.Html; exportOptions.Encoding = Encoding.BigEndianUnicode; exportOptions.ShowColumnFooters = true; exportOptions.ShowColumnHeaders = true; //exportOptions.ShowGroupFooters = true; gv.Export(stream, exportOptions); } MessageBox.Show("导出完毕"); } } else { MessageBox.Show("没有可导出的数据"); } }
public void Export(object parameter) { RadGridView grid = parameter as RadGridView; if (grid != null) { grid.ElementExporting -= this.ElementExporting; grid.ElementExporting += this.ElementExporting; grid.ElementExported -= this.ElementExported; grid.ElementExported += this.ElementExported; string extension = "xls"; ExportFormat format = ExportFormat.Html; SaveFileDialog dialog = new SaveFileDialog(); dialog.DefaultExt = extension; dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, "Excel"); dialog.FilterIndex = 1; if (dialog.ShowDialog() == true) { using (Stream stream = dialog.OpenFile()) { GridViewExportOptions options = new GridViewExportOptions(); options.Format = format; options.ShowColumnHeaders = true; options.Encoding = System.Text.Encoding.UTF8; grid.Export(stream, options); } } } }
//Crystal View DataSourch public DataTable CristalReportDataSourch(Telerik.Windows.Controls.RadGridView radgridview, DataTable tempDataTable, Variables.OperationTrypes oprationType) { string txtline = string.Empty; NecessaryFunction necessaryElement = new NecessaryFunction(); GridViewExportOptions exportOption = new GridViewExportOptions(); exportOption.Format = ExportFormat.Text; string fileNameWithPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DATA_EXPORT.txt"); necessaryElement.CreateTextFile(fileNameWithPath); FileStream fileStream = new FileStream(fileNameWithPath, FileMode.OpenOrCreate, FileAccess.Write); radgridview.Export((Stream)fileStream, exportOption); fileStream.Close(); fileStream.Dispose(); StreamReader txtReader = new StreamReader(fileNameWithPath); while ((txtline = txtReader.ReadLine()) != null) { char[] delimiterChars = { ' ', '"', '\t' }; String[] columnData = txtline.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries); if (oprationType.Equals(Variables.OperationTrypes.Sales)) { tempDataTable.Rows.Add(columnData[0].Trim(), columnData[1].Trim(), columnData[2].Trim(), columnData[3].Trim()); } else if (oprationType.Equals(Variables.OperationTrypes.InvPurDebCre)) { tempDataTable.Rows.Add(columnData[0].Trim(), columnData[3].Trim(), columnData[4].Trim(), columnData[5].Trim(), columnData[6].Trim(), columnData[7].Trim(), columnData[8].Trim(), columnData[9].Trim(), columnData[11].Trim(), columnData[10].Trim()); } } txtReader.Close(); txtReader.Dispose(); necessaryElement.DeleteFile(fileNameWithPath); return(tempDataTable); }
//Crystal View DataSourch public DataTable CristalReportDataSourch(Telerik.Windows.Controls.RadGridView radgridview, DataTable tempDataTable, Variables.OperationTrypes oprationType) { string txtline = string.Empty; NecessaryFunction necessaryElement = new NecessaryFunction(); GridViewExportOptions exportOption = new GridViewExportOptions(); exportOption.Format = ExportFormat.Text; string fileNameWithPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DATA_EXPORT.txt"); necessaryElement.CreateTextFile(fileNameWithPath); FileStream fileStream = new FileStream(fileNameWithPath, FileMode.OpenOrCreate, FileAccess.Write); radgridview.Export((Stream)fileStream, exportOption); fileStream.Close(); fileStream.Dispose(); StreamReader txtReader = new StreamReader(fileNameWithPath); while ((txtline=txtReader.ReadLine())!=null) { char[] delimiterChars = { ' ', '"', '\t' }; String[] columnData = txtline.Split(delimiterChars,StringSplitOptions.RemoveEmptyEntries); if (oprationType.Equals(Variables.OperationTrypes.Sales)) { tempDataTable.Rows.Add(columnData[0].Trim(), columnData[1].Trim(), columnData[2].Trim(), columnData[3].Trim()); } else if (oprationType.Equals(Variables.OperationTrypes.InvPurDebCre)) { tempDataTable.Rows.Add(columnData[0].Trim(), columnData[3].Trim(), columnData[4].Trim(), columnData[5].Trim(), columnData[6].Trim(), columnData[7].Trim(), columnData[8].Trim(), columnData[9].Trim(), columnData[11].Trim(), columnData[10].Trim()); } } txtReader.Close(); txtReader.Dispose(); necessaryElement.DeleteFile(fileNameWithPath); return tempDataTable; }
public void Export(object parameter) { var grid = parameter as RadGridView; if (grid != null) { grid.ElementExporting -= this.ElementExporting; grid.ElementExporting += this.ElementExporting; string extension = ""; var format = ExportFormat.Html; switch (SelectedExportFormat) { case "Excel": extension = "xls"; format = ExportFormat.Html; break; case "ExcelML": extension = "xml"; format = ExportFormat.ExcelML; break; case "Word": extension = "doc"; format = ExportFormat.Html; break; case "Csv": extension = "csv"; format = ExportFormat.Csv; break; } var dialog = new SaveFileDialog(); dialog.DefaultExt = extension; dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, SelectedExportFormat); dialog.FilterIndex = 1; if (dialog.ShowDialog() == true) { using (var stream = dialog.OpenFile()) { var exportOptions = new GridViewExportOptions(); exportOptions.Format = format; exportOptions.ShowColumnFooters = true; exportOptions.ShowColumnHeaders = true; exportOptions.ShowGroupFooters = true; exportOptions.Encoding = Encoding.Unicode; grid.Export(stream, exportOptions); } } } }
/// <summary> /// 设置导出样式 /// </summary> /// <returns></returns> public static GridViewExportOptions SetGridViewExportOptions() { GridViewExportOptions exportOptions = new GridViewExportOptions(); exportOptions.Format = ExportFormat.Html; exportOptions.Encoding = System.Text.Encoding.UTF8; exportOptions.ShowColumnFooters = false; exportOptions.ShowColumnHeaders = true; exportOptions.ShowGroupFooters = false; return exportOptions; }
public static void BillExportExcel(string title, RadGridView gv, object item) { var row = (GridViewRow)gv.ItemContainerGenerator.ContainerFromItem(item);//View.Extension.UIHelper.GetAncestor<GridViewRow>(sender as RadButton); row.DetailsVisibility = Visibility.Visible; var detailsPresenter = row.ChildrenOfType <DetailsPresenter>().FirstOrDefault(); // same as e.DetailsElement from gridView_RowDetailsVisibilityChanged var gvDetails = (RadGridView)detailsPresenter.Content; Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog(); dialog.DefaultExt = "xls"; dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", "xls", "Excel"); dialog.FilterIndex = 1; if (dialog.ShowDialog() == true) { try { using (System.IO.Stream stream = dialog.OpenFile()) { System.IO.StreamWriter writer = new System.IO.StreamWriter(stream); writer.WriteLine(@"<table border=""1"">"); writer.WriteLine(@"<tr><td style=""background-color:#CCC;text-align:center;""><b>"); writer.WriteLine(title); writer.WriteLine("</b></td></tr><tr><td>"); foreach (var col in gv.Columns) { if (col.Header != null)//过滤没有列头(如标识列)的列 { var colname = col.Header.ToString(); if (colname != "操作" && col is IExportableColumn && col.IsVisible) { writer.Write(@"<b>{0}:</b> {1} <br />", colname, ((IExportableColumn)col).GetCellContent(item)); } } } writer.WriteLine("</td></tr><tr><td>"); writer.Flush(); GridViewExportOptions exportOptions = new GridViewExportOptions(); exportOptions.Format = ExportFormat.Html; exportOptions.ShowColumnHeaders = true; gvDetails.Export(stream, exportOptions); writer.WriteLine("</td></tr></table>"); writer.Flush(); } MessageBox.Show("导出完毕"); } catch (Exception e) { MessageBox.Show("导出失败,失败原因:\n" + e.Message); } } }
/// <summary> /// 设置导出样式 /// </summary> /// <returns></returns> public static GridViewExportOptions SetGridViewExportOptions() { var exportOptions = new GridViewExportOptions { Format = ExportFormat.Html, Encoding = System.Text.Encoding.UTF8, ShowColumnFooters = false, ShowColumnHeaders = true, ShowGroupFooters = false }; return exportOptions; }
private void gvExport_DataLoaded(object sender, EventArgs e) { if (gvExport.Items.Count > 1) { string extension = "xls"; Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog() { DefaultExt = extension, Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, "Excel"), FilterIndex = 1 }; if (dialog.ShowDialog() == true) { using (System.IO.Stream stream = dialog.OpenFile()) { firstRow = null; firstCell = true; gvExport.Visibility = System.Windows.Visibility.Visible; gvExport.InitializingExcelMLStyles -= InitializingExcelMLStyles; gvExport.InitializingExcelMLStyles += InitializingExcelMLStyles; gvExport.ElementExporting -= ElementExporting; gvExport.ElementExporting += ElementExporting; GridViewExportOptions options = new GridViewExportOptions() { Format = Telerik.Windows.Controls.ExportFormat.ExcelML, Encoding = Encoding.UTF8, ShowColumnHeaders = false, ShowColumnFooters = true, ShowGroupFooters = false }; gvExport.Export(stream, options); gvExport.Visibility = System.Windows.Visibility.Hidden; } } } else { MessageBox.Show("No se encontraron pacientes para exportar con incidencias en los módulos seleccionados entre las fechas ingresadas para las áreas y usuarios seleccionados para exportar", "Información", MessageBoxButton.OK, MessageBoxImage.Information); } }
public void ExportWithOptions(object parameter, GridViewExportOptions exportOptions) { RadGridView grid = parameter as RadGridView; if (grid != null) { grid.ElementExporting -= this.ElementExporting; grid.ElementExporting += this.ElementExporting; string extension = ""; switch (SelectedExportFormat) { case "Excel": extension = "xls"; break; case "ExcelML": extension = "xml"; break; case "Word": extension = "doc"; break; case "Csv": extension = "csv"; break; } SaveFileDialog dialog = new SaveFileDialog(); dialog.DefaultExt = extension; dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, SelectedExportFormat); dialog.FilterIndex = 1; if (dialog.ShowDialog() == true) { using (Stream stream = dialog.OpenFile()) { grid.Export(stream, exportOptions); } } } }
public void ExportWithHeader(List <object> parameter, List <Header> headers, List <bool> showFooter) { string extension = ""; ExportFormat format = ExportFormat.Html; switch (SelectedExportFormat) { case "Excel": extension = "xls"; format = ExportFormat.Html; break; case "ExcelML": extension = "xml"; format = ExportFormat.ExcelML; break; case "Word": extension = "doc"; format = ExportFormat.Html; break; case "Csv": extension = "csv"; format = ExportFormat.Csv; break; } SaveFileDialog dialog = new SaveFileDialog(); dialog.DefaultExt = extension; dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, SelectedExportFormat); dialog.FilterIndex = 1; if (dialog.ShowDialog() == true) { using (Stream stream = dialog.OpenFile()) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < parameter.Count; i++) { string header = createHeaderStr(headers[i]); RadGridView grid = parameter[i] as RadGridView; if (grid != null) { grid.ElementExporting -= this.ElementExporting; grid.ElementExporting += this.ElementExporting; GridViewExportOptions exportOptions = new GridViewExportOptions(); exportOptions.Format = format; exportOptions.ShowColumnFooters = showFooter[i]; exportOptions.ShowColumnHeaders = true; exportOptions.ShowGroupFooters = true; exportOptions.Encoding = Encoding.UTF8; using (MemoryStream ms = new MemoryStream()) { grid.Export(ms, exportOptions); byte[] bs = new byte[ms.Length]; ms.Seek(0, SeekOrigin.Begin); ms.Read(bs, 0, bs.Length); string str = Encoding.UTF8.GetString(bs, 0, bs.Length); string str1 = str.Insert(str.IndexOf('>', 0) + 1, header); sb.Append(str1); } } } byte[] bysall = Encoding.UTF8.GetBytes(sb.ToString()); stream.Seek(0, SeekOrigin.Begin); stream.Write(bysall, 0, bysall.Length); stream.Flush(); } } }
public void ExportWithHeader(object parameter, Header header) { RadGridView grid = parameter as RadGridView; if (grid != null) { grid.ElementExporting -= this.ElementExporting; grid.ElementExporting += this.ElementExporting; string extension = ""; ExportFormat format = ExportFormat.Html; switch (SelectedExportFormat) { case "Excel": extension = "xls"; format = ExportFormat.Html; break; case "ExcelML": extension = "xml"; format = ExportFormat.ExcelML; break; case "Word": extension = "doc"; format = ExportFormat.Html; break; case "Csv": extension = "csv"; format = ExportFormat.Csv; break; } SaveFileDialog dialog = new SaveFileDialog(); dialog.DefaultExt = extension; dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, SelectedExportFormat); dialog.FilterIndex = 1; if (dialog.ShowDialog() == true) { using (Stream stream = dialog.OpenFile()) { GridViewExportOptions exportOptions = new GridViewExportOptions(); exportOptions.Format = format; exportOptions.ShowColumnFooters = false; exportOptions.ShowColumnHeaders = true; exportOptions.ShowGroupFooters = true; exportOptions.Encoding = Encoding.UTF8; grid.Export(stream, exportOptions); byte[] bs = new byte[stream.Length]; stream.Seek(0, SeekOrigin.Begin); stream.Read(bs, 0, bs.Length); string str = Encoding.UTF8.GetString(bs, 0, bs.Length); string str1 = str.Insert(str.IndexOf('>', 0) + 1, createHeaderStr(header)); byte[] bys = Encoding.UTF8.GetBytes(str1); stream.Seek(0, SeekOrigin.Begin); stream.Write(bys, 0, bys.Length); stream.Flush(); } } } }
public static Workbook CreateWorkBook(RadGridView grid) { Workbook book = null; using (var stream = new MemoryStream()) { int index = 0; foreach (GroupDescriptor group in grid.GroupDescriptors) { grid.Columns.Insert(index++, new GridViewDataColumn() { DataMemberBinding = new System.Windows.Data.Binding(group.Member) }); } grid.ElementExporting += elementExporting; var exportOptions = new GridViewExportOptions() { Format = ExportFormat.Csv, ShowColumnFooters = grid.ShowColumnFooters, ShowColumnHeaders = grid.ShowColumnHeaders, ShowGroupFooters = grid.ShowGroupFooters, Culture = new CultureInfo("fr-FR"), Items = grid.Items }; grid.Export(stream, exportOptions); grid.ElementExporting -= elementExporting; stream.Position = 0; var csvProvider = new CsvFormatProvider(); book = csvProvider.Import(stream); // /!\ telerik date format workaround var pattern = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern; var dateCellFormat = new CellValueFormat(pattern); var dateColsIdx = grid.Columns.Cast<GridViewColumn>().OfType<GridViewDataColumn>().Where(col => col.DataType == typeof(DateTime)).Select(col => col.DisplayIndex).ToList(); if (dateColsIdx.Any()) { var skipFirstRow = true; var rowCount = grid.Items.Count; for (int i = skipFirstRow ? 1 : 0; i < rowCount; i++) { foreach (var colIdx in dateColsIdx) { book.Worksheets[0].Cells[i, colIdx].SetFormat(dateCellFormat); } } } foreach (GroupDescriptor group in grid.GroupDescriptors) { grid.Columns.RemoveAt(0); } } return book; }