using System;
using System.Collections.Generic;
using System.Linq;
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.Shapes;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Collections;
using System.Windows.Controls.Primitives;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using System.Data;
namespace Binnovitec.GSMController.Dashboard.Helper
{
public static class Export
{
public static void ExportToPdf(DataGrid grid, string pathOfPdfWithFileName)
{
PdfPTable table = new PdfPTable(grid.Columns.Count);
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter writer = PdfWriter.GetInstance(doc, new System.IO.FileStream(pathOfPdfWithFileName, System.IO.FileMode.Create));
doc.Open();
for (int j = 0; j < grid.Columns.Count; j++)
{
table.AddCell(new Phrase(grid.Columns[j].Header.ToString()));
}
table.HeaderRows = 1;
IEnumerable itemsSource = grid.ItemsSource as IEnumerable;
if (itemsSource != null)
{
foreach (var item in itemsSource)
{
DataGridRow row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
if (row != null)
{
DataGridCellsPresenter presenter = FindVisualChild<DataGridCellsPresenter>(row);
for (int i = 0; i < grid.Columns.Count; ++i)
{
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(i);
TextBlock txt = cell.Content as TextBlock;
if (txt != null)
{
table.AddCell(new Phrase(txt.Text));
}
}
}
}
doc.Add(table);
doc.Close();
}
}
public static void ExportToCsv(DataGrid dgDisplay, string pathOfCSVWithFileName)
{
int h = 0;
h = dgDisplay.Items.Count;
dgDisplay.SelectAllCells();
dgDisplay.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, dgDisplay);
String resultat = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);
String result = (string)Clipboard.GetData(DataFormats.Text);
dgDisplay.UnselectAllCells();
System.IO.StreamWriter file = new System.IO.StreamWriter(pathOfCSVWithFileName);
file.WriteLine(resultat);
file.Close();
// file1.Close();
MessageBox.Show("Excel file created.xls");
}
public static void ExportToExcel(DataGrid dgDisplay, string pathOfExcelWithFileName) //Microsoft.Office.Interop.Excel.Application
{
int i = 0;
int k = 1, h = 1;
GetDataGridRows(dgDisplay);
var rows = GetDataGridRows(dgDisplay);
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook ExcelBook;
Microsoft.Office.Interop.Excel._Worksheet ExcelSheet;
ExcelBook = (Microsoft.Office.Interop.Excel._Workbook)ExcelApp.Workbooks.Add(1);
ExcelSheet = (Microsoft.Office.Interop.Excel._Worksheet)ExcelBook.ActiveSheet;
for (i = 1; i <= dgDisplay.Columns.Count; i++)
{
ExcelSheet.Cells[1, i] = dgDisplay.Columns[i - 1].Header.ToString();
}
foreach (DataGridRow r in rows)
{
DataRowView rv = (DataRowView)r.Item;
foreach (DataGridColumn column in dgDisplay.Columns)
{
if (column.GetCellContent(r) is TextBlock)
{
TextBlock cellContent = column.GetCellContent(r) as TextBlock;
ExcelSheet.Cells[h + 1, k] = cellContent.Text.Trim();
k++;
}
}
k = 1;
h++;
}
//ExcelApp.Visible = true;
//ExcelApp.DisplayAlerts = false;
ExcelBook.SaveAs(pathOfExcelWithFileName, Excel.XlFileFormat.xlOpenXMLWorkbook, Missing.Value, Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlNoChange,
Excel.XlSaveConflictResolution.xlUserResolution, true,
Missing.Value, Missing.Value, Missing.Value);
ExcelBook.Close(pathOfExcelWithFileName, Missing.Value, Missing.Value);
ExcelSheet = null;
ExcelBook = null;
ExcelApp = null;
// return ExcelApp;
}
private static IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid)
{
var itemsSource = grid.ItemsSource as IEnumerable;
if (null == itemsSource) yield return null;
foreach (var item in itemsSource)
{
var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
if (null != row) yield return row;
}
}
private static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is T)
return (T)child;
else
{
T childOfChild = FindVisualChild<T>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
}
}
using System.Collections.Generic;
using System.Linq;
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.Shapes;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Collections;
using System.Windows.Controls.Primitives;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using System.Data;
namespace Binnovitec.GSMController.Dashboard.Helper
{
public static class Export
{
public static void ExportToPdf(DataGrid grid, string pathOfPdfWithFileName)
{
PdfPTable table = new PdfPTable(grid.Columns.Count);
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter writer = PdfWriter.GetInstance(doc, new System.IO.FileStream(pathOfPdfWithFileName, System.IO.FileMode.Create));
doc.Open();
for (int j = 0; j < grid.Columns.Count; j++)
{
table.AddCell(new Phrase(grid.Columns[j].Header.ToString()));
}
table.HeaderRows = 1;
IEnumerable itemsSource = grid.ItemsSource as IEnumerable;
if (itemsSource != null)
{
foreach (var item in itemsSource)
{
DataGridRow row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
if (row != null)
{
DataGridCellsPresenter presenter = FindVisualChild<DataGridCellsPresenter>(row);
for (int i = 0; i < grid.Columns.Count; ++i)
{
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(i);
TextBlock txt = cell.Content as TextBlock;
if (txt != null)
{
table.AddCell(new Phrase(txt.Text));
}
}
}
}
doc.Add(table);
doc.Close();
}
}
public static void ExportToCsv(DataGrid dgDisplay, string pathOfCSVWithFileName)
{
int h = 0;
h = dgDisplay.Items.Count;
dgDisplay.SelectAllCells();
dgDisplay.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, dgDisplay);
String resultat = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);
String result = (string)Clipboard.GetData(DataFormats.Text);
dgDisplay.UnselectAllCells();
System.IO.StreamWriter file = new System.IO.StreamWriter(pathOfCSVWithFileName);
file.WriteLine(resultat);
file.Close();
// file1.Close();
MessageBox.Show("Excel file created.xls");
}
public static void ExportToExcel(DataGrid dgDisplay, string pathOfExcelWithFileName) //Microsoft.Office.Interop.Excel.Application
{
int i = 0;
int k = 1, h = 1;
GetDataGridRows(dgDisplay);
var rows = GetDataGridRows(dgDisplay);
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook ExcelBook;
Microsoft.Office.Interop.Excel._Worksheet ExcelSheet;
ExcelBook = (Microsoft.Office.Interop.Excel._Workbook)ExcelApp.Workbooks.Add(1);
ExcelSheet = (Microsoft.Office.Interop.Excel._Worksheet)ExcelBook.ActiveSheet;
for (i = 1; i <= dgDisplay.Columns.Count; i++)
{
ExcelSheet.Cells[1, i] = dgDisplay.Columns[i - 1].Header.ToString();
}
foreach (DataGridRow r in rows)
{
DataRowView rv = (DataRowView)r.Item;
foreach (DataGridColumn column in dgDisplay.Columns)
{
if (column.GetCellContent(r) is TextBlock)
{
TextBlock cellContent = column.GetCellContent(r) as TextBlock;
ExcelSheet.Cells[h + 1, k] = cellContent.Text.Trim();
k++;
}
}
k = 1;
h++;
}
//ExcelApp.Visible = true;
//ExcelApp.DisplayAlerts = false;
ExcelBook.SaveAs(pathOfExcelWithFileName, Excel.XlFileFormat.xlOpenXMLWorkbook, Missing.Value, Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlNoChange,
Excel.XlSaveConflictResolution.xlUserResolution, true,
Missing.Value, Missing.Value, Missing.Value);
ExcelBook.Close(pathOfExcelWithFileName, Missing.Value, Missing.Value);
ExcelSheet = null;
ExcelBook = null;
ExcelApp = null;
// return ExcelApp;
}
private static IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid)
{
var itemsSource = grid.ItemsSource as IEnumerable;
if (null == itemsSource) yield return null;
foreach (var item in itemsSource)
{
var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
if (null != row) yield return row;
}
}
private static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is T)
return (T)child;
else
{
T childOfChild = FindVisualChild<T>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
}
}
No comments:
Post a Comment
Don't judge me like you know me.Don't talk to me if you don't like me. I am my own person and If you don't like that i don't give a f***