ASP.NET利用MD.DLL轉EXCEL具體實現

來源:互聯網
上載者:User

前提
引入MD.DLL 檔案;
:http://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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.