BatchExport2.aspx.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.OleDb;
  9. using System.Data.Sql;
  10. using System.Data.SqlClient;
  11. using System.Collections;
  12. using System.Text;
  13. using System.IO;
  14. public partial class Admin_BatchExport2 : System.Web.UI.Page
  15. {
  16. // string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("~\\App_Data\\ZY.mdb");
  17. LYS.UserData1 myud = new LYS.UserData1();
  18. string username = ""; string usertype = ""; string userdemp = "";
  19. protected void Page_Load(object sender, EventArgs e)
  20. {
  21. username = myud.ReadCookies("UserName").Trim();
  22. usertype = myud.ReadCookies("UserType").Trim();
  23. userdemp = myud.ReadCookies("UserDemp").Trim();
  24. }
  25. protected void Button2_Click(object sender, EventArgs e)
  26. {
  27. showdata();
  28. }
  29. protected void showdata()
  30. {
  31. SqlConnection objConn = new SqlConnection(myud.ConnString());
  32. objConn.Open();
  33. DataSet objDataset = new DataSet();
  34. SqlDataAdapter objAdapter = new SqlDataAdapter();
  35. string strSQL = "Select " + Session["FieldList"] + " From " + Session["TableName"].ToString()+" where 系部='" + userdemp + "' order by 听课日期 Desc";
  36. objAdapter.SelectCommand = new SqlCommand(strSQL, objConn);
  37. objAdapter.Fill(objDataset);
  38. DataView oView = new DataView(objDataset.Tables[0]);
  39. DataGrid1.DataSource = oView;
  40. DataGrid1.DataBind();
  41. objConn.Close();
  42. objConn.Dispose();
  43. objConn = null;
  44. }
  45. protected void Button1_Click(object sender, EventArgs e)
  46. {
  47. showdata();
  48. Response.ContentType = "application/vnd.ms-Word";
  49. Response.AddHeader("content-disposition", "inline;filename=ImportToWord.doc");
  50. Response.Charset = "";
  51. EnableViewState = false;
  52. StringWriter tw = new StringWriter();
  53. HtmlTextWriter hw = new HtmlTextWriter(tw);
  54. DataGrid1.RenderControl(hw);
  55. Response.Write(tw.ToString());
  56. Response.End();
  57. //BatchExportData();
  58. }
  59. protected void Button3_Click(object sender, EventArgs e)
  60. { //BatchExportData();
  61. showdata();
  62. Response.ContentType = "application/vnd.ms-Excel";
  63. Response.AddHeader("content-disposition", "inline;filename=ImportToWord.XLS");
  64. Response.Charset = "";
  65. EnableViewState = false;
  66. StringWriter tw = new StringWriter();
  67. HtmlTextWriter hw = new HtmlTextWriter(tw);
  68. DataGrid1.RenderControl(hw);
  69. Response.Write(tw.ToString());
  70. Response.End();
  71. }
  72. protected void BatchExportData()
  73. {
  74. //设置Http的头信息,编码格式
  75. HttpContext.Current.Response.Buffer = true;
  76. HttpContext.Current.Response.Clear();
  77. HttpContext.Current.Response.ClearContent();
  78. HttpContext.Current.Response.ClearHeaders();
  79. HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("行政管理制度.doc", System.Text.Encoding.UTF8));
  80. HttpContext.Current.Response.ContentType = "application/ms-word";
  81. HttpContext.Current.Response.Charset = "UTF-8";
  82. HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.UTF8;
  83. HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
  84. //关闭控件的视图状态
  85. Page.EnableViewState = false;
  86. //初始化HtmlWriter
  87. System.IO.StringWriter writer = new System.IO.StringWriter();
  88. System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
  89. Page.RenderControl(htmlWriter);
  90. //输出
  91. string pageHtml = writer.ToString();
  92. int startIndex = pageHtml.IndexOf("<div style=\"margin: 0 auto;\" id=\"mainContent\">");
  93. int endIndex = pageHtml.LastIndexOf("</div>");
  94. int lenth = endIndex - startIndex;
  95. pageHtml = pageHtml.Substring(startIndex, lenth);
  96. pageHtml = pageHtml.Remove(pageHtml.LastIndexOf("<div class=\"flowbar\">"));
  97. //设置图片路径
  98. Uri MyUrl = Request.UrlReferrer;
  99. pageHtml = pageHtml.Replace("../Images/", "http://" + MyUrl.Authority + "/Images/").Replace("textarea", "span");
  100. HttpContext.Current.Response.Write(pageHtml.ToString());
  101. HttpContext.Current.Response.End();
  102. }
  103. }