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

 更新时间:2021年9月22日 10:15  点击:1435
前提
引入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
[!--infotagslink--]

相关文章

  • SpringBoot实现excel文件生成和下载

    这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
  • c#读取excel方法实例分析

    这篇文章主要介绍了c#读取excel方法,实例分析了C#读取excel文件的原理与相关技巧,需要的朋友可以参考下...2020-06-25
  • Python导入数值型Excel数据并生成矩阵操作

    这篇文章主要介绍了Python导入数值型Excel数据并生成矩阵操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-06-09
  • C# winform打开Excel文档的方法总结(必看篇)

    下面小编就为大家带来一篇C# winform打开Excel文档的方法总结(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C# 导出Excel的6种简单方法实现

    C# 导出 Excel 的6种简单方法:数据表导出到 Excel,对象集合导出到 Excel,数据库导出到 Excel,微软网格控件导出到 Excel,数组导出到 Excel,CSV 导出到 Excel,你都会了吗?需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • C#实现Excel表数据导入Sql Server数据库中的方法

    这篇文章主要介绍了C#实现Excel表数据导入Sql Server数据库中的方法,结合实例形式详细分析了C#读取Excel表数据及导入Sql Server数据库的具体操作步骤与相关操作技巧,需要的朋友可以参考下...2020-06-25
  • C#实现将数据导出到word或者Excel中的方法

    这篇文章主要介绍了C#实现将数据导出到word或者Excel中的方法,涉及C#操作word及Excel格式文件的方法,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • C#使用Ado.Net更新和添加数据到Excel表格的方法

    这篇文章主要介绍了C#使用Ado.Net更新和添加数据到Excel表格的方法,较为详细的分析了OLEDB的原理与使用技巧,可实现较为方便的操作Excel数据,需要的朋友可以参考下...2020-06-25
  • C#使用Aspose.Cells创建和读取Excel文件

    这篇文章主要为大家详细介绍了C#使用Aspose.Cells创建和读取Excel文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • C#使用oledb导出数据到excel的方法

    这篇文章主要介绍了C#使用oledb导出数据到excel的方法,结合实例形式分析了C#操作oledb导出数据的相关技巧与注意事项,需要的朋友可以参考下...2020-06-25
  • C#操作excel打印的示例

    这篇文章主要介绍了C#操作excel打印的示例,帮助大家利用c#打印表格,提高办公效率,感兴趣的朋友可以了解下...2020-12-08
  • C#删除Excel中的图片实例代码

    在本篇文章里小编给大家分享了关于C#删除Excel中的图片的实例代码内容,有兴趣的朋友们参考学习下。...2020-06-25
  • C#使用Aspose.Cells控件读取Excel

    本文介绍Aspose.Cells基础的用法,供大家参考。...2020-06-25
  • C#中将ListView中数据导出到Excel的实例方法

    首先 你需要添加引用Microsoft Excel 11.0 Object Library...2020-06-25
  • python操作mysql、excel、pdf的示例

    这篇文章主要介绍了python操作mysql、excel、pdf的示例,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下...2021-03-29
  • python3 循环读取excel文件并写入json操作

    这篇文章主要介绍了python3 循环读取excel文件并写入json操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-14
  • C#将Excel转成PDF的方法

    今天小编就为大家分享一篇关于C#将Excel转成PDF的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧...2020-06-25
  • C# WinForm导出Excel方法介绍

    在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式...2020-06-25
  • 解决python pandas读取excel中多个不同sheet表格存在的问题

    这篇文章主要介绍了解决python pandas读取excel中多个不同sheet表格存在的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-14
  • phpexcel导入xlsx文件报错xlsx is not recognised as an OLE file 怎么办

    phpexcel是一款php读写excel的插件了,小编有一个这样的功能要来实现,但是在导入xlsx时发现xlsx is not recognised as an OLE file 了,但是导入xls是没有问题了,碰到这种...2016-11-25