public static void SaveToExcel(object parameter, RadGridView radGridView, params string[] columnsRequiredFormatting)
{
try
{
string newLine = "\n";
string tab = "\t";
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Excel Files (.xls)|*.xls|All Files (*.*)|*.*";
var objectList = (IList)parameter;
bool? dialogResult = saveFileDialog.ShowDialog();
if (dialogResult.Value)
{
TextWriter writer = new StreamWriter(saveFileDialog.OpenFile());
try
{
IList<
string
> binders = new List<
string
>();
writer.Write(string.Format("<
html
xmlns:x=\"urn:schemas-microsoft-com:office:excel\"> <
table
border=\"1\" width=\"100%\">"));
if (radGridView != null)
{
writer.Write(string.Format("<
thead
>"));
writer.Write(string.Format("<
tr
>"));
binders = radGridView.Columns.Where(c => c.Visibility == Visibility.Visible).Select(c =>
{
writer.Write("<
th
bgcolor=\"#5882FA\">" + c.Header.ToString() + "</
th
>");
writer.Write(tab);
return c.ClipboardContentBinding.Path.Path.ToString();
}).ToList();
writer.Write(string.Format("</
tr
>"));
writer.Write(string.Format("</
thead
>"));
}
writer.Write(newLine);
if (objectList.Count == 0)
{
writer.Write(string.Format("<
tbody
>"));
writer.Write(string.Format("<
tr
>"));
writer.WriteLine("There are no rows to be written under this section");
writer.Write(string.Format("</
tr
>"));
writer.Write(string.Format("</
tbody
>"));
writer.Write(newLine);
writer.Write(newLine);
return;
}
string tdFormat = "<
td
x:str=\"'{0}\">{0}</
td
>";
string tdSimple = "<
td
>{0}</
td
>";
writer.Write(string.Format("<
tbody
>"));
if (columnsRequiredFormatting == null)
{
foreach (var reportData in objectList)
{
writer.Write(string.Format("<
tr
>"));
foreach (string binder in binders)
{
//Bar the properties which should not go
string propVal = string.Empty;
PropertyInfo propertyInfo = reportData.GetType().GetProperty(binder);
object prop = reportData.GetType().GetProperty(binder).GetValue(reportData, null);
if (prop != null)
propVal = prop.ToString();
writer.Write(String.Format(tdSimple, propVal));
}
writer.Write(string.Format("</
tr
>"));
}
}
else
{
foreach (var reportData in objectList)
{
writer.Write(string.Format("<
tr
>"));
//IEnumerable<
bool
> a = binders.Select(c =>
// {
// object propVal = "";
// bool formatReq = false;
// PropertyInfo prop = reportData.GetType().GetProperty(c);
// propVal = prop.GetValue(reportData, null);
// if (columnsRequiredFormatting.Contains(prop.Name.ToString()))
// formatReq = true;
// if (formatReq)
// writer.Write(String.Format(tdFormat, propVal.ToString()));
// else
// writer.Write(String.Format(tdSimple, propVal.ToString()));
// return true;
// });
foreach (string binder in binders)
{
//Bar the properties which should not go
string propVal = string.Empty;
PropertyInfo propertyInfo = reportData.GetType().GetProperty(binder);
object prop = reportData.GetType().GetProperty(binder).GetValue(reportData, null);
if (prop != null)
propVal = prop.ToString();
if (columnsRequiredFormatting.Contains(propertyInfo.Name.ToString()))
writer.Write(String.Format(tdFormat, propVal));
else
writer.Write(String.Format(tdSimple, propVal));
}
writer.Write(string.Format("</
tr
>"));
}
}
writer.Write(string.Format("</
tbody
>"));
writer.Write("</
table
> </
html
>");
}
finally
{
if (writer != null)
{
writer.Flush();
writer.Close();
}
}
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.ToString());
throw;
}
}
I am using Save to excel code above Parameter, for That Save to excel even i put this code in view model,