Asp. NET using MD.DLL to Excel Concrete implementation _ practical skills

Source: Internet
Author: User
premises
introduction of MD.dll documents;
Download Address: http://www.jb51.net/dll/MD.dll.html
1, the establishment of downexcel.aspx files without CS files
Copy Code code as follows:

<%@ Page language= "C #"%>
<%@ import namespace= "System.Data"%>
<%@ import namespace= "System.Data.SqlClient"%>
<%@ import namespace= "MD"%>
<script runat= "Server" >
String tablename = "";
String procname = "";
private string Selectsql (String selstr)
{
String sp =selstr + "WHERE";
int iwhere;
Iwhere=sp. IndexOf ("WHERE");
iwhere=iwhere+7;
String sall = Server.urldecode (Request.QueryString.ToString ());
String[] Sparams;
Sparams=sall. Split (' & ');
int i=0;
if (sparams. length>1) {
while (I<sparams. Length) {
if (!) ( Sparams[i]. StartsWith ("table")) {
if ((sparams[i). StartsWith ("str"))) {
Sp=sp+ "and" + sparams[i]. Replace ("=", "= '"). Substring (3) + "'";
}
if ((sparams[i). StartsWith ("num"))
{
Sp=sp+ "and" + sparams[i]. Substring (3) + "";
}
}
i++;
}
}
if (sp. IndexOf ("and") >0) {
SP = (sp. Substring (0,SP. IndexOf ("and")) + sp. Substring (sp. IndexOf ("and") +3));
}
Sp=sp. Replace ("=", "= '");
if (sp. Length<iwhere) {
Sp=sp. Substring (0, (iwhere-8));
}
return SP;
}
private string Selectproc (String selstr)
{
String sp =selstr + "";
String sall = Server.urldecode (Request.QueryString.ToString ());
Server.urldecode (Request.QueryString.ToString ());
String[] Sparams;
Sparams=sall. Split (' & ');
int i=0;
if (sparams. LENGTH&GT;1)
{
while (I<sparams. Length)
{
if (!) ( Sparams[i]. StartsWith ("procedure"))
{
if ((sparams[i). StartsWith ("str"))
{
SP=SP + "'" + sparams[i]. Substring (Sparams[i]. IndexOf ("=") +1) + "',";
}
if ((sparams[i). StartsWith ("num"))
{
SP=SP + sparams[i]. Substring (Sparams[i]. IndexOf ("=") +1) + ",";
}
}
i++;
}
}
if (sp. EndsWith (",")) {
Sp=sp. Substring (0, Sp. LENGTH-1));
}
return SP;
}
private void Page_Load (object sender, System.EventArgs e)
{
Setup Connection
Response.Write (Selectsql ("Start test!"));
String conn = configurationmanager.connectionstrings["ConnStr"]. ConnectionString; system.configuration.configurationsettings.appsettings["ConnectionString"];
if (request.querystring["table"]== null && request.querystring["procedure"]==null)
{
This. Response.Write ("Not supply correct parameters!");
This. Response.End ();
Return
}
DataSet ds = new DataSet ();
Ds. Locale = new System.Globalization.CultureInfo ("ZH-CN");
OleDbDataAdapter adapter=new OleDbDataAdapter ();
if (!) ( request.querystring["table"]== null))
{
/*string Test1=selectsql (("SELECT * from" + request.querystring["table"));
This. Response.Write (test1);
This. Response.End ();
return;*/
tablename=request.querystring["Table"];
Md. Sqlhelper.filldataset (Conn,system.data.commandtype.text, Selectsql ("SELECT * from" + tablename), Ds,new string[] {" Down "});
}
if (!) ( Request.querystring["procedure"]== null))
{
/*string Test2=selectproc (("Exec" + request.querystring["procedure"));
This. Response.Write (Test2);
This. Response.End ();
return;*/
Procname=request.querystring["procedure"];
Md. Sqlhelper.filldataset (Conn,system.data.commandtype.text, Selectproc ("exec" + procname)), Ds,new string[] {"Down"});
}
if (ds. Tables[0]. rows.count==0) {
This. Response.Write ("The condition does not match, the inquiry does not have any information!");
Return
}
String Downres= "";
if (procname== "")
{
Downres=tablename;
}
Else
{
Downres=procname;
}
OleDbDataAdapter adapter = new OleDbDataAdapter ("SELECT * from" + tablename, conn);
Response.Write (Selectsql ("SELECT * from" + tablename));
Return
Open the Database and get the results
This. Datagriddown.datasource=ds;
This. Datagriddown.databind ();
This. Response.Clear ();
This. Response.Buffer =true;
This. Response.charset= "Utf-8";
This. Response.contenttype= "Application/ms-excel";
This. Response.appendheader ("Content-disposition", "Attachment;filename=" +downres+ ". xls");
This. Response.ContentEncoding =system.text.encoding.getencoding ("Utf-8");
response.contentencoding = System.text.encoding.utf-8;
This. EnableViewState =false;
System.IO.StringWriter ostringwriter = new System.IO.StringWriter ();
System.Web.UI.HtmlTextWriter ohtmltextwriter = new System.Web.UI.HtmlTextWriter (ostringwriter);
This. Datagriddown.rendercontrol (Ohtmltextwriter);
This. Response.Write (Ostringwriter.tostring ());
This. Response.End ();
If the action is update, so, we update our DB
}
</script>
<meta http-equiv= "Content-type" content= "application/x-excel; Charset=utf-8 "/>
<!--<meta http-equiv= "Content-type" content= "application/x-msexcel; Charset=iso-8859-1 "/>-->
<body>
<form runat= "Server" >
<asp:datagrid id= "Datagriddown" style= "z-index:100; Position:absolute "runat=" Server "height=" 373px "width=" 674px ">
</asp:DataGrid>
<!--Insert content here-->
</form>
</body>

2. Calling Methods
Http://localhost:13042/report/downexcel.aspx?procedure=P_PP_SPC_FindCount&strWorkCenterNum=0&strStatus = All &strpartno=
Annotations
P_pp_spc_findcoun: Stored Procedures
Workcenternum: Parameters
Precede each parameter with ' Str ' to indicate that the argument is a string type
So the parameters are written as Strworkcenternum.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.