Silverlight 4 Hosting :: Export from Silverlight

Below is the code for export data from silver light code.

With this code user can export data in (csv formate) by very fast performance results.

In this code need to read data from grid and convert in one csv format as tab separator. And that file data need to save in one file with xls extension with save dialog box by silver light So user can save it any where in client machine.

This Process gives very efficient results because all code executes in client machine by Silver light.

PagedCollectionView pageview = (PagedCollectionView)(dgReportData.ItemsSource);
try
{

SaveFileDialog sfd = new SaveFileDialog()
{
DefaultExt = "xls",
Filter = "xls Files (*.xls)|*.xls|All files (*.*)|*.*",
FilterIndex = 1
}; 

if (sfd.ShowDialog() == true)
{  

public string ExportDataGrid(bool withHeaders, DataGrid grid, PagedCollectionView pageview)
{ 

string colPath;
System.Reflection.PropertyInfo propInfo;
//System.Windows.Data.Binding binding;
strBuilder = new System.Text.StringBuilder(); 

List<string> headers = new List<string>(); 

if (stkPanelTotal.Visibility == Visibility.Visible)
{
if (TBTotalMRC.Text != "")
{
strBuilder.Append(TBTotalMRC.Text).Append("\t");
}
if (TBTotalNRC.Text != "")
{
strBuilder.Append(TBTotalNRC.Text).Append("\t\n");
}
} 

grid.Columns.ToList().ForEach(col =>
{
if (col is DataGridBoundColumn || col is DataGridTemplateColumn)
{
if (col.Visibility == Visibility.Visible)
{
headers.Add(FormatCSVField(col.Header.ToString()));
}
}
});
strBuilder
.Append(String.Join("\t", headers.ToArray()))
.Append("\r\n"); 

foreach (Object data in pageview.SourceCollection)
{
grid.ScrollIntoView(data, null); 

List<string> csvRow = new List<string>();
foreach (DataGridColumn col in grid.Columns)
{
if (col is DataGridBoundColumn || col is DataGridTemplateColumn)
{
if (col is DataGridBoundColumn)
{
objBinding = (col as DataGridBoundColumn).Binding; 

} 

if (col is DataGridTemplateColumn)
{ 

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;
}
}
objDO = null;
oFE = null;
oFI = null;
}   

colPath = objBinding.Path.Path;
propInfo = data.GetType().GetProperty(colPath);
if (propInfo != null)
{
csvRow.Add(FormatCSVField(Convert.ToString(propInfo.GetValue(data, null))));  

}
propInfo = null;
}
}
}
strBuilder
.Append(String.Join("\t", csvRow.ToArray()))
.Append("\r\n");
}
return strBuilder.ToString();
} 

PagedCollectionView CollectionView = new PagedCollectionView(pageview.SourceCollection);
dgTQShowDataPager.Source = CollectionView;
dgReportData.ItemsSource = CollectionView;
using (Stream stream = sfd.OpenFile())
{
using (StreamWriter writer = new StreamWriter(stream, System.Text.Encoding.Unicode))
{
writer.Write(strBuilder);
writer.Close();
}
stream.Close();

}
}
private string FormatCSVField(string data)
{ 

return String.Format("\"{0}\"",
data.Replace("\"", "\"\"\"")
.Replace("\n", "")
.Replace("\r", "")
);
}

Comments are closed.

Sponsors

JaringanHosting.com

Cheap Reseller Hosting

Best Blog Hosting Provider

Best European ASP.NET 4.0 Hosting Provider