private int writeDetail(WritableSheet writableSheet, IEnumerable <ColumnInfo> columnInfoList, ColumnDataSet columnDataSet, int targetRowIndex, Dictionary <int, HashSet <string> > usedCells, Dictionary <string, int> maxWidthMap) { int targetColIndex = 0; int newTargetRowIndex = targetRowIndex; foreach (ColumnInfo columnInfo in columnInfoList) { // 取得子欄位 List <ColumnDetailInfo> columnDetailInfoList = columnInfo.ColumnDetailInfoList; // 為子欄位陣列時,進行遞迴處理 if (ExcelStringUtil.NotEmpty(columnDetailInfoList)) { foreach (ColumnDetailInfo columnDetailInfo in columnDetailInfoList) { // 設定元素類別 string type = columnDetailInfo.Type; // dataId string dataId = columnDetailInfo.DataId; // 欄位下的欄位 List <ColumnInfo> childColumnInfoList = columnDetailInfo.ColumnInfoList; // ELEMENT_SINGLE if (string.Equals(type, Constant.ELEMENT_SINGLE, StringComparison.OrdinalIgnoreCase)) { // 遞迴處理 newTargetRowIndex = writeDetail( writableSheet, childColumnInfoList, columnDataSet.getSingle(dataId), newTargetRowIndex, usedCells, maxWidthMap); } if (string.Equals(type, Constant.ELEMENT_ARRAY, StringComparison.OrdinalIgnoreCase)) { // 取得 array 元素的資料集 List <ColumnDataSet> arrayDataList = columnDataSet.getArray(dataId); // 逐筆處理 foreach (ColumnDataSet arrayColumnDataSet in arrayDataList) { // 遞迴處理 newTargetRowIndex = writeDetail(writableSheet, childColumnInfoList, arrayColumnDataSet, newTargetRowIndex, usedCells, maxWidthMap); } } } continue; } // 取得 key string key = columnInfo.Key; // 取得欄位設定 WritableCellFormat cellFormat = getCellFormat(columnInfo, columnInfo); // 取得要放入 cell 的值 string content = perpareContent(key, columnInfo.DefaultValue, columnInfo.FuncId, columnInfo.FuncParam, columnDataSet.ColumnDataMap); // 取得寬度設定 int width = Convert.ToInt32(ExcelStringUtil.SafeTrim(columnInfo.Width, "0")); // 取得還未使用的 column targetColIndex = getUnUsedCol(usedCells, targetRowIndex, targetColIndex); // 取得 rowspan (之前已計算好) if (!columnDataSet.ColumnDataMap.ContainsKey(KEY_COLUMN_COLSPAN_PERFIX + key)) { throw new Exception("指定的索引鍵不在字典中 [" + key + "]"); } var rowspan = (int)columnDataSet.ColumnDataMap[KEY_COLUMN_COLSPAN_PERFIX + key]; // colspan int colspan = columnInfo.Colspan; if (colspan > 1 || rowspan > 1) { // 合併儲存格 merageCell(writableSheet, usedCells, targetColIndex, targetRowIndex, colspan, rowspan, maxWidthMap, width); // addCell //addCell(writableSheet, usedCells, targetColIndex, targetRowIndex, content, cellFormat, maxWidthMap, width, key); addCell(writableSheet, usedCells, targetColIndex, targetRowIndex, content, cellFormat, maxWidthMap, width); // 移動 col 指標 if (colspan > 0) { targetColIndex += colspan; } else { targetColIndex++; } } else { // addCell //addCell(writableSheet, usedCells, targetColIndex, targetRowIndex, content, cellFormat, maxWidthMap, width, key); addCell(writableSheet, usedCells, targetColIndex, targetRowIndex, content, cellFormat, maxWidthMap, width); // 移動 col 指標 targetColIndex++; } } targetRowIndex++; // newTargetRowIndex++; return((targetRowIndex > newTargetRowIndex) ? targetRowIndex : newTargetRowIndex); }
/// <summary> /// 計算 rowspan /// </summary> /// <param name="columnInfoList"></param> /// <param name="columnDataSet"></param> private int countRowspan(List <ColumnInfo> columnInfoList, ColumnDataSet columnDataSet) { int myRowspan = 0; foreach (ColumnInfo columnInfo in columnInfoList) { // 取得子欄位 List <ColumnDetailInfo> columnDetailInfoList = columnInfo.ColumnDetailInfoList; // 無子欄位設定時略過 if (ExcelStringUtil.IsEmpty(columnDetailInfoList)) { continue; } int currRowAdd = 0; foreach (ColumnDetailInfo columnDetailInfo in columnDetailInfoList) { // 設定元素類別 string type = columnDetailInfo.Type; // dataId string dataId = columnDetailInfo.DataId; // 欄位下的欄位 List <ColumnInfo> childColumnInfoList = columnDetailInfo.ColumnInfoList; // ELEMENT_SINGLE if (string.Equals(type, Constant.ELEMENT_SINGLE, StringComparison.OrdinalIgnoreCase)) { // 遞迴處理 currRowAdd += countRowspan(childColumnInfoList, columnDataSet.getSingle(dataId)); } if (string.Equals(type, Constant.ELEMENT_ARRAY, StringComparison.OrdinalIgnoreCase)) { // 取得 array 元素的資料集 List <ColumnDataSet> arrayDataList = columnDataSet.getArray(dataId); if (arrayDataList == null || arrayDataList.Count == 0) { arrayDataList = new List <ColumnDataSet>(); arrayDataList.Add(new ColumnDataSet()); } // 逐筆處理 foreach (ColumnDataSet arrayColumnDataSet in arrayDataList) { // 遞迴處理 currRowAdd += countRowspan(childColumnInfoList, arrayColumnDataSet); } } // 取得最大 rowspan if (currRowAdd > myRowspan) { myRowspan = currRowAdd; } } } // 加上基礎的1列 if (myRowspan == 0) { myRowspan = 1; } foreach (ColumnInfo columnInfo in columnInfoList) { // 取得子欄位 List <ColumnDetailInfo> columnDetailInfoList = columnInfo.ColumnDetailInfoList; // 有子欄位設定時略過 if (ExcelStringUtil.NotEmpty(columnDetailInfoList)) { continue; } // 取得 key string key = columnInfo.Key; // 把 rowspan值 以 key 加上固定前綴之後, 放入 資料MAP try { columnDataSet.ColumnDataMap.Add(KEY_COLUMN_COLSPAN_PERFIX + key, myRowspan); } catch (ArgumentException) { //throw new Exception("detail 的 key :[" + key + "] 重複,請檢查設定檔"); } } return(myRowspan); }
private ColumnDataSet perpareTesDataDetail(Dictionary <string, object> columnData) { // ColumnDataSet var columnDataSet = new ColumnDataSet(); columnDataSet.ColumnDataMap = columnData; // ============================================== // 年度資料 // ============================================== var yearDataList = new List <Dictionary <string, object> >(); var yearDataMap98 = new Dictionary <string, object>(); yearDataList.Add(yearDataMap98); yearDataMap98["YEAR"] = 98; yearDataMap98["1"] = 4; yearDataMap98["2"] = 1; yearDataMap98["3-1"] = 6; yearDataMap98["3-2"] = 0; yearDataMap98["3-3"] = 12; yearDataMap98["3-4"] = 0; yearDataMap98["3-5"] = 0; yearDataMap98["4"] = 1; yearDataMap98["5"] = 0; yearDataMap98["6"] = 0; yearDataMap98["7-1"] = 0; yearDataMap98["7-2"] = 0; yearDataMap98["7-3"] = 0; yearDataMap98["8"] = 24; var yearDataMap99 = new Dictionary <string, object>(); yearDataList.Add(yearDataMap99); yearDataMap99["YEAR"] = 99; yearDataMap99["1"] = 5; yearDataMap99["2"] = 0; yearDataMap99["3-1"] = 0; yearDataMap99["3-2"] = 6; yearDataMap99["3-3"] = 0; yearDataMap99["3-4"] = 12; yearDataMap99["3-5"] = 0; yearDataMap99["4"] = 0; yearDataMap99["5"] = 0; yearDataMap99["6"] = 1; yearDataMap99["7-1"] = 0; yearDataMap99["7-2"] = 0; yearDataMap99["7-3"] = 0; yearDataMap99["8"] = 24; columnDataSet.setArray("yearDataList", yearDataList); // ============================================== // 小計 // ============================================== var singleData = new Dictionary <string, object>(); singleData["1"] = 9; singleData["2"] = 1; singleData["3"] = 36; singleData["4"] = 1; singleData["5"] = 0; singleData["6"] = 1; singleData["7"] = 0; singleData["8"] = 48; columnDataSet.setSingle("countMap", singleData); return(columnDataSet); }