#region 导出Excel
/// <summary>
/// 方法编号:07
/// 方法名称:ExportToExcel
/// 内容摘要:将DataGrid导出为Excel文件,结构与DataGrid相同;引用此方法,须传入page对象
/// 算法思路:将DataGrid render并以Excel的格式输出给客户端;
/// 须取消分页并重新执行绑定后调用此方法
/// </summary>
public void ExportToExcel(DataGrid m_grid,System.Web.UI.Page _page)
{
_page.Response.Clear(); //清除缓冲区流中的所有内容输出
_page.Response.Buffer = true;
_page.Response.Charset= "GB2312"; //设置输出流的http字符集
//生成文件名
StringBuilder m_strName = new StringBuilder();
m_strName.Append(DateTime.Now.Year.ToString()).Append(DateTime.Now.Month.ToString())
.Append(DateTime.Now.Day.ToString()).Append(DateTime.Now.Hour.ToString())
.Append(DateTime.Now.Minute.ToString()).Append(DateTime.Now.Second.ToString());
_page.Response.AppendHeader("Content-Disposition","attachment;filename=ExportData" + m_strName.ToString() + ".xls");
_page.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
//设置输出文件类型为excel文件
_page.Response.ContentType = "application/ms-excel";
//关闭保存视图状态
_page.EnableViewState = false;
System.Globalization.CultureInfo m_CI = new System.Globalization.CultureInfo("ZH-CN",true);//区域设置
System.IO.StringWriter m_stringWriter = new System.IO.StringWriter(m_CI);
System.Web.UI.HtmlTextWriter m_htmlTextWriter = new System.Web.UI.HtmlTextWriter(m_stringWriter);
//将m_grid中的内容输出到m_htmlTextWriter
m_grid.RenderControl(m_htmlTextWriter);
_page.Response.Write(m_stringWriter.ToString());
//将当前所有缓冲的输出发送到客户端,并停止该页执行
_page.Response.End();
}
/// <summary>
/// 方法编号:08
/// 方法名称:ExportToExcel
/// 内容摘要:将DataGrid导出为Excel文件,结构与DataGrid相同;引用此方法,须传入page对象
/// 算法思路:将DataGrid render并以Excel的格式输出给客户端;
/// 须取消分页并重新执行绑定后调用此方法
/// </summary>
public void ExportToExcel(DataGrid m_grid)
{
this.ExportToExcel(m_grid,this.m_page);
}
/// <summary>
/// 方法编号:09
/// 方法名称:SetExcelStyle
/// 内容摘要:设置DataGrid导出为EXCEL时的样式
/// </summary>
public DataGrid SetExcelStyle(DataGrid m_grid)
{
//给DataGrid增加样式
Table m_tb = (Table)m_grid.Controls[0];
m_grid.Style.Add("font-size","9pt");
m_grid.Style.Add("font-family","Courier New");
m_grid.Style.Add("heigth","25px");
m_grid.HeaderStyle.Height = Unit.Pixel(25);
m_grid.GridLines = GridLines.Both;
m_tb.BorderStyle = BorderStyle.Solid;
m_tb.BorderColor = Color.FromKnownColor(KnownColor.Black);
m_grid.ItemStyle.BackColor = Color.FromKnownColor(KnownColor.White);
m_grid.FooterStyle.BackColor= Color.FromKnownColor(KnownColor.Beige);
m_grid.HeaderStyle.BackColor= Color.FromKnownColor(KnownColor.Beige);
m_grid.AlternatingItemStyle.BackColor = Color.FromKnownColor(KnownColor.White);
return m_grid;
}
#endregion 导出Excel
/// <summary>
/// 方法编号:07
/// 方法名称:ExportToExcel
/// 内容摘要:将DataGrid导出为Excel文件,结构与DataGrid相同;引用此方法,须传入page对象
/// 算法思路:将DataGrid render并以Excel的格式输出给客户端;
/// 须取消分页并重新执行绑定后调用此方法
/// </summary>
public void ExportToExcel(DataGrid m_grid,System.Web.UI.Page _page)
{
_page.Response.Clear(); //清除缓冲区流中的所有内容输出
_page.Response.Buffer = true;
_page.Response.Charset= "GB2312"; //设置输出流的http字符集
//生成文件名
StringBuilder m_strName = new StringBuilder();
m_strName.Append(DateTime.Now.Year.ToString()).Append(DateTime.Now.Month.ToString())
.Append(DateTime.Now.Day.ToString()).Append(DateTime.Now.Hour.ToString())
.Append(DateTime.Now.Minute.ToString()).Append(DateTime.Now.Second.ToString());
_page.Response.AppendHeader("Content-Disposition","attachment;filename=ExportData" + m_strName.ToString() + ".xls");
_page.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
//设置输出文件类型为excel文件
_page.Response.ContentType = "application/ms-excel";
//关闭保存视图状态
_page.EnableViewState = false;
System.Globalization.CultureInfo m_CI = new System.Globalization.CultureInfo("ZH-CN",true);//区域设置
System.IO.StringWriter m_stringWriter = new System.IO.StringWriter(m_CI);
System.Web.UI.HtmlTextWriter m_htmlTextWriter = new System.Web.UI.HtmlTextWriter(m_stringWriter);
//将m_grid中的内容输出到m_htmlTextWriter
m_grid.RenderControl(m_htmlTextWriter);
_page.Response.Write(m_stringWriter.ToString());
//将当前所有缓冲的输出发送到客户端,并停止该页执行
_page.Response.End();
}
/// <summary>
/// 方法编号:08
/// 方法名称:ExportToExcel
/// 内容摘要:将DataGrid导出为Excel文件,结构与DataGrid相同;引用此方法,须传入page对象
/// 算法思路:将DataGrid render并以Excel的格式输出给客户端;
/// 须取消分页并重新执行绑定后调用此方法
/// </summary>
public void ExportToExcel(DataGrid m_grid)
{
this.ExportToExcel(m_grid,this.m_page);
}
/// <summary>
/// 方法编号:09
/// 方法名称:SetExcelStyle
/// 内容摘要:设置DataGrid导出为EXCEL时的样式
/// </summary>
public DataGrid SetExcelStyle(DataGrid m_grid)
{
//给DataGrid增加样式
Table m_tb = (Table)m_grid.Controls[0];
m_grid.Style.Add("font-size","9pt");
m_grid.Style.Add("font-family","Courier New");
m_grid.Style.Add("heigth","25px");
m_grid.HeaderStyle.Height = Unit.Pixel(25);
m_grid.GridLines = GridLines.Both;
m_tb.BorderStyle = BorderStyle.Solid;
m_tb.BorderColor = Color.FromKnownColor(KnownColor.Black);
m_grid.ItemStyle.BackColor = Color.FromKnownColor(KnownColor.White);
m_grid.FooterStyle.BackColor= Color.FromKnownColor(KnownColor.Beige);
m_grid.HeaderStyle.BackColor= Color.FromKnownColor(KnownColor.Beige);
m_grid.AlternatingItemStyle.BackColor = Color.FromKnownColor(KnownColor.White);
return m_grid;
}
#endregion 导出Excel
如果datagrid里有linkbutton等服务端控件,则需要手动进行转换为文字,如:
//将DataGrid里的Linkbutton替换为label对象
foreach (DataGridItem _item in m_outGrid.Items)
{
LinkButton _lb = (LinkButton)_item.Cells[0].Controls[0];
_item.Cells[0].Text = _lb.Text;
}
foreach (DataGridItem _item in m_outGrid.Items)
{
LinkButton _lb = (LinkButton)_item.Cells[0].Controls[0];
_item.Cells[0].Text = _lb.Text;
}