Beispiel #1
0
    /// <summary>
    /// 加载页签
    /// </summary>
    public void Load(IWorkbook workbook, ISheet sheet)
    {
        _workbook = workbook;
        _sheet    = sheet;

        // 公式计算器
        _evaluator = new XSSFFormulaEvaluator(_workbook);

        int firstRowNum = sheet.FirstRowNum;

        // 数据头一共三行
        IRow row1 = sheet.GetRow(firstRowNum);         //类型
        IRow row2 = sheet.GetRow(++firstRowNum);       //名称
        IRow row3 = sheet.GetRow(++firstRowNum);       //CBS

        // 检测策划备注行
        while (true)
        {
            int checkRow = firstRowNum + 1;
            if (checkRow > sheet.LastRowNum)
            {
                break;
            }
            IRow row = sheet.GetRow(checkRow);
            if (IsNotesRow(row))
            {
                ++firstRowNum;
            }
            else
            {
                break;
            }
        }

        // 组织头部数据
        for (int cellNum = row1.FirstCellNum; cellNum < row1.LastCellNum; cellNum++)
        {
            ICell row1cell = row1.GetCell(cellNum);
            ICell row2cell = row2.GetCell(cellNum);
            ICell row3cell = row3.GetCell(cellNum);

            // 检测重复的列
            string headName   = GetCellValue(row1cell);
            bool   isNotesRow = headName.Contains(ConstDefine.StrNotesRow);
            if (isNotesRow == false)
            {
                if (IsContainsHead(headName))
                {
                    throw new Exception($"检测到重复列 : {headName}");
                }
            }

            // 创建Wrapper
            string      type    = GetCellValue(row1cell);
            string      name    = GetCellValue(row2cell);
            string      logo    = GetCellValue(row3cell);
            HeadWrapper wrapper = new HeadWrapper(cellNum, name, type, logo);
            Heads.Add(wrapper);
        }

        // 如果没有ID列
        if (IsContainsHead(ConstDefine.StrHeadId) == false)
        {
            throw new Exception("表格必须设立一个 'id' 列.");
        }

        // 所有数据行
        int tableBeginRowNum = ++firstRowNum;         //Table初始行

        for (int rowNum = tableBeginRowNum; rowNum <= sheet.LastRowNum; rowNum++)
        {
            IRow row = sheet.GetRow(rowNum);

            // 如果是结尾行
            if (IsEndRow(row))
            {
                break;
            }

            TableWrapper wrapper = new TableWrapper(rowNum, row);
            wrapper.CacheAllCellValue(this);
            Tables.Add(wrapper);
        }

        // 创建所有注册的导出器
        for (int i = 0; i < ExportHandler.ExportTypes.Count; i++)
        {
            Type         type     = ExportHandler.ExportTypes[i];
            BaseExporter exporter = (BaseExporter)Activator.CreateInstance(type, this);
            _exporters.Add(exporter);
        }
    }