ASP.NET는 MD.DLL을 이용하여 EXCEL로 구체적으로 실현

6646 단어
사전 요구 사항:
MD.dll 파일 가져오기;
다운로드 주소://www.jb51.net/dll/MD.dll.html
1. CS 파일이 없는 DownExcel을 만듭니다.aspx 파일
 
  




<br>string tableName = ""; <br>string procName =""; <br>private string selectSql( string selstr ) <br>{ <br>string sp =selstr + " WHERE"; <br>int iwhere; <br>iwhere=sp.IndexOf("WHERE"); <br>iwhere=iwhere+7; <br>string sall = Server.UrlDecode(Request.QueryString.ToString()); <br>string[] sparams; <br>sparams=sall.Split('&'); <br>int i=0; <br>if (sparams.Length>1){ <br>while (i<sparams.Length){ <br>if (!(sparams[i].StartsWith("table"))){ <br>if ((sparams[i].StartsWith("str") )){ <br>sp=sp+" and " + sparams[i].Replace("=","='").Substring(3) + "'"; <br>} <br>if ((sparams[i].StartsWith("num") )) <br>{ <br>sp=sp+" and " + sparams[i].Substring(3) + ""; <br>} <br>} <br>i++; <br>} <br>} <br>if (sp.IndexOf("and") >0 ){ <br>sp = (sp.Substring(0,sp.IndexOf("and")) + sp.Substring(sp.IndexOf("and")+3)); <br>} <br>//sp=sp.Replace("=","='"); <br>if (sp.Length<iwhere) { <br>sp=sp.Substring(0,(iwhere-8)); <br>} <br>return sp; <br>} <br>private string selectProc( string selstr ) <br>{ <br>string sp =selstr + " "; <br>string sall = Server.UrlDecode(Request.QueryString.ToString()); <br>//Server.UrlDecode(Request.QueryString.ToString()); <br>string[] sparams; <br>sparams=sall.Split('&'); <br>int i=0; <br>if (sparams.Length>1) <br>{ <br>while (i<sparams.Length) <br>{ <br>if (!(sparams[i].StartsWith("procedure"))) <br>{ <br>if ((sparams[i].StartsWith("str") )) <br>{ <br>sp=sp + "'" + sparams[i].Substring( sparams[i].IndexOf("=")+1) + "',"; <br>} <br>if ((sparams[i].StartsWith("num") )) <br>{ <br>sp=sp + sparams[i].Substring( sparams[i].IndexOf("=")+1) + ","; <br>} <br>} <br>i++; <br>} <br>} <br>if (sp.EndsWith(",")){ <br>sp=sp.Substring(0, (sp.Length -1)); <br>} <br>return sp; <br>} <br>private void Page_Load(object sender, System.EventArgs e) <br>{ <br>// setup connection <br>//Response.Write(selectSql("start test!")); <br>string conn = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString; /// System.Configuration.ConfigurationSettings.AppSettings["connectionString"]; <br>if (Request.QueryString["table"]== null && Request.QueryString["procedure"]==null) <br>{ <br>this.Response.Write("not supply correct parameters!"); <br>this.Response.End(); <br>return; <br>} <br>DataSet ds = new DataSet(); <br>ds.Locale = new System.Globalization.CultureInfo("zh-CN"); <br>//OleDbDataAdapter adapter=new OleDbDataAdapter(); <br>if (!(Request.QueryString["table"]== null ) ) <br>{ <br>/*string test1=selectSql(("SELECT * from " + Request.QueryString["table"])); <br>this.Response.Write(test1); <br>this.Response.End(); <br>return;*/ <br>tableName=Request.QueryString["table"]; <br>MD.SqlHelper.FillDataset(conn,System.Data.CommandType.Text ,selectSql(("SELECT * from " + tableName)),ds,new string[] {"down"}); <br>} <br>if (!(Request.QueryString["procedure"]== null ) ) <br>{ <br>/*string test2=selectProc(("exec " + Request.QueryString["procedure"])); <br>this.Response.Write(test2); <br>this.Response.End(); <br>return;*/ <br>procName=Request.QueryString["procedure"]; <br>MD.SqlHelper.FillDataset(conn,System.Data.CommandType.Text ,selectProc(("exec " + procName)),ds,new string[] {"down"}); <br>} <br>if (ds.Tables[0].Rows.Count==0){ <br>this.Response.Write(" , !"); <br>return; <br>} <br>string downRes=""; <br>if (procName=="") <br>{ <br>downRes=tableName; <br>} <br>else <br>{ <br>downRes=procName; <br>} <br>//OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * from " + tableName, conn); <br>//Response.Write(selectSql("SELECT * from " + tableName)); <br>//return; <br>// open the Database and get the results <br>this.DataGridDown.DataSource=ds; <br>this.DataGridDown.DataBind(); <br>this.Response.Clear(); <br>this.Response.Buffer =true; <br>this.Response.Charset="utf-8"; <br>this.Response.ContentType="application/ms-excel"; <br>this.Response.AppendHeader("content-Disposition","attachment;filename="+downRes+".xls"); <br>this.Response.ContentEncoding =System.Text.Encoding.GetEncoding("utf-8"); <br>//Response.ContentEncoding = System.Text.Encoding.utf-8; <br>this.EnableViewState =false; <br>System.IO.StringWriter OStringWriter = new System.IO.StringWriter(); <br>System.Web.UI.HtmlTextWriter OHtmlTextWriter = new System.Web.UI.HtmlTextWriter(OStringWriter); <br>this.DataGridDown.RenderControl(OHtmlTextWriter); <br>this.Response.Write(OStringWriter.ToString()); <br>this.Response.End(); <br>// if the action is update, well, we update our DB <br>} <br>














2. 호출 방법:
http://localhost:13042/report/downexcel.aspx?procedure=P_PP_SPC_FindCount&strWorkCenterNum=0&strStatus=전체 &strPartno=
주석:
P_PP_SPC_FindCoun:스토리지 프로세스
WorkcenterNum:매개 변수
각 매개변수 앞에 "Str"를 붙여 문자열임을 나타냅니다.
그래서 매개 변수는 StrWorkcenterNum으로 써야 돼요.

좋은 웹페이지 즐겨찾기