Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        private int WriteContext(
            WritableSheet writableSheet,
            ContextInfo contextInfo,
            int targetRowIndex,
            Dictionary <string, object> dataMap,
            Dictionary <int, HashSet <string> > usedCells,
            Dictionary <string, int> maxWidthMap)
        {
            // 無資料時跳出
            if (contextInfo.TrInfoList == null)
            {
                return(targetRowIndex);
            }

            // 逐列處理
            for (int row = 0; row < contextInfo.TrInfoList.Count; row++)
            {
                //取得 TrInfo
                TrInfo trInfo = contextInfo.TrInfoList[row];

                // col index 指標
                int targetColIndex = 0;

                for (int col = 0; col < trInfo.TdInfoList.Count; col++)
                {
                    // 取得 TdInfo
                    TdInfo tdInfo = trInfo.TdInfoList[col];
                    // 取得欄位設定
                    WritableCellFormat cellFormat = getCellFormat(trInfo, tdInfo);
                    // 取得要放入 cell 的值
                    string content = perpareContent(tdInfo.Key, tdInfo.DefaultValue, tdInfo.FuncId, tdInfo.FuncParam,
                                                    dataMap);
                    // 取得寬度設定
                    int width = Convert.ToInt32(ExcelStringUtil.SafeTrim(tdInfo.Width, "0"));
                    // 取得還未使用的 column
                    targetColIndex = getUnUsedCol(usedCells, targetRowIndex, targetColIndex);

                    if (tdInfo.Colspan > 1 || tdInfo.Rowspan > 1)
                    {
                        // 合併儲存格
                        merageCell(writableSheet, usedCells, targetColIndex, targetRowIndex, tdInfo.Colspan,
                                   tdInfo.Rowspan, maxWidthMap, width);
                        // addCell
                        //addCell(writableSheet, usedCells, targetColIndex, targetRowIndex, content, cellFormat, maxWidthMap, width, tdInfo.Key);
                        addCell(writableSheet, usedCells, targetColIndex, targetRowIndex, content, cellFormat,
                                maxWidthMap, width);

                        // 移動 col 指標
                        if (tdInfo.Colspan > 0)
                        {
                            targetColIndex += tdInfo.Colspan;
                        }
                        else
                        {
                            targetColIndex++;
                        }
                    }
                    else
                    {
                        // addCell
                        //addCell(writableSheet, usedCells, targetColIndex, targetRowIndex, content, cellFormat, maxWidthMap, width, tdInfo.Key);
                        addCell(writableSheet, usedCells, targetColIndex, targetRowIndex, content, cellFormat,
                                maxWidthMap, width);
                        // 移動 col 指標
                        targetColIndex++;
                    }
                }

                // 取得列高設定
                //height of the row in 1/20ths of a point
                int height = Convert.ToInt32(ExcelStringUtil.SafeTrim(trInfo.Height, "0")) * 20;
                if (height > 0)
                {
                    writableSheet.setRowView(targetRowIndex, height);
                }

                targetRowIndex++;
            }
            return(targetRowIndex);
        }