动态资料导出导入平台(一)
又來(lái)啦,又要說(shuō)偷懶的主張了。在實(shí)際的項(xiàng)目中,資料的導(dǎo)入導(dǎo)出是經(jīng)常能遇的到的,到底有沒(méi)有辦法可以做成動(dòng)態(tài)的呢? 開(kāi)發(fā)人員開(kāi)發(fā)出來(lái)后,剩下的可以將由HELPDESK組去維護(hù),后續(xù)如果還要增加類似的功能,HELPDESK組人員就自己可以去處理,而我們開(kāi)發(fā)人員可以不用處理,節(jié)省一些不必要的時(shí)間,或者可以花更多的時(shí)間到某件事情上,而不必受過(guò)多的打擾呢。答案是有的,接下我們先講一下導(dǎo)出的實(shí)現(xiàn),導(dǎo)出在這里相對(duì)簡(jiǎn)單一點(diǎn)。導(dǎo)入將放到后面的隨筆。這里的導(dǎo)入導(dǎo)出與網(wǎng)上大多數(shù)人的ExcelHelper或者COM形式的導(dǎo)出還是有比較大的區(qū)別。
動(dòng)態(tài),所謂的動(dòng)態(tài),就是很容易的就實(shí)現(xiàn)數(shù)據(jù)源的切換,還有某數(shù)據(jù)源其中的一部分?jǐn)?shù)據(jù)。這里我們主要用到EAI接口,按指定格式導(dǎo)入書寫XML,然后讀取這個(gè)XML以獲得動(dòng)態(tài)的數(shù)所輸出。實(shí)現(xiàn)起來(lái)也不會(huì)太難。
1.前臺(tái)頁(yè)面設(shè)計(jì)。
從別的畫面到跳出這個(gè)導(dǎo)出面面,只需要指定相應(yīng)的XML
function btn_ExUR() {window.open('/ImportAndExport/SelectFields.aspx?loadXml=UserRightMapping', 'ExportSelFields', 'top=' + (screen.height - 525) / 2 + ', left=' + (screen.width - 820) / 2 + ', width=820, height=525, toolbar=no, menubar=no, scrollbar=no, resizable=no, location=no, status=no')}運(yùn)行后按XML動(dòng)態(tài)產(chǎn)生字段。
?然后用戶可以動(dòng)態(tài)選擇所需要的導(dǎo)出字段。
導(dǎo)出結(jié)果如下
下面大家看一下XML到底是如何構(gòu)成的。
<?xml version="1.0" encoding="utf-8" ?> <EAI xmlns="urn:mapping-schema" from="SqlServerDataBase" to="ExcelFile"><Source><table name="CENTRAL_USERIGHT" dataSourceName="SqlServer" handType="EAI.DataBaseHandler.SqlServerExport"><fields><field name="SUSER" mapping="員工帳號(hào)" dbType="Char"/><field name="MODNAME" mapping="模組" dbType="Char"/> <field name="CANREAD" mapping="只讀" dbType="Bit"/><field name="CANINS" mapping="新增" dbType="Bit"/><field name="CANUPD" mapping="修改" dbType="Bit"/><field name="CANDEL" mapping="刪除" dbType="Bit"/></fields></table></Source><Target><file range="A2" dataSourceName="Test.xls" name="Sheet1" titleLine="1" autoGenColumnTitle="false" handType="EAI.FileHandler.ExcelImport"><fields><field name="SUSER" mapping="員工帳號(hào)" primaryKey="true" canNull="false" dbType="Char" length="20"/><field name="MODNAME" mapping="模組" canNull="false" dbType="NChar" length="255"/><field name="CANREAD" mapping="只讀" dbType="Bit" defaultValue="0" replace="是,1|否,0|是,TRUE|否,FALSE"/><field name="CANINS" mapping="新增" dbType="Bit" defaultValue="0" replace="是,1|否,0|是,TRUE|否,FALSE"/><field name="CANUPD" mapping="修改" dbType="Bit" defaultValue="0" replace="是,1|否,0|是,TRUE|否,FALSE"/><field name="CANDEL" mapping="刪除" dbType="Bit" defaultValue="0" replace="是,1|否,0|是,TRUE|否,FALSE"/> </fields></file></Target> </EAI>大家一看,相信也不難理解。后續(xù)如果用戶還需要導(dǎo)出其它數(shù)據(jù),HELPDESK人員可以按XML做一個(gè)類似的文檔,同時(shí)在前臺(tái)寫個(gè)按鈕直接把XML參數(shù)傳給這個(gè)SelectFields頁(yè)面就可以了。開(kāi)發(fā)人員不需再管任何事情。
下面把SelectFields頁(yè)面的后臺(tái)代碼也帖出。如果有人想要EAI的動(dòng)態(tài)庫(kù)的話,可以留下郵箱。如果方便,順便給個(gè)贊唄。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.SessionState; using System.Web.Profile; using System.Text; using System.IO;namespace WebApp.ImportAndExport {public partial class SelectFields : WebBase, IRequiresSessionState{// Fieldsprivate const string btnHideSearch = "<div id='hideSearch' style=\"height:25px; position:absolute; z-index:-20px;\"><input type=\"button\" name=\"btnSearch\" id=\"btnSearch\" value=\"搜索\" class=\"SUB-BUTTON\" style=\"visibility:hidden;\" οnclick=\"btnSearchClick()\"/></div>";private const string btnShowSave = "<div id='showSave' style=\"height:25px;\"><input type=\"button\" name=\"btnSave\" id=\"btnSave\" value=\"保存\" class=\"SUB-BUTTON\" style=\"visibility:visible; background-color:#54bd24; color:#FFFFFF; font-weight:bold\" οnclick=\"btnSaveClick()\"/></div>";private const string btnShowSearch = "<div style=\"height:25px\"><input type=\"button\" name=\"btnSearch\" id=\"btnSearch\" value=\"搜索\" class=\"SUB-BUTTON\" style=\"visibility:visible;\" οnclick=\"btnSearchClick()\"/></div>";private string compareKey;private string compareValue;private Dictionary<string, string> dicChkSel = new Dictionary<string, string>();private Dictionary<string, string> dicMap;private const string disabledCheckBox = "<li style=\"height:22px\"><input type=\"checkbox\" name=\"{0}\" id=\"{0}\" checked=\"checked\" disabled=\"true\"/><label for=\"{0}\" class=\"font-12\">{1}</label></li>";private const string enabledCheckBox = "<li style=\"height:22px\"><input type=\"checkbox\" name=\"{0}\" id=\"{0}\" checked=\"checked\"/><label for=\"{0}\" class=\"font-12\">{1}</label></li>";private string equalKey;private string equalValue;private string jField;private string jTable;private string loadXml;private string tField;private const string unselDisabledCheckBox = "<li style=\"height:22px\"><input type=\"checkbox\" name=\"{0}\" id=\"{0}\" disabled=\"true\"/><label for=\"{0}\" class=\"font-12\">{1}</label></li>";private string whereCase;private XmlFieldsToTable xmlFieldsToTable;// Methodsprotected void BuildPage(BtnType btnType){string str = " <div id=\"loadPic\" class=\"font-12\" style=\"display:none; color:#336699; float:left; width: 500px;\"><img src=\"../images/connecting.gif\" style=\"width: 140px; height: 13px\" />。下載中。</div>";StringBuilder builder = new StringBuilder();if (btnType == BtnType.Search){builder.Append("<div style=\"height:25px\"><input type=\"button\" name=\"btnSearch\" id=\"btnSearch\" value=\"搜索\" class=\"SUB-BUTTON\" style=\"visibility:visible;\" οnclick=\"btnSearchClick()\"/></div>");builder.Append(str);}else{builder.Append("<div id='hideSearch' style=\"height:25px; position:absolute; z-index:-20px;\"><input type=\"button\" name=\"btnSearch\" id=\"btnSearch\" value=\"搜索\" class=\"SUB-BUTTON\" style=\"visibility:hidden;\" οnclick=\"btnSearchClick()\"/></div>");builder.Append("<div id='showSave' style=\"height:25px;\"><input type=\"button\" name=\"btnSave\" id=\"btnSave\" value=\"保存\" class=\"SUB-BUTTON\" style=\"visibility:visible; background-color:#54bd24; color:#FFFFFF; font-weight:bold\" οnclick=\"btnSaveClick()\"/></div>");builder.Append(str);}builder.Append("<div style=\"margin:0px 0px 0px 0px; background-color:#EFEFEF\"><ul>");this.xmlFieldsToTable = new XmlFieldsToTable(base.Server.MapPath("~/XmlMapping/" + this.loadXml + ".xml"), SelXmlNodeMode.TargetNode, SelXmlNodeInfo.MappingOnly);this.dicMap = this.xmlFieldsToTable.DicMap;if (btnType == BtnType.Search){foreach (string str2 in this.dicMap.Keys){builder.Append(string.Format("<li style=\"height:22px\"><input type=\"checkbox\" name=\"{0}\" id=\"{0}\" checked=\"checked\"/><label for=\"{0}\" class=\"font-12\">{1}</label></li>", str2.ToString(), this.dicMap[str2.ToString()]));}}else{if (this.Session["selectCheckList"] != null){this.dicChkSel = this.Session["selectCheckList"] as Dictionary<string, string>;}foreach (string str3 in this.dicMap.Keys) //保存按鈕時(shí),所有的選項(xiàng)均為不可以選 {if (this.dicChkSel.ContainsKey(str3.ToString())){builder.Append(string.Format("<li style=\"height:22px\"><input type=\"checkbox\" name=\"{0}\" id=\"{0}\" checked=\"checked\" disabled=\"true\"/><label for=\"{0}\" class=\"font-12\">{1}</label></li>", str3.ToString(), this.dicMap[str3.ToString()]));}else{builder.Append(string.Format("<li style=\"height:22px\"><input type=\"checkbox\" name=\"{0}\" id=\"{0}\" disabled=\"true\"/><label for=\"{0}\" class=\"font-12\">{1}</label></li>", str3.ToString(), this.dicMap[str3.ToString()]));}}}builder.Append("</ul></div>");//this.Page.RegisterClientScriptBlock("createControl", builder.ToString());this.ClientScript.RegisterClientScriptBlock(this.GetType(), "createControl", builder.ToString());}protected string ExecAction(string whereCase, string loadXml, Dictionary<string, string> excludeDic){ExportSearch search = new ExportSearch(whereCase, loadXml, excludeDic, new RowFilterDictionary(this.tField, this.jTable, this.jField, this.compareKey, this.compareValue, this.equalKey, this.equalValue).RowFilterList);string importExcelDataTable = search.GetImportExcelDataTable();this.Session["OutputFileName"] = search.Name;return importExcelDataTable;}protected void Page_Load(object sender, EventArgs e){ // 其它通過(guò)網(wǎng)頁(yè)前臺(tái)實(shí)現(xiàn)。// base.Request.Form["ThisFormJustForSelectFields"] 這個(gè)是網(wǎng)頁(yè)前臺(tái)提交的值this.Page.Title = "XXX Crop : " + this.Page.Title;this.SetContextTitle();if ((base.Request.Form["ThisFormJustForSelectFields"] == null) && (this.Session["SelectFieldsToExportPath"] == null)){ //1. 第一次生成搜索網(wǎng)頁(yè)this.loadXml = (base.Request.QueryString["loadXml"] == null) ? "" : base.Request.QueryString["loadXml"];this.BuildPage(BtnType.Search);}else if ((base.Request.Form["ThisFormJustForSelectFields"] != null) && (this.Session["SelectFieldsToExportPath"] != null)){ //4.下載所需要的文件string path = base.Server.MapPath("~/export/" + HttpContext.Current.User.Identity.Name + this.Session["SelectFieldsToExportPath"].ToString() + ".xls");if (File.Exists(path)){FileInfo info = new FileInfo(path);base.Response.ClearHeaders();base.Response.ClearContent();base.Response.ContentType = "APPLICATION/OCTET-STREAM";base.Response.AppendHeader("Content-Disposition", string.Format("Attachment; Filename=\"{0}\"", this.Session["SelectFieldsToExportPath"].ToString() + ".xls"));base.Response.AppendHeader("Content-Length", info.Length.ToString());base.Response.Flush();base.Response.WriteFile(info.FullName);this.Session["selectCheckList"] = null;this.Session["SelectFieldsToExportPath"] = null;base.Response.End();}}else if ((base.Request.Form["ThisFormJustForSelectFields"] == null) && (this.Session["SelectFieldsToExportPath"] != null)){ //3.生成保存網(wǎng)頁(yè),供用戶可以下載生成的表格this.loadXml = (base.Request.QueryString["loadXml"] == null) ? "" : base.Request.QueryString["loadXml"];this.BuildPage(BtnType.Save);//this.Page.RegisterClientScriptBlock("showSaveMsg", "\r\n <script type='text/javascript'>$('loadPic').innerHTML='文件已下載,請(qǐng)保存!';\r\n $('loadPic').style.display = 'block'; $('hideSearch').style.display='none';\r\n </script>");this.ClientScript.RegisterClientScriptBlock(this.GetType(), "showSaveMsg", "\r\n <script type='text/javascript'>$('loadPic').innerHTML='文件已下載,請(qǐng)保存!';\r\n $('loadPic').style.display = 'block'; $('hideSearch').style.display='none';\r\n </script>");}else{ //2.按照選擇的字段生成文件,同時(shí)再跳轉(zhuǎn)回目前網(wǎng)頁(yè) 轉(zhuǎn)到第3步this.loadXml = (base.Request.QueryString["loadXml"] == null) ? "" : base.Request.QueryString["loadXml"];this.whereCase = (this.Session["SearchSqlString"] == null) ? "" : this.Session["SearchSqlString"].ToString();this.tField = (base.Request.QueryString["tField"] == null) ? "" : base.Request.QueryString["tField"].ToString();this.jTable = (base.Request.QueryString["jTable"] == null) ? "" : base.Request.QueryString["jTable"].ToString();this.jField = (base.Request.QueryString["jField"] == null) ? "" : base.Request.QueryString["jField"].ToString();this.compareKey = (base.Request.QueryString["compareKey"] == null) ? "" : base.Request.QueryString["compareKey"].ToString();this.compareValue = "";// base.userpositionid;this.equalKey = (base.Request.QueryString["equalKey"] == null) ? "" : base.Request.QueryString["equalKey"].ToString();this.equalValue = base.userinfo.Trim().ToLower();this.xmlFieldsToTable = new XmlFieldsToTable(base.Server.MapPath("~/XmlMapping/" + this.loadXml + ".xml"), SelXmlNodeMode.TargetNode, SelXmlNodeInfo.MappingOnly);this.dicMap = this.xmlFieldsToTable.DicMap;foreach (string str2 in base.Request.Form.AllKeys){if (this.dicMap.ContainsKey(str2)){this.dicMap.Remove(str2);this.dicChkSel.Add(str2, "");}}this.Session["selectCheckList"] = this.dicChkSel;StringBuilder builder = new StringBuilder();builder.Append(this.ExecAction(this.whereCase, this.loadXml, this.dicMap));base.DeleteTemp();this.Session["SelectFieldsToExportPath"] = DateTime.Now.Ticks.ToString();FileStream stream = new FileStream(base.Server.MapPath("~/export/" + HttpContext.Current.User.Identity.Name + this.Session["SelectFieldsToExportPath"].ToString() + ".xls"), FileMode.Create);StreamWriter writer = new StreamWriter(stream, Encoding.Unicode);writer.Write(builder.ToString());writer.Flush();writer.Close();stream.Close();HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.ToString());}}protected void SetContextTitle(){if (this.Session["ExportContextTitle"] == null){this.Label3.Text = "員工權(quán)限表"; //員工基本資料 }else{this.Label3.Text = this.Session["ExportContextTitle"].ToString();}}// Nested Typesprotected enum BtnType{Search,Save}}} View Code?
轉(zhuǎn)載于:https://www.cnblogs.com/Geton/p/4171431.html
總結(jié)
以上是生活随笔為你收集整理的动态资料导出导入平台(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 空调压缩机红灯怎么回事啊?
- 下一篇: 米的建站日记(2014年12月18日)