string FileName = "FgOrder";
HttpResponse resp;
resp = HttpContext.Current.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-disposition", "attachment;filename=" + FileName + ".xls");
resp.ContentType = "application/ms-excel";
//变量定义
string colHeaders = null;
string Is_item = null;
StringWriter sfw = new StringWriter();
System.Data.DataTable dt = (DataTable)ViewState["table"];
DataRow[] myRow = dt.Select();
int i = 0;
int cl = dt.Columns.Count;
//取得数据表各列标题,各标题之间以/t分割,最后一个列标题后加回车符
for (i = 0; i < cl; i++)
{
//if(i==(cl-1)) //最后一列,加/n
// colHeaders+=dt.Columns[i].Caption.ToString();
//else
colHeaders += dt.Columns[i].Caption.ToString() + "/t";
}
sfw.WriteLine(colHeaders);
//sw.WriteLine(colHeaders);
//逐行处理数据
foreach (DataRow row in myRow)
{
//当前数据写入
for (i = 0; i < cl; i++)
{
Is_item += row[i].ToString() + "/t";
}
sfw.WriteLine(Is_item);
Is_item = null;
}
resp.Write(sfw);
resp.End();