导出EXCLE
Response.Clear();
Response.ClearHeaders();
Response.BufferOutput = false;
Response.ContentType = "application/ms-excel";
Response.AppendHeader("Content-disposition", "attachment;filename=" + HttpUtility.UrlEncode("AppointmentOrders_" + ddl_MCList.SelectedItem.Text + "_" + DateTime.Parse(startTime.Value).ToString("yyyyMMdd") + ".xls", System.Text.Encoding.UTF8));
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
属性设置
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
e.Item.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:¥#,###.00");
}
}
关健是这两句啦
e.Item.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");//格式字符串比如080001没有这句话就会变成80001
e.Item.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:¥#,###.00");
还有其它