1 代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Data.OracleClient;
using System.IO;
using System.Text;
using Hhxx.App_Code.DatabaseOperate;
using Hhxx.App_Code.Database;
using Hhxx.App_Code.Common;
public partial class InterFace_Reports_QiYeJiBenQingKuang : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// 初始化默认Gridview的SQL语句
String querySql = "SELECT 建立日期,信息标题,发布部门,经办人,信息类型,关键字,阅读次数,审批结果,SN FROM xinxi WHERE ROWNUM<11";
fillGridView(querySql);
}
// gridview 换行控制
this.GridViewXinXiLan.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
}
/// <summary>
/// 填充GridView
/// </summary>
private void fillGridView(String querySql)
{
DataSet ds = new DataSet();
ds = GetData(querySql);
// 清空以前的数据源
GridViewXinXiLan.DataSource = null;
// 将ds作为信息栏GridView的数据源
GridViewXinXiLan.DataSource = ds;
// 绑定数据
GridViewXinXiLan.DataBind();
// 保存数据源 防止丢失 VS的破Bug
ViewState["GridViewXinXiLan_DataSource"] = GridViewXinXiLan.DataSource;
// 保存SQL语句
ViewState["sql"] = querySql;
}
/// <summary>
/// 转换SQL查询结果为DataSet
/// </summary>
/// <returns>ds</returns>
private DataSet GetData(String querySql)
{
String connStr = OracleHelper.DatabaseConnStr;
// 创建数据集
DataSet ds = OracleHelper.ExeSqlForDataSet(connStr, CommandType.Text, querySql, null);
return ds;
}
/// <summary>
/// 导出到Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ButtonExcel_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridViewXinXiLan.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Data.OracleClient;
using System.IO;
using System.Text;
using Hhxx.App_Code.DatabaseOperate;
using Hhxx.App_Code.Database;
using Hhxx.App_Code.Common;
public partial class InterFace_Reports_QiYeJiBenQingKuang : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// 初始化默认Gridview的SQL语句
String querySql = "SELECT 建立日期,信息标题,发布部门,经办人,信息类型,关键字,阅读次数,审批结果,SN FROM xinxi WHERE ROWNUM<11";
fillGridView(querySql);
}
// gridview 换行控制
this.GridViewXinXiLan.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
}
/// <summary>
/// 填充GridView
/// </summary>
private void fillGridView(String querySql)
{
DataSet ds = new DataSet();
ds = GetData(querySql);
// 清空以前的数据源
GridViewXinXiLan.DataSource = null;
// 将ds作为信息栏GridView的数据源
GridViewXinXiLan.DataSource = ds;
// 绑定数据
GridViewXinXiLan.DataBind();
// 保存数据源 防止丢失 VS的破Bug
ViewState["GridViewXinXiLan_DataSource"] = GridViewXinXiLan.DataSource;
// 保存SQL语句
ViewState["sql"] = querySql;
}
/// <summary>
/// 转换SQL查询结果为DataSet
/// </summary>
/// <returns>ds</returns>
private DataSet GetData(String querySql)
{
String connStr = OracleHelper.DatabaseConnStr;
// 创建数据集
DataSet ds = OracleHelper.ExeSqlForDataSet(connStr, CommandType.Text, querySql, null);
return ds;
}
/// <summary>
/// 导出到Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ButtonExcel_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridViewXinXiLan.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
}