Esempio n. 1
0
        // ===========================================================================
        // 功能區
        // ===========================================================================

        /// <summary>
        ///     產生Excel
        /// </summary>
        /// <param name="exportConfigInfo"> </param>
        /// <param name="exportDataSet"> </param>
        /// <param name="outString"></param>
        public virtual void export(ExportConfigInfo exportConfigInfo, ExportDataSet exportDataSet, Stream outString)
        {
            configInfo = exportConfigInfo;

            try
            {
                // =========================================================
                // 建立 Workbook
                // =========================================================
                WritableWorkbook writableWorkbook = Workbook.createWorkbook(outString);

                for (int sheetlIndex = 0; sheetlIndex < exportConfigInfo.SheetList.Count; sheetlIndex++)
                {
                    // =====================================================
                    // 建立 sheet
                    // =====================================================
                    // 取得 sheetlInfo 設定
                    SheetlInfo sheetlInfo = exportConfigInfo.SheetList[sheetlIndex];

                    // 取得 sheetName
                    string sheetName = (ExcelStringUtil.IsEmpty(sheetlInfo.SheetName))
                        ? "Sheet" + (sheetlIndex + 1)
                        : sheetlInfo.SheetName;

                    // 建立 sheet
                    WritableSheet writableSheet = writableWorkbook.createSheet(sheetName, sheetlIndex);

                    // 版面設定
                    // setPageSetup Parameters:
                    // p - the page orientation
                    // ps - the paper size
                    // hm - the header margin, in inches
                    // fm - the footer margin, in inches
                    writableSheet.setPageSetup(PageOrientation.LANDSCAPE, exportConfigInfo.PaperSize, 0, 0);
                    writableSheet.getSettings().setLeftMargin(0);
                    writableSheet.getSettings().setRightMargin(0);

                    // =====================================================
                    // 處理前準備
                    // =====================================================
                    // 列指標
                    int targetRowIndex = 0;
                    // 紀錄已使用的儲存格 (cell)
                    var usedCells = new Dictionary <int, HashSet <string> >();
                    // 紀錄欄(column)的最大欄寬
                    var maxWidthMap = new Dictionary <string, int>();

                    // =====================================================
                    // 資訊
                    // =====================================================
                    foreach (var entry in sheetlInfo.PartInfoMap)
                    {
                        if (entry.Value == null)
                        {
                            return;
                        }

                        // 內容為 context
                        if (entry.Key.StartsWith(Constant.ELEMENT_CONTEXT))
                        {
                            //取得 context 設定檔設定資料
                            var contextInfo = (ContextInfo)entry.Value;
                            //取得 匯出資料
                            Dictionary <string, object> dataMap = exportDataSet.getContext(contextInfo.DataId);

                            targetRowIndex = WriteContext(
                                writableSheet,
                                contextInfo,
                                targetRowIndex,
                                dataMap,
                                usedCells,
                                maxWidthMap);
                        }

                        //內容為 detail
                        if (entry.Key.StartsWith(Constant.ELEMENT_DETAIL))
                        {
                            //取得 context 設定檔設定資料
                            var detailInfo = (DetailInfo)entry.Value;
                            //取得 匯出資料
                            var columnDataSetList = exportDataSet.getDetail(detailInfo.DataId);

                            targetRowIndex = EnterWriteDetail(
                                writableSheet,
                                detailInfo,
                                targetRowIndex,
                                columnDataSetList,
                                usedCells,
                                maxWidthMap);
                        }
                    }

                    // =====================================================
                    // 設定欄寬
                    // =====================================================
                    // 取得最大欄位 index
                    int maxColIndex = maxWidthMap[KEY_MAX_COL];
                    for (int colIndex = 0; colIndex <= maxColIndex; colIndex++)
                    {
                        // 取得欄寬
                        int colWidth = 0;
                        //取得 MAP 中的值 (tr、td 設定)
                        if (maxWidthMap.ContainsKey(colIndex + ""))
                        {
                            colWidth = maxWidthMap[colIndex + ""];
                        }
                        //若 tr td 未設定時,取 style 設定
                        if (colWidth == 0)
                        {
                            colWidth = Convert.ToInt32(ExcelStringUtil.SafeTrim(exportConfigInfo.StyleInfo.Width, "0"));
                        }

                        // 以上都未設定時使用預設值
                        //if (colWidth == 0)
                        //{
                        //    colWidth = Convert.ToInt32(Constant.DEFAULT_WIDTH);
                        //}

                        if (colWidth > 0)
                        {
                            writableSheet.setColumnView(colIndex, colWidth);
                        }
                    }
                }

                writableWorkbook.write();
                writableWorkbook.close();
            }
            catch (Exception e)
            {
                throw new ExcelOperateException("EXCEL 檔案匯出處理錯誤! \r\n" + e.Message + "\r\n" + e.StackTrace);
            }
        }
Esempio n. 2
0
        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);
        }