/// <summary> /// 讀取 format 標籤設定 (正規表示式檢核設定) /// </summary> /// <param name="document"> XmlDocument </param> /// <returns></returns> protected Dictionary <string, FormatInfo> readFormatInfo(XmlDocument document) { // ========================================================= // 讀取 function 設定 // ========================================================= XmlNodeList formatNodeList = document.SelectNodes("//" + Constant.ELEMENT_FORMAT); // ========================================================= // 解析 NODE 設定 // ========================================================= var formatInfoMap = new Dictionary <string, FormatInfo>(); if (formatNodeList == null) { return(formatInfoMap); } foreach (XmlNode funcNode in formatNodeList) { var formatInfo = new FormatInfo(); if (funcNode.Attributes == null) { continue; } formatInfo.FormatId = ExcelStringUtil.GetNodeAttr(funcNode, Constant.ATTRIBUTE_FORMATID); formatInfo.Regex = ExcelStringUtil.GetNodeAttr(funcNode, Constant.ATTRIBUTE_REGEX); formatInfo.RegexErrorMsg = ExcelStringUtil.GetNodeAttr(funcNode, Constant.ATTRIBUTE_REGEX_ERROR_MSG); formatInfoMap.Add(formatInfo.FormatId, formatInfo); } return(formatInfoMap); }
/// <summary> /// 讀取 function 標籤設定 /// </summary> /// <param name="document"> XmlDocument </param> /// <returns></returns> protected Dictionary <string, FunctionInfo> readFunctionInfo(XmlDocument document) { // ========================================================= // 讀取 function 設定 // ========================================================= XmlNodeList functionNodeList = document.SelectNodes("//" + Constant.ELEMENT_FUNCTION); // ========================================================= // 解析 NODE 設定 // ========================================================= var functionInfoMap = new Dictionary <string, FunctionInfo>(); if (functionNodeList == null) { return(functionInfoMap); } foreach (XmlNode funcNode in functionNodeList) { var functionInfo = new FunctionInfo(); if (funcNode.Attributes == null) { continue; } functionInfo.FuncId = ExcelStringUtil.GetNodeAttr(funcNode, Constant.ATTRIBUTE_FUNCID); functionInfo.ClassName = ExcelStringUtil.GetNodeAttr(funcNode, Constant.ATTRIBUTE_CLASSNAME); functionInfo.Method = ExcelStringUtil.GetNodeAttr(funcNode, Constant.ATTRIBUTE_METHOD); functionInfoMap.Add(functionInfo.FuncId, functionInfo); } return(functionInfoMap); }
// ===================================================== // 公用程式 // ===================================================== /// <summary> /// 讀取 Node 中,與 Data Column 類型元素 相關的屬性 /// </summary> /// <param name="node">XmlNode</param> public void readDataColumnAttr(XmlNode node) { if (node == null || node.Attributes == null) { return; } //key Key = ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_KEY); //funcId FuncId = ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_FUNCID); //FuncParam FuncParam = ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_FUNC_PARAM); //colspan Colspan = Convert.ToInt32(ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_COLSPAN, "0")); // defaultValue (先讀取 node, node 無值時, 讀取 attr) XmlNode defaultValueNode = node.SelectSingleNode(Constant.ELEMENT_DEFAULT_VALUE); string defaultValue = ""; if (defaultValueNode != null) { defaultValue = defaultValueNode.InnerText; } if (ExcelStringUtil.IsEmpty(defaultValue) && node.Attributes != null) { defaultValue = ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_DEFAULT_VALUE); } DefaultValue = defaultValue; }
/// <summary> /// 解析 sheet 層的設定 /// </summary> /// <param name="partNode">子項目名稱</param> private List <TrInfo> readContextInfo(XmlNode partNode) { List <TrInfo> trInfoList = new List <TrInfo>(); // 未設定時返回 if (partNode == null) { return(trInfoList); } // 取得 tr node list XmlNodeList trNodeList = partNode.SelectNodes(Constant.ELEMENT_TR); // 未設定時返回 if (trNodeList == null || trNodeList.Count == 0) { return(trInfoList); } foreach (XmlNode trNode in trNodeList) { TrInfo trInfo = new TrInfo(); // 讀取 style 屬性設定 trInfo.readStyleAttr(trNode); // 取得 TD 設定 list 設定 XmlNodeList tdNodeList = trNode.SelectNodes(Constant.ELEMENT_TD); // 檢核 if (tdNodeList == null || tdNodeList.Count == 0) { throw new ExcelOperateException("<tr> 標籤下, 不可無 <td> 設定!"); } // 取得TD 設定 List <TdInfo> tdInfoList = new List <TdInfo>(); foreach (XmlNode tdNode in tdNodeList) { TdInfo tdInfo = new TdInfo(); // 讀取rowspan 屬性 (TdInfo 獨有) if (tdNode.Attributes != null) { tdInfo.Rowspan = Convert.ToInt32(ExcelStringUtil.GetNodeAttr(tdNode, Constant.ATTRIBUTE_ROWSPAN, "0")); } // 讀取資料元素設定 tdInfo.readDataColumnAttr(tdNode); // 讀取 style 屬性設定 tdInfo.readStyleAttr(tdNode); tdInfoList.Add(tdInfo); } trInfo.TdInfoList = tdInfoList; trInfoList.Add(trInfo); } return(trInfoList); }
private List <ColumnInfo> readDetailInfo(XmlNode partNode) { List <ColumnInfo> columnInfoList = new List <ColumnInfo>(); // 取得 column node list XmlNodeList columnList = partNode.SelectNodes(Constant.ELEMENT_COLUMN); foreach (XmlNode columnNode in columnList) { ColumnInfo columnInfo = new ColumnInfo(); // 讀取資料元素設定 columnInfo.readDataColumnAttr(columnNode); // 讀取 style 屬性設定 columnInfo.readStyleAttr(columnNode); // 取得子元素list XmlNodeList columnDeatilNodList = columnNode.SelectNodes("(" + Constant.ELEMENT_ARRAY + "|" + Constant.ELEMENT_SINGLE + ")"); // column(array|single)[遞迴] List <ColumnDetailInfo> columnDetailInfoList = new List <ColumnDetailInfo>(); if (columnDeatilNodList != null) { foreach (XmlNode columnDetailNode in columnDeatilNodList) { ColumnDetailInfo columnDetailInfo = new ColumnDetailInfo(); // type columnDetailInfo.Type = columnDetailNode.Name; // dataId columnDetailInfo.DataId = ExcelStringUtil.GetNodeAttr(columnDetailNode, Constant.ATTRIBUTE_DATAID); // column var xmlNodeList = columnDetailNode.SelectNodes(Constant.ELEMENT_COLUMN); if (xmlNodeList != null && xmlNodeList.Count > 0) { columnDetailInfo.ColumnInfoList = this.readDetailInfo(columnDetailNode); } // add to list columnDetailInfoList.Add(columnDetailInfo); } } columnInfo.ColumnDetailInfoList = columnDetailInfoList; columnInfoList.Add(columnInfo); } return(columnInfoList); }
// ===================================================== // 元素屬性 // ===================================================== // ===================================================== // 公用程式 // ===================================================== /// <summary> /// 讀取 Node 中,共通屬性 </summary> /// <param name="node"> </param> public virtual void readCommonAttr(XmlNode node) { if (node == null) { return; } // key this.Key = ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_KEY); // desc this.Desc = ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_DESC); // funcId this.FuncId = ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_FUNCID); // index this.Index = ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_INDEX); // defaultValue (先讀取 node, node 無值時, 讀取 attr) XmlNode defaultValueNode = node.SelectSingleNode(Constant.ELEMENT_DEFAULT_VALUE); string defaultValue = ""; if (defaultValueNode != null) { defaultValue = defaultValueNode.Value; } if (ExcelStringUtil.IsEmpty(defaultValue)) { defaultValue = ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_DEFAULT_VALUE); } this.DefaultValue = defaultValue; // funcParam (先讀取 node, node 無值時, 讀取 attr) XmlNode funcParamNode = node.SelectSingleNode(Constant.ELEMENT_FUNC_PARAM); string funcParam = ""; if (funcParamNode != null) { funcParam = funcParamNode.Value; } if (ExcelStringUtil.IsEmpty(funcParam)) { funcParam = ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_FUNC_PARAM); } this.FuncParam = funcParam; }
// ===================================================== // 公用程式 // ===================================================== /// <summary> /// 讀取 Node 中,與 style 類型元素 相關的屬性 /// </summary> /// <param name="node"></param> public void readStyleAttr(XmlNode node) { if (node == null) { return; } if (node.Attributes == null) { return; } // 字型 Font = getFont(ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_FONT)); // 字體大小 Size = ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_SIZE); // 粗體 Bold = ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_BOLD); // 斜體 Italic = ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_ITALIC); // 底線 Underline = getUnderlineStyle(ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_UNDERLINE)); // 列高 Height = ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_HEIGHT); // 欄寬 Width = ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_WIDTH); // 文字顏色 Color = getColour(ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_COLOR)); // 水平位置 Align = getAlign(ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_ALIGN)); // 垂直位置 Valign = getValign(ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_VALIGN)); // 自動換行 Wrap = ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_WRAP); // 背景顏色 Background = getColour(ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_BACKGROUND)); // 邊線位置 BorderSide = getBorderSide(ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_BORDERSIDE)); // 邊線樣式 BorderStyle = getBorderLineStyle(ExcelStringUtil.GetNodeAttr(node, Constant.ATTRIBUTE_BORDERSTYLE)); }
private ExportConfigInfo readExcelInfo(XmlDocument document, string id) { // ========================================================= // 讀取設定資訊 // ========================================================= XmlNode excelNode = document.SelectSingleNode( "//" + Constant.ELEMENT_EXCEL + "[@" + Constant.ATTRIBUTE_ID + "=\"" + id + "\"]"); if (excelNode == null) { throw new ExcelOperateException("設定資訊:[" + id + "] 不存在!"); } // ========================================================= // 讀取 excelInfo // ========================================================= var exportConfigInfo = new ExportConfigInfo { Id = ExcelStringUtil.GetNodeAttr(excelNode, Constant.ATTRIBUTE_ID), FileName = ExcelStringUtil.GetNodeAttr(excelNode, Constant.ATTRIBUTE_FILENAME), PaperSize = this.getPaperSize( ExcelStringUtil.GetNodeAttr(excelNode, Constant.ATTRIBUTE_PAPERSIZE)) }; // ========================================================= // 讀取 範圍 style 設定 // ========================================================= // 讀取 style node XmlNode styleNode = excelNode.SelectSingleNode(Constant.ELEMENT_STYLE); // 讀取屬性設定 var styleInfo = new StyleInfo(); styleInfo.readStyleAttr(styleNode); // 將未設定的屬性,設為系統預設值 styleInfo.setEmptyAttrToSystemDefault(); // 放入 excelInfo exportConfigInfo.StyleInfo = styleInfo; // ========================================================= // 讀取 sheet 設定 // =========================================================」 XmlNodeList sheetNodeList = excelNode.SelectNodes(Constant.ELEMENT_SHEET); // 記錄已使用的 dataId var dataIdSet = new Dictionary <string, Boolean>(); // 逐筆讀取 var sheetList = new List <SheetlInfo>(); if (sheetNodeList != null) { foreach (XmlNode sheetNode in sheetNodeList) { // sheet 基本資訊 var sheetlInfo = new SheetlInfo { Id = ExcelStringUtil.GetNodeAttr(sheetNode, Constant.ATTRIBUTE_ID), SheetName = ExcelStringUtil.GetNodeAttr(sheetNode, Constant.ATTRIBUTE_SHEETNAME) }; // sheet 以下的 part 設定 var partInfoMap = new Dictionary <string, object>(); // 取得Node list XmlNodeList nodeList = sheetNode.SelectNodes("(" + Constant.ELEMENT_CONTEXT + "|" + Constant.ELEMENT_DETAIL + ")"); // 解析 node 設定 for (int i = 0; i < nodeList.Count; i++) { // 取得 node XmlNode partInfoNode = nodeList[i]; // 取得 dataId if (partInfoNode.Attributes != null) { string dataId = ExcelStringUtil.GetNodeAttr(partInfoNode, Constant.ATTRIBUTE_DATAID); // 檢核 dataId 不可重複 if (dataIdSet.ContainsKey(dataId)) { throw new ExcelOperateException( " <sheet> 標籤下的 (context|detail) 標籤, dataId 不可重複! " + "[" + dataId + "] (取用 ExportDataSet 中資料時會造成異常)"); } dataIdSet.Add(dataId, true); // 依據標籤類型,進行解析 if (Constant.ELEMENT_CONTEXT.Equals(partInfoNode.Name)) { var contextInfo = new ContextInfo(); contextInfo.DataId = dataId; contextInfo.TrInfoList = this.readContextInfo(partInfoNode); partInfoMap.Add(partInfoNode.Name + "_" + i, contextInfo); } else if (Constant.ELEMENT_DETAIL.Equals(partInfoNode.Name)) { var xmlNodeList = partInfoNode.SelectNodes(Constant.ELEMENT_COLUMN); if (xmlNodeList != null && xmlNodeList.Count < 1) { throw new ExcelOperateException("<detail> 標籤下, 不可無 <column> 設定!"); } var detailInfo = new DetailInfo { DataId = dataId, ColumnInfoList = this.readDetailInfo(partInfoNode) }; partInfoMap.Add(partInfoNode.Name + "_" + i, detailInfo); } } } // sheetlInfo.PartInfoMap = partInfoMap; // 放入List sheetList.Add(sheetlInfo); } } // 放入 excelInfo exportConfigInfo.SheetList = sheetList; return(exportConfigInfo); }
/// <summary> /// ExportConfigInfo /// </summary> /// <param name="document"> </param> /// <param name="id"></param> private ImportConfigInfo readExcelInfo(XmlDocument document, string id) { // ========================================================= // 讀取設定資訊 // ========================================================= XmlNode excelNode = document.SelectSingleNode( "//" + Constant.ELEMENT_EXCEL + "[@" + Constant.ATTRIBUTE_ID + "=\"" + id + "\"]"); //XmlNode excelNode = document.SelectSingleNode("//" + Constant.ELEMENT_EXCEL + "[" + Constant.ATTRIBUTE_ID + "=\"" + configID + "\"]"); if (excelNode == null) { throw new ExcelOperateException("設定資訊:[" + id + "] 不存在!"); } // ========================================================= // 讀取 excel 標籤屬性 // ========================================================= ImportConfigInfo importConfigInfo = new ImportConfigInfo(); //configID importConfigInfo.Desc = ExcelStringUtil.GetNodeAttr(excelNode, Constant.ATTRIBUTE_ID); //sheetNum string sheetNum = ExcelStringUtil.GetNodeAttr(excelNode, Constant.ATTRIBUTE_SHEETNUM, "1"); if (!ExcelStringUtil.isNumber(sheetNum)) { throw new ExcelOperateException("屬性 sheetNum 設定錯誤! sheetNum:[" + sheetNum + "]"); } importConfigInfo.SheetNum = Convert.ToInt32(sheetNum); //startRow string startRow = ExcelStringUtil.GetNodeAttr(excelNode, Constant.ATTRIBUTE_STARTROW); if (!ExcelStringUtil.isNumber(startRow)) { throw new ExcelOperateException("屬性 startRow 設定錯誤! startRow:[" + startRow + "]"); } importConfigInfo.StartRow = Convert.ToInt32(startRow); //CheckEmptyRow importConfigInfo.CheckEmptyRow = ExcelStringUtil.GetNodeAttr(excelNode, Constant.ATTRIBUTE_CHECK_EMPTY_ROW); //check duplicate importConfigInfo.CheckDuplicate = ExcelStringUtil.GetNodeAttr(excelNode, Constant.ATTRIBUTE_CHECK_DUPLICATE); //desc importConfigInfo.Desc = ExcelStringUtil.GetNodeAttr(excelNode, Constant.ATTRIBUTE_DESC); // ========================================================= // 讀取 excel/read 標籤 下的 column // ========================================================= //讀取 node list XmlNodeList columnNodeList = excelNode.SelectNodes(Constant.ELEMENT_READ + "/" + Constant.ELEMENT_COLUMN); //檢核 if (ExcelStringUtil.IsEmpty(columnNodeList)) { throw new ExcelOperateException("未找到任何 <column> (config/excel/read/column)"); } //收集屬性設定list IList <ColumnInfo> columnInfoList = new List <ColumnInfo>(); importConfigInfo.ColumnInfoList = columnInfoList; //紀錄KEY避免重複 var keySet = new HashSet <string>(); //逐筆讀取 if (columnNodeList != null) { foreach (XmlNode columnNode in columnNodeList) { //未設定 key 代表要略過的欄位 //if (ExcelStringUtil.GetNodeAttr(columnNode, Constant.ATTRIBUTE_KEY, "@@xx") == "@@xx") //{ // continue; //}; //初始化物件 ColumnInfo columnInfo = new ColumnInfo(); //讀取共通屬性 columnInfo.readCommonAttr(columnNode); //FormatId columnInfo.FormatId = ExcelStringUtil.GetNodeAttr(columnNode, Constant.ATTRIBUTE_FORMATID); //regexp columnInfo.Regex = ExcelStringUtil.GetNodeAttr(columnNode, Constant.ATTRIBUTE_REGEX); //RegexErrorMsg columnInfo.RegexErrorMsg = ExcelStringUtil.GetNodeAttr(columnNode, Constant.ATTRIBUTE_REGEX_ERROR_MSG); //isNull columnInfo.CheckNull = ExcelStringUtil.GetNodeAttr(columnNode, Constant.ATTRIBUTE_CHECK_NULL); //pass columnInfo.Pass = "******".Equals(ExcelStringUtil.GetNodeAttr(columnNode, Constant.ATTRIBUTE_PASS), StringComparison.CurrentCultureIgnoreCase); //add to list columnInfoList.Add(columnInfo); //檢核Key 是否重複 this.checkKey(keySet, columnInfo.Key); } } // ========================================================= // 讀取 excel/params 標籤 下的 param // ========================================================= //讀取 node list XmlNodeList paramNodeList = excelNode.SelectNodes(Constant.ELEMENT_PARAMS + "/" + Constant.ELEMENT_PARAM); //收集屬性設定list IList <ParamInfo> paramInfoList = new List <ParamInfo>(); importConfigInfo.ParamInfoList = paramInfoList; //逐筆讀取 if (paramNodeList != null) { foreach (XmlNode paramNode in paramNodeList) { //初始化物件 ParamInfo paramInfo = new ParamInfo(); //讀取共通屬性 paramInfo.readCommonAttr(paramNode); //add to list paramInfoList.Add(paramInfo); //檢核Key 是否重複 this.checkKey(keySet, paramInfo.Key); } } return(importConfigInfo); }