ASP.NET利用MD.DLL转EXCEL具体实现

所属分类: 网络编程 / ASP.NET 阅读数: 1293
收藏 0 赞 0 分享
前提
引入MD.dll 文件;
下载地址:https://www.jb51.net/dll/MD.dll.html
1、建立无CS文件的DownExcel.aspx 文件
复制代码 代码如下:

<%@ 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>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("条件不符,查询没有任何资料!");
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, well, we update our DB
}
</script>
<html>
<head>
<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" /> -->
</head>
<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>
</html>

2、调用方法
http://localhost:13042/report/downexcel.aspx?procedure=P_PP_SPC_FindCount&strWorkCenterNum=0&strStatus=全部&strPartno=
注解
P_PP_SPC_FindCoun:存储过程
WorkcenterNum:参数
在每个参数前都要加上‘Str'表示该参数是字符串型
所以参数要写成StrWorkcenterNum
更多精彩内容其他人还在看

asp.net中利用ashx实现图片防盗链代码

直接分析盗链原理:看下面用httpwatch截获的http发送的数据
收藏 0 赞 0 分享

ASP.NET程序中常用代码汇总

对于学习asp.net的朋友能用的到
收藏 0 赞 0 分享

asp.net 无重复随机数代码

asp.net产生无重复随机数的实现代码
收藏 0 赞 0 分享

asp.net(C#)中上传大文件的几中常见应用方法

最近博客需要做一个文件上下载功能,我从网上找了点资料,整理了下希望对大家有帮助!
收藏 0 赞 0 分享

asp.net AJAX实现无刷新获得数据

提供一个使用AJAX实现无刷新判断注册用户名是否被注册的代码:
收藏 0 赞 0 分享

Ajax.net Sys未定义错误解决办法

用Asp.net2.0开发的系统,使用了Ajax技术,在本地没有任何问题!但是发布到Web托管服务器上后,系统总是出现“Sys 未定义”的错误!
收藏 0 赞 0 分享

asp.net(c#)判断远程图片是否存在

不错的应用,大家可以拓展到,判断远程文件是否存在等功能
收藏 0 赞 0 分享

asp.net保存远程图片的代码

最近有点烦,没怎么看书,几天下来,就研究了一个保存远程图片的。
收藏 0 赞 0 分享

Asp.Net类库中发送电子邮件的代码

发送电子邮件是许多需要用户注册的网站的通用功能,通过正则表达式我们可以过滤掉不符合电子邮件格式的输入,但是仍没有办法确保用户填写的电子邮件地址一定是他本人真实有效的电子邮件地址
收藏 0 赞 0 分享

ASP.NET表单验证方法详解第1/2页

在表单提交的时候,经常需要对录入信息的长度、格式、内容等进行验证,以便获得合理的信息。在ASP.NET开发中主要的验证方法,我总结了一下,主要有一下几种,如有不足之处请朋友们予以指出。
收藏 0 赞 0 分享
查看更多