日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

动态资料导出导入平台(一)

發布時間:2025/5/22 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 动态资料导出导入平台(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

又來啦,又要說偷懶的主張了。在實際的項目中,資料的導入導出是經常能遇的到的,到底有沒有辦法可以做成動態的呢? 開發人員開發出來后,剩下的可以將由HELPDESK組去維護,后續如果還要增加類似的功能,HELPDESK組人員就自己可以去處理,而我們開發人員可以不用處理,節省一些不必要的時間,或者可以花更多的時間到某件事情上,而不必受過多的打擾呢。答案是有的,接下我們先講一下導出的實現,導出在這里相對簡單一點。導入將放到后面的隨筆。這里的導入導出與網上大多數人的ExcelHelper或者COM形式的導出還是有比較大的區別。

動態,所謂的動態,就是很容易的就實現數據源的切換,還有某數據源其中的一部分數據。這里我們主要用到EAI接口,按指定格式導入書寫XML,然后讀取這個XML以獲得動態的數所輸出。實現起來也不會太難。

1.前臺頁面設計。

從別的畫面到跳出這個導出面面,只需要指定相應的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')}

運行后按XML動態產生字段。

?然后用戶可以動態選擇所需要的導出字段。

導出結果如下

下面大家看一下XML到底是如何構成的。

<?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="員工帳號" 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="員工帳號" 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>

大家一看,相信也不難理解。后續如果用戶還需要導出其它數據,HELPDESK人員可以按XML做一個類似的文檔,同時在前臺寫個按鈕直接把XML參數傳給這個SelectFields頁面就可以了。開發人員不需再管任何事情。

下面把SelectFields頁面的后臺代碼也帖出。如果有人想要EAI的動態庫的話,可以留下郵箱。如果方便,順便給個贊唄。

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) //保存按鈕時,所有的選項均為不可以選 {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){ // 其它通過網頁前臺實現。// base.Request.Form["ThisFormJustForSelectFields"] 這個是網頁前臺提交的值this.Page.Title = "XXX Crop : " + this.Page.Title;this.SetContextTitle();if ((base.Request.Form["ThisFormJustForSelectFields"] == null) && (this.Session["SelectFieldsToExportPath"] == null)){ //1. 第一次生成搜索網頁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.生成保存網頁,供用戶可以下載生成的表格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='文件已下載,請保存!';\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='文件已下載,請保存!';\r\n $('loadPic').style.display = 'block'; $('hideSearch').style.display='none';\r\n </script>");}else{ //2.按照選擇的字段生成文件,同時再跳轉回目前網頁 轉到第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 = "員工權限表"; //員工基本資料 }else{this.Label3.Text = this.Session["ExportContextTitle"].ToString();}}// Nested Typesprotected enum BtnType{Search,Save}}} View Code

?

轉載于:https://www.cnblogs.com/Geton/p/4171431.html

總結

以上是生活随笔為你收集整理的动态资料导出导入平台(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。