///
/// Ilist导出Excel数据
///
/// 需要传入的Ilist对象
///
public void CreateExcel (IList lt, string FileName)
{
if (lt == null || lt.Count <= 0)
{
JavaScript.Alert("没有数据需要导出!",Page);
return ;
}
System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
HttpResponse resp;
resp = Page.Response;
resp.Charset = "UTF-8";
resp.ContentEncoding = System.Text.Encoding.Default;
resp.ContentType = "application/ms-excel";
resp.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");
EnableViewState = false;
string colHeaders = "", Is_item = "";
int i = 0,j;
for (i = 0, j = myPropertyInfo.Length; i < j; i++)
{
System.Reflection.PropertyInfo pi = myPropertyInfo[i];
string name = pi.Name;
colHeaders += name + "/t";
}
colHeaders += "/n";
resp.Write(colHeaders);
foreach (T t in lt)
{
if (t == null)
{
continue;
}
for (i = 0, j = myPropertyInfo.Length; i < j; i++)
{
System.Reflection.PropertyInfo pi = myPropertyInfo[i];
string str = string.Format("{0}", pi.GetValue(t, null)).Replace("/n", "");
if(str=="")
{
Is_item += "/t";
}
else
{
Is_item += str + "/t";
}
}
Is_item += "/n";
resp.Write(Is_item);
Is_item = "";
}
//写缓冲区中的数据到HTTP头文件中
resp.End();
}
protected void Button1_Click(object sender, EventArgs e)
{
questions qs = new questions();
qs.qflag = 0;
int count = 0;
IList
lt = dac.GetListQA(qs, pageIndex, 0, ref count);
CreateExcel(lt, "ToAskQuestions");
}