public class ExcelHelper
{
public static void ExportListToExcel(List
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AppendHeader("Content-Type", "application/vnd.ms-excel");
HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment; filename=" + xlsName + ".xls");
Int32 success = 0;
string sep = "";
try
{
PropertyInfo[] fieldInfo = listToExport[0].GetType().GetProperties();
foreach (PropertyInfo col in fieldInfo)
{
HttpContext.Current.Response.Write(sep + col.Name);
sep = "\t";
}
HttpContext.Current.Response.Write("\n");
foreach (Blog dataItem in listToExport)
{
PropertyInfo[] allProperties = dataItem.GetType().GetProperties();
sep = "";
foreach (PropertyInfo thisProperty in allProperties)
{
object value = thisProperty.GetValue(dataItem, null);
String propetyValue = (value == null ? String.Empty : value.ToString());
HttpContext.Current.Response.Write(sep + propetyValue.ToString());
sep = "\t";
}
++success;
HttpContext.Current.Response.Write("\n");
}
HttpContext.Current.Response.End();
}
catch (Exception ex)
{
throw ex;
}
}
}
No comments:
New comments are not allowed.