ASP.NET利用MD.DLL轉(zhuǎn)EXCEL具體實(shí)現(xiàn)
引入MD.dll 文件;
下載地址:http://www.dbjr.com.cn/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、調(diào)用方法:
http://localhost:13042/report/downexcel.aspx?procedure=P_PP_SPC_FindCount&strWorkCenterNum=0&strStatus=全部&strPartno=
注解:
P_PP_SPC_FindCoun:存儲(chǔ)過程
WorkcenterNum:參數(shù)
在每個(gè)參數(shù)前都要加上‘Str'表示該參數(shù)是字符串型
所以參數(shù)要寫成StrWorkcenterNum
- ASP.NET實(shí)現(xiàn)讀取Excel內(nèi)容并在Web上顯示
- ASP.NET中上傳并讀取Excel文件數(shù)據(jù)示例
- asp.net導(dǎo)出excel的簡(jiǎn)單方法實(shí)例
- asp.net讀取excel文件的三種方法示例
- asp.net導(dǎo)出Excel亂碼的原因及解決方法
- asp.net讀取excel中的數(shù)據(jù)并綁定在gridview
- asp.net使用npoi讀取excel模板并導(dǎo)出下載詳解
- asp.net導(dǎo)出Excel類庫(kù)代碼分享
- Asp.Net用OWC操作Excel的實(shí)例代碼
- Asp.net中DataTable導(dǎo)出到Excel的方法介紹
- ASP.NET(C#)讀取Excel的文件內(nèi)容
- Asp.Net使用Npoi導(dǎo)入導(dǎo)出Excel的方法
相關(guān)文章
vs.Net2003無法打開或創(chuàng)建Web應(yīng)用程序若干解決辦法.
這篇文章主要介紹了vs.Net2003無法打開或創(chuàng)建Web應(yīng)用程序若干解決辦法.2006-10-10AspNetCore&MassTransit?Courier實(shí)現(xiàn)分布式事務(wù)的詳細(xì)過程
MassTransit?Courier是一種用于創(chuàng)建和執(zhí)行帶有故障補(bǔ)償?shù)姆植际绞聞?wù)的機(jī)制,它可以用于滿足本地事務(wù)的需求,也可以在分布式系統(tǒng)中實(shí)現(xiàn)分布式事務(wù),這篇文章主要介紹了AspNetCore&MassTransit?Courier實(shí)現(xiàn)分布式事務(wù),需要的朋友可以參考下2022-10-10Asp.net簡(jiǎn)單代碼設(shè)置GridView自適應(yīng)列寬不變形實(shí)現(xiàn)思路與代碼
動(dòng)態(tài)綁定的GridView由于列數(shù)不固定,而列又太多(博主做的這個(gè)項(xiàng)目有150個(gè)左右的字段),這樣設(shè)置GridView固定寬度就不能滿足需求了2013-01-01ASP.NET文件上傳Upload的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了ASP.NET文件上傳的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11ASP.NET?MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁
這篇文章介紹了ASP.NET?MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09.net 讀取非標(biāo)準(zhǔn)配置文件的小例子
這篇文章介紹了.net 讀取非標(biāo)準(zhǔn)配置文件的小例子,有需要的朋友可以參考一下2013-07-07asp.net 獨(dú)立Discuz頭像編輯模塊分離打包
在Discuz產(chǎn)品系列(包括UCenter、UCHome)中有一個(gè)flash頭像上傳編輯的功能比較好用,和之前自己用js實(shí)現(xiàn)的照片在線編輯插件比較像,于是想將它獨(dú)立出來,一方面可以學(xué)習(xí)研究,另一方面有機(jī)會(huì)可以在項(xiàng)目中使用(這里主要是指Asp.Net程序,php的與之類似)。2011-06-06ASP.NET MVC5+EF6+EasyUI后臺(tái)管理系統(tǒng) 微信公眾平臺(tái)開發(fā)之消息管理
這篇文章主要介紹了ASP.NET MVC5+EF6+EasyUI后臺(tái)管理系統(tǒng),微信公眾平臺(tái)開發(fā)之消息管理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Forms身份認(rèn)證在IE11下無法保存Cookie的問題
這篇文章主要介紹了Forms身份認(rèn)證在IE11下無法保存Cookie問題的解決方法,需要的朋友可以參考下2014-05-05