//Grid数据导出到Excel
private void ExportToExcel()
{
string strFileName;
strFileName = HttpUtility.UrlEncode("导出名称.xls");
Response.Clear(); //清空
Response.Buffer = true; //缓冲输出
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=" + strFileName);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
//定义一个输入流
System.IO.StringWriter swStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htwHtmlTextWriter = new System.Web.UI.HtmlTextWriter(swStringWriter);
ResetDataGrid(DataGrid1);//去除其它格式
Response.Write ("<p align=/"center/"><b><font face=/"Verdana/" size=/"18px/">导出名称</b></p>");
DataGrid1.Columns[13].Visible = false;
DataGrid1.RenderControl(htwHtmlTextWriter);
Response.Write (swStringWriter.ToString());
Response.End();
}
//去除其它格式
private void ResetDataGrid(C1WebGrid grid)
{
int i;
int intItemCount;
// string strPlanName;
HyperLink lnkPlanName;
intItemCount = grid.Items.Count;
for (i = 0; i < intItemCount; i++)
{
lnkPlanName = (HyperLink)grid.Items[i].Cells[6].FindControl("lnkPlanName");
if (lnkPlanName != null)
{
lnkPlanName.NavigateUrl = "";
// strPlanName = grid.Items[i].Cells[5].Text;
//
// strPlanName = lnkPlanName.Text;
//
// grid.Items[i].Cells[5].Controls.Clear();
//
// grid.Items[i].Cells[5].Text = strPlanName;
}
}
}