using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.OleDb; using System.Data.Sql; using System.Data.SqlClient; using System.Collections; using System.Text; using System.IO; public partial class Admin_BatchExport2 : System.Web.UI.Page { // string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("~\\App_Data\\ZY.mdb"); LYS.UserData1 myud = new LYS.UserData1(); string username = ""; string usertype = ""; string userdemp = ""; protected void Page_Load(object sender, EventArgs e) { username = myud.ReadCookies("UserName").Trim(); usertype = myud.ReadCookies("UserType").Trim(); userdemp = myud.ReadCookies("UserDemp").Trim(); } protected void Button2_Click(object sender, EventArgs e) { showdata(); } protected void showdata() { SqlConnection objConn = new SqlConnection(myud.ConnString()); objConn.Open(); DataSet objDataset = new DataSet(); SqlDataAdapter objAdapter = new SqlDataAdapter(); string strSQL = "Select " + Session["FieldList"] + " From " + Session["TableName"].ToString()+" where 系部='" + userdemp + "' order by 听课日期 Desc"; objAdapter.SelectCommand = new SqlCommand(strSQL, objConn); objAdapter.Fill(objDataset); DataView oView = new DataView(objDataset.Tables[0]); DataGrid1.DataSource = oView; DataGrid1.DataBind(); objConn.Close(); objConn.Dispose(); objConn = null; } protected void Button1_Click(object sender, EventArgs e) { showdata(); Response.ContentType = "application/vnd.ms-Word"; Response.AddHeader("content-disposition", "inline;filename=ImportToWord.doc"); Response.Charset = ""; EnableViewState = false; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); DataGrid1.RenderControl(hw); Response.Write(tw.ToString()); Response.End(); //BatchExportData(); } protected void Button3_Click(object sender, EventArgs e) { //BatchExportData(); showdata(); Response.ContentType = "application/vnd.ms-Excel"; Response.AddHeader("content-disposition", "inline;filename=ImportToWord.XLS"); Response.Charset = ""; EnableViewState = false; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); DataGrid1.RenderControl(hw); Response.Write(tw.ToString()); Response.End(); } protected void BatchExportData() { //设置Http的头信息,编码格式 HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("行政管理制度.doc", System.Text.Encoding.UTF8)); HttpContext.Current.Response.ContentType = "application/ms-word"; HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.UTF8; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; //关闭控件的视图状态 Page.EnableViewState = false; //初始化HtmlWriter System.IO.StringWriter writer = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer); Page.RenderControl(htmlWriter); //输出 string pageHtml = writer.ToString(); int startIndex = pageHtml.IndexOf("