Beispiel #1
0
 protected void CopyTo(CFHeaderBase result)
 {
     result.field_1_numcf = field_1_numcf;
     result.field_2_need_recalculation_and_id = field_2_need_recalculation_and_id;
     result.field_3_enclosing_cell_range      = field_3_enclosing_cell_range.Copy();
     result.field_4_cell_ranges = field_4_cell_ranges.Copy();
 }
Beispiel #2
0
        public void TestCopy()
        {
            CellRangeAddress ref1 = new CellRangeAddress(1, 2, 3, 4);
            CellRangeAddress copy = ref1.Copy();

            Assert.AreEqual(ref1.ToString(), copy.ToString());
        }
Beispiel #3
0
        public void CopyMergeRow(ISheet sheet, int sourceMergeRowStartIndex, int sourceMergeRowEndIndex, int targerMergeRowStartIndex)
        {
            int targetRowFrom;
            int targetRowTo;
            CellRangeAddress sourceRegion = null;

            for (int i = 0; i < sheet.NumMergedRegions; i++)
            {
                sourceRegion = sheet.GetMergedRegion(i);
                if (sourceRegion == null)
                {
                    continue;
                }
                if (sourceRegion.FirstRow >= sourceMergeRowStartIndex && sourceRegion.LastRow <= sourceMergeRowEndIndex)
                {
                    targetRowFrom = targerMergeRowStartIndex + (sourceRegion.FirstRow - sourceMergeRowStartIndex);
                    targetRowTo   = targerMergeRowStartIndex + (sourceRegion.LastRow - sourceMergeRowStartIndex);
                    CellRangeAddress newRegion = sourceRegion.Copy();
                    newRegion.FirstRow    = targetRowFrom;
                    newRegion.FirstColumn = sourceRegion.FirstColumn;
                    newRegion.LastRow     = targetRowTo;
                    newRegion.LastColumn  = sourceRegion.LastColumn;
                    sheet.AddMergedRegion(newRegion);
                }
            }
        }
Beispiel #4
0
 protected AbstractCellRangeDataSource(ISheet sheet, CellRangeAddress cellRangeAddress)
 {
     this.sheet = sheet;
     // Make copy since CellRangeAddress is mutable.
     this.cellRangeAddress = cellRangeAddress.Copy();
     this.numOfCells       = this.cellRangeAddress.NumberOfCells;
     this.evaluator        = sheet.Workbook.GetCreationHelper().CreateFormulaEvaluator();
 }
Beispiel #5
0
        public object Clone()
        {
            FtrHeader result = new FtrHeader
            {
                recordType      = recordType,
                grbitFrt        = grbitFrt,
                associatedRange = associatedRange.Copy()
            };

            return(result);
        }
Beispiel #6
0
        /**
         * Create an enclosing CellRange for the two cell ranges.
         *
         * @return enclosing CellRange
         */

        public static CellRangeAddress CreateEnclosingCellRange(CellRangeAddress crA, CellRangeAddress crB)
        {
            if (crB == null)
            {
                return(crA.Copy());
            }

            return
                (new CellRangeAddress(
                     lt(crB.FirstRow, crA.FirstRow) ? crB.FirstRow : crA.FirstRow,
                     gt(crB.LastRow, crA.LastRow) ? crB.LastRow : crA.LastRow,
                     lt(crB.FirstColumn, crA.FirstColumn) ? crB.FirstColumn : crA.FirstColumn,
                     gt(crB.LastColumn, crA.LastColumn) ? crB.LastColumn : crA.LastColumn
                     ));
        }
Beispiel #7
0
        private static IDictionary <int, CellRangeAddress> GetRangeBetweenRows(ISheet sheet, int sourceIndex, int targetIndex)
        {
            IDictionary <int, CellRangeAddress> rangeList = new Dictionary <int, CellRangeAddress>();

            for (int i = 0; i < sheet.NumMergedRegions; i++)
            {
                CellRangeAddress region = sheet.GetMergedRegion(i);
                if (region.FirstRow <= sourceIndex && sourceIndex <= region.LastRow &&
                    region.FirstRow <= targetIndex && targetIndex <= region.LastRow)
                {
                    rangeList.Add(i, region.Copy());//克隆解决版本兼容问题:CellRangeAddress在HSSF中会随Copy操作移动,而在XSSF中不会
                }
            }
            return(rangeList);
        }
Beispiel #8
0
        public override Object Clone()
        {
            HyperlinkRecord rec = new HyperlinkRecord();

            rec._range = _range.Copy();
            rec._guid  = _guid;

            rec._linkOpts          = _linkOpts;
            rec._fileOpts          = _fileOpts;
            rec._label             = _label;
            rec._address           = _address;
            rec._moniker           = _moniker;
            rec._shortFilename     = _shortFilename;
            rec._targetFrame       = _targetFrame;
            rec._textMark          = _textMark;
            rec._uninterpretedTail = _uninterpretedTail;
            return(rec);
        }
Beispiel #9
0
        private void GenerateXlsListByTemplate <T>(string templateUrl, string xlsUrl, List <T> list, string reportName)
        {
            HSSFWorkbook hSSFWorkbook  = this.CreateWorkbook(templateUrl);
            HSSFWorkbook hSSFWorkbook2 = this.CreateWorkbook("");
            ISheet       sheetAt       = hSSFWorkbook.GetSheetAt(0);
            ISheet       sheet         = hSSFWorkbook2.CreateSheet(reportName);
            int          num           = sheetAt.LastRowNum - 1;
            int          num2          = Convert.ToInt32(sheetAt.GetRow(1).LastCellNum);
            IRow         row           = sheet.CreateRow(0);

            for (int i = 0; i < num2; i++)
            {
                ICell cell = sheetAt.GetRow(0).GetCell(i);
                if (cell == null)
                {
                    row.CreateCell(i);
                }
                else
                {
                    ICellStyle cellStyle = cell.CellStyle;
                    ICell      cell2     = row.CreateCell(i);
                    cell2.SetCellValue(cell.StringCellValue);
                    ICellStyle cellStyle2 = hSSFWorkbook2.CreateCellStyle();
                    cellStyle2.CloneStyleFrom(cellStyle);
                    cell2.CellStyle = cellStyle2;
                }
            }
            ICellStyle cellStyle3 = hSSFWorkbook2.CreateCellStyle();
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
            IRow row2 = null;

            for (int i = 0; i < list.Count; i++)
            {
                IRow row3 = sheet.CreateRow(i + 1);
                for (int j = 0; j < num2; j++)
                {
                    ICell cell = sheetAt.GetRow(1).GetCell(j);
                    if (cell == null)
                    {
                        row3.CreateCell(j);
                    }
                    else
                    {
                        if (cell.CellType == CellType.Formula)
                        {
                            ICell  cell2 = row3.CreateCell(j);
                            string text  = cell.CellFormula;
                            text = text.Replace("2", (i + 2).ToString());
                            cell2.SetCellFormula(text);
                        }
                        else
                        {
                            ICell  cell2        = row3.CreateCell(j);
                            string name         = cell.StringCellValue.Replace("[$", "").Replace("]", "");
                            Type   propertyType = properties[name].PropertyType;
                            if (propertyType == typeof(decimal) || propertyType == typeof(int) || propertyType == typeof(double))
                            {
                                cell2.SetCellValue(Convert.ToDouble(properties[name].GetValue(list[i])));
                            }
                            else
                            {
                                cell2.SetCellValue(properties[name].GetValue(list[i]).ToString());
                            }
                            if (i == 0)
                            {
                                ICellStyle cellStyle = hSSFWorkbook2.CreateCellStyle();
                                cellStyle.CloneStyleFrom(cell.CellStyle);
                                if (cellStyle != null)
                                {
                                    cell2.CellStyle = cellStyle;
                                }
                            }
                            else
                            {
                                cell2.CellStyle = row2.Cells[j].CellStyle;
                            }
                        }
                    }
                }
                row2 = row3;
            }
            ICellStyle cellStyle4 = hSSFWorkbook2.CreateCellStyle();

            for (int k = 0; k < num; k++)
            {
                IRow row4 = sheet.CreateRow(list.Count + 1 + k);
                for (int i = 0; i < num2; i++)
                {
                    IRow row5 = sheetAt.GetRow(2 + k);
                    if (row5 != null)
                    {
                        ICell cell = row5.GetCell(i);
                        if (cell != null)
                        {
                            ICellStyle cellStyle = cell.CellStyle;
                            ICell      cell2     = row4.CreateCell(i);
                            if (cell.CellType == CellType.Formula)
                            {
                                string text = cell.CellFormula;
                                string str  = text.Split(new char[]
                                {
                                    ':'
                                })[1].Replace("2", (list.Count + 1).ToString());
                                text = text.Split(new char[]
                                {
                                    ':'
                                })[0] + ":" + str;
                                cell2.SetCellFormula(text);
                            }
                            else
                            {
                                cell2.SetCellValue(cell.StringCellValue);
                            }
                            cellStyle4.CloneStyleFrom(cellStyle);
                            cell2.CellStyle = cellStyle4;
                        }
                    }
                }
            }
            for (int i = 0; i < sheetAt.NumMergedRegions; i++)
            {
                CellRangeAddress mergedRegion     = sheetAt.GetMergedRegion(i);
                CellRangeAddress cellRangeAddress = mergedRegion.Copy();
                cellRangeAddress.FirstRow = cellRangeAddress.FirstRow + list.Count - 1;
                cellRangeAddress.LastRow  = cellRangeAddress.LastRow + list.Count - 1;
                sheet.AddMergedRegion(cellRangeAddress);
            }
            sheet.ForceFormulaRecalculation = true;
            FileStream fileStream = new FileStream(xlsUrl, FileMode.Create);

            hSSFWorkbook2.Write(fileStream);
            fileStream.Close();
        }
Beispiel #10
0
        private void GenerateXlsByTemplate(string templateUrl, string xlsUrl, DataTable dt, string reportName)
        {
            HSSFWorkbook hSSFWorkbook  = this.CreateWorkbook(templateUrl);
            HSSFWorkbook hSSFWorkbook2 = this.CreateWorkbook("");
            ISheet       sheet         = hSSFWorkbook.GetSheet("Sheet1");
            ISheet       sheet2        = hSSFWorkbook2.CreateSheet(reportName);
            int          num           = sheet.LastRowNum - 1;
            IRow         row           = sheet2.CreateRow(0);

            for (int i = 0; i < 100; i++)
            {
                ICell cell = sheet.GetRow(0).GetCell(i);
                if (cell == null)
                {
                    row.CreateCell(i);
                }
                else
                {
                    ICellStyle cellStyle = cell.CellStyle;
                    ICell      cell2     = row.CreateCell(i);
                    cell2.SetCellValue(cell.StringCellValue);
                    ICellStyle cellStyle2 = hSSFWorkbook2.CreateCellStyle();
                    cellStyle2.CloneStyleFrom(cellStyle);
                    cell2.CellStyle = cellStyle2;
                }
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                IRow row2 = sheet2.CreateRow(i + 1);
                IRow row3 = null;
                for (int j = 0; j < 100; j++)
                {
                    ICell cell = sheet.GetRow(1).GetCell(j);
                    if (cell == null)
                    {
                        row2.CreateCell(j);
                    }
                    else
                    {
                        if (cell.CellType == CellType.Formula)
                        {
                            ICell  cell2 = row2.CreateCell(j);
                            string text  = cell.CellFormula;
                            text = text.Replace("2", (i + 2).ToString());
                            cell2.SetCellFormula(text);
                        }
                        else
                        {
                            ICell  cell2    = row2.CreateCell(j);
                            string text2    = cell.StringCellValue.Replace("[$", "").Replace("]", "");
                            Type   dataType = dt.Columns[text2].DataType;
                            if (dataType == typeof(decimal) || dataType == typeof(int) || dataType == typeof(double))
                            {
                                cell2.SetCellValue(Convert.ToDouble(dt.Rows[i][text2]));
                            }
                            else
                            {
                                cell2.SetCellValue(dt.Rows[i][text2].ToString());
                            }
                            if (i == 0)
                            {
                                ICellStyle cellStyle = hSSFWorkbook2.CreateCellStyle();
                                cellStyle.CloneStyleFrom(cell.CellStyle);
                                if (cellStyle != null)
                                {
                                    cell2.CellStyle = cellStyle;
                                }
                            }
                            else
                            {
                                cell2.CellStyle = row3.Cells[j].CellStyle;
                            }
                        }
                    }
                }
            }
            for (int k = 0; k < num; k++)
            {
                IRow row4 = sheet2.CreateRow(dt.Rows.Count + 1 + k);
                for (int i = 0; i < 100; i++)
                {
                    IRow row5 = sheet.GetRow(2 + k);
                    if (row5 != null)
                    {
                        ICell cell = row5.GetCell(i);
                        if (cell != null)
                        {
                            ICellStyle cellStyle = cell.CellStyle;
                            ICell      cell2     = row4.CreateCell(i);
                            if (cell.CellType == CellType.Formula)
                            {
                                string text = cell.CellFormula;
                                string str  = text.Split(new char[]
                                {
                                    ':'
                                })[1].Replace("2", (dt.Rows.Count + 1).ToString());
                                text = text.Split(new char[]
                                {
                                    ':'
                                })[0] + ":" + str;
                                cell2.SetCellFormula(text);
                            }
                            else
                            {
                                cell2.SetCellValue(cell.StringCellValue);
                            }
                            ICellStyle cellStyle2 = hSSFWorkbook2.CreateCellStyle();
                            cellStyle2.CloneStyleFrom(cellStyle);
                            cell2.CellStyle = cellStyle2;
                        }
                    }
                }
            }
            for (int i = 0; i < sheet.NumMergedRegions; i++)
            {
                CellRangeAddress mergedRegion     = sheet.GetMergedRegion(i);
                CellRangeAddress cellRangeAddress = mergedRegion.Copy();
                cellRangeAddress.FirstRow = cellRangeAddress.FirstRow + dt.Rows.Count - 1;
                cellRangeAddress.LastRow  = cellRangeAddress.LastRow + dt.Rows.Count - 1;
                sheet2.AddMergedRegion(cellRangeAddress);
            }
            sheet2.ForceFormulaRecalculation = true;
            FileStream fileStream = new FileStream(xlsUrl, FileMode.Create);

            hSSFWorkbook2.Write(fileStream);
            fileStream.Close();
        }
Beispiel #11
0
    //Grid填充
    protected static void DoGridFill(HSSFWorkbook book, DataSet dataset)
    {
        HSSFSheet sheetFillDefine = (HSSFSheet)book.GetSheet("GridFill");

        if (sheetFillDefine == null)
        {
            return;
        }

        GridDefineCollection gridDefines = new GridDefineCollection();
        GridDefine           gridDefine  = null;
        object curSection = null;

        for (int r = 1; r <= sheetFillDefine.LastRowNum; r++)
        {
            HSSFRow excelRow = (HSSFRow)sheetFillDefine.GetRow(r);
            if (excelRow == null)
            {
                continue;
            }

            //获得FixFill中的一设定行
            string[] values = new string[12];
            for (int i = 0; i <= values.Length - 1 && i <= excelRow.LastCellNum; i++)
            {
                HSSFCell cell = (HSSFCell)excelRow.GetCell(i);
                if (cell == null)
                {
                    continue;
                }

                string value;
                if (cell.CellType == CellType.NUMERIC)
                {
                    value = cell.NumericCellValue.ToString();
                }
                else
                {
                    value = cell.StringCellValue;
                }

                if (value != null)
                {
                    value = value.Trim();
                }

                values[i] = value;
            }

            string sheetName   = values[0];  //填充到的sheet
            string startRow    = values[1];  //填充到的Cell
            string blockRows   = values[2];  //替换Cell中的一部分
            string tableName   = values[3];  //用那个表中的数据填充
            string sectionName = values[4];  //用那列数据填充
            string minRows     = values[11]; //至少几行

            //应用缺省值
            if (String.IsNullOrEmpty(sheetName))
            {
                sheetName = book.GetSheetName(0);
            }

            if (!String.IsNullOrEmpty(startRow))
            {
                gridDefine = new GridDefine();
                gridDefines.Add(gridDefine);

                gridDefine.SheetName     = sheetName;
                gridDefine.Sheet         = (HSSFSheet)book.GetSheet(sheetName);
                gridDefine.StartRow      = Int32.Parse(startRow) - 1;
                gridDefine.BlockRowCount = Int32.Parse(blockRows);
                gridDefine.FillTableName = tableName;
                gridDefine.MinRows       = 0;
                Int32.TryParse(minRows, out gridDefine.MinRows);
            }

            if (gridDefine == null)
            {
                continue;
            }

            if (!String.IsNullOrEmpty(sectionName))
            {
                if (String.Compare(sectionName, "Fill", true) == 0)
                {
                    curSection = gridDefine.GridCellFills;
                }
                else if (String.Compare(sectionName, "Template", true) == 0)
                {
                    curSection = gridDefine.GridBlockTemplates;
                }
                else if (String.Compare(sectionName, "Group", true) == 0)
                {
                    curSection = gridDefine.Groups;
                }
                else
                {
                    curSection = null;
                }
            }
            else
            {
                if (curSection is GridCellFillCollection)
                {
                    GridCellFillCollection fills = curSection as GridCellFillCollection;
                    GridCellFill           fill  = new GridCellFill();
                    fills.Add(fill);

                    string cellName       = values[5];
                    string cellVarName    = values[6];
                    string fillColumnName = values[7];
                    string renderFunction = values[8];
                    string emptyText      = values[9];

                    //应用缺省值
                    if (String.IsNullOrEmpty(renderFunction))
                    {
                        renderFunction = "DefaultRender";
                    }

                    Point cellPos = YZExcelHelper.CellNameToIndex(cellName);

                    fill.RowOffset      = cellPos.Y - gridDefine.StartRow;
                    fill.ColumnIndex    = cellPos.X;
                    fill.CellVarName    = cellVarName;
                    fill.FillColumnName = fillColumnName;
                    fill.RenderFunction = renderFunction;
                    fill.EmptyText      = emptyText;
                }
                else if (curSection is GridBlockTemplateCollection)
                {
                    string templateName     = values[5];
                    string startRowIndexStr = values[6];
                    string condition        = values[7];

                    int startRowIndex = 0;
                    if (Int32.TryParse(startRowIndexStr, out startRowIndex))
                    {
                        GridBlockTemplateCollection templates = curSection as GridBlockTemplateCollection;
                        GridBlockTemplate           template  = new GridBlockTemplate(gridDefine);
                        templates.Add(template);

                        template.Name      = templateName;
                        template.StartRow  = startRowIndex - 1;
                        template.Condition = condition;
                    }
                }
                else if (curSection is GroupCollection)
                {
                    string columnName = values[5];

                    if (!String.IsNullOrEmpty(columnName))
                    {
                        GroupCollection groups = curSection as GroupCollection;
                        Group           group  = new Group();
                        groups.Add(group);

                        group.ColumnName = columnName;
                    }
                }
                else
                {
                }
            }
        }

        gridDefines.Sort();

        foreach (GridDefine grid in gridDefines)
        {
            HSSFSheet sheetfill = grid.Sheet;

            foreach (GridBlockTemplate template in grid.GridBlockTemplates)
            {
                for (int i = 0; i < grid.BlockRowCount; i++)
                {
                    HSSFRow row = (HSSFRow)sheetfill.GetRow(template.StartRow + i);
                    template.Rows.Add(row);
                }

                for (int i = 0; i < sheetfill.NumMergedRegions; i++)
                {
                    CellRangeAddress region = sheetfill.GetMergedRegion(i);
                    if (region.FirstRow >= template.StartRow && region.LastRow <= template.StartRow + grid.BlockRowCount - 1)
                    {
                        region           = region.Copy();
                        region.FirstRow -= template.StartRow;
                        region.LastRow  -= template.StartRow;
                        template.MergedRegions.Add(region);
                    }
                }

                for (int i = 0; i < book.NumberOfSheets; i++)
                {
                    HSSFSheet sheet = book.GetSheetAt(i) as HSSFSheet;

                    if (IsSystemSheet(sheet))
                    {
                        continue;
                    }

                    foreach (HSSFChart chart in HSSFChart.GetSheetCharts(sheet))
                    {
                        foreach (HSSFChart.HSSFSeries series in chart.Series)
                        {
                            if (template.Contains(sheet, series))
                            {
                                SeriesTemplate seriesTemplate = new SeriesTemplate();
                                template.SeriesTemplates.Add(seriesTemplate);
                                seriesTemplate.Chart  = chart;
                                seriesTemplate.Series = series;
                            }
                        }
                    }
                }
            }

            if (grid.GridBlockTemplates.Count != 0)
            {
                grid.GridBlockTemplates[grid.GridBlockTemplates.Count - 1].Condition = null;
            }
        }

        for (int i = 0; i < book.NumberOfSheets; i++)
        {
            HSSFSheet sheet = book.GetSheetAt(i) as HSSFSheet;
            if (IsSystemSheet(sheet))
            {
                continue;
            }

            foreach (CellValueRecordInterface recInferface in sheet.Sheet.RowsAggregate.GetValueRecords())
            {
                if (recInferface is FormulaRecordAggregate)
                {
                    FormulaRecordAggregate fraInterface  = recInferface as FormulaRecordAggregate;
                    FormulaRecord          formulaRecord = fraInterface.FormulaRecord;
                    if (formulaRecord.IsSharedFormula)
                    {
                        fraInterface.UnlinkSharedFormula();
                    }
                }
            }
        }

        foreach (GridDefine grid in gridDefines)
        {
            //创建空Grid
            HSSFSheet sheetfill         = grid.Sheet;
            DataTable dataTable         = dataset.Tables[grid.FillTableName];
            int       gridTagBlockCount = Math.Max(dataTable.Rows.Count, grid.MinRows);
            int       gridTagRowCount   = grid.BlockRowCount * gridTagBlockCount;

            //GroupData(dataTable);
            //DataView view1 = new DataView(dataTable);
            //view1.Sort = "City asc,Shop asc";
            //dataTable = view1.ToTable();

            if (dataTable != null && dataTable.Rows.Count != 0)
            {
                if (dataTable.Rows.Count != 0 && sheetfill.LastRowNum >= grid.TemplateEndRow + 1)
                {
                    sheetfill.ShiftRows(grid.TemplateEndRow + 1, sheetfill.LastRowNum, gridTagRowCount, true, false);
                }

                for (int i = 0; i < sheetfill.NumMergedRegions; i++)
                {
                    CellRangeAddress region = sheetfill.GetMergedRegion(i);
                    if (region.FirstRow <= grid.StartRow && region.LastRow >= grid.TemplateEndRow)
                    {
                        region.LastRow += Math.Max(dataTable.Rows.Count, grid.MinRows);
                    }
                }

                HSSFSheet hssfsheet = sheetfill as HSSFSheet;
                foreach (CellValueRecordInterface recInferface in hssfsheet.Sheet.RowsAggregate.GetValueRecords())
                {
                    if (recInferface is FormulaRecordAggregate)
                    {
                        FormulaRecord formulaRecord = ((FormulaRecordAggregate)recInferface).FormulaRecord;
                        Ptg[]         ptgs          = formulaRecord.ParsedExpression;
                        foreach (Ptg ptg in ptgs)
                        {
                            List <int> rowIndexs    = new List <int>();
                            List <int> newRowIndexs = new List <int>();

                            if (ptg is RefPtgBase)
                            {
                                RefPtgBase refPtg = ptg as RefPtgBase;
                                rowIndexs.Add(refPtg.Row);
                            }
                            else if (ptg is AreaPtgBase)
                            {
                                AreaPtgBase aptg = ptg as AreaPtgBase;
                                rowIndexs.Add(aptg.FirstRow);
                                rowIndexs.Add(aptg.LastRow);
                            }

                            foreach (int rowIndex in rowIndexs)
                            {
                                int newRowIndex = rowIndex;
                                if (formulaRecord.Row < grid.StartRow || formulaRecord.Row > grid.TemplateEndRow)
                                {
                                    if (rowIndex == grid.StartRow)
                                    {
                                        newRowIndex = grid.TemplateEndRow + 1;
                                    }

                                    if (rowIndex == grid.TemplateEndRow)
                                    {
                                        newRowIndex = grid.TemplateEndRow + gridTagRowCount;
                                    }
                                }

                                newRowIndexs.Add(newRowIndex);
                            }

                            for (int i = 0; i < rowIndexs.Count; i++)
                            {
                                int rowIndex    = rowIndexs[i];
                                int newRowIndex = newRowIndexs[i];

                                if (newRowIndex != rowIndex)
                                {
                                    if (ptg is RefPtgBase)
                                    {
                                        RefPtgBase refPtg = ptg as RefPtgBase;
                                        refPtg.Row = newRowIndex;
                                    }
                                    else if (ptg is AreaPtgBase)
                                    {
                                        AreaPtgBase aptg = ptg as AreaPtgBase;
                                        if (i == 0)
                                        {
                                            aptg.FirstRow = newRowIndex;
                                        }
                                        else
                                        {
                                            aptg.LastRow = newRowIndex;
                                        }
                                    }
                                    formulaRecord.ParsedExpression = ptgs;
                                }
                            }
                        }
                    }
                }

                for (int i = 0; i < book.NumberOfSheets; i++)
                {
                    HSSFSheet sheet = book.GetSheetAt(i) as HSSFSheet;

                    if (IsSystemSheet(sheet))
                    {
                        continue;
                    }

                    foreach (RecordBase recbase in sheet.Sheet.Records)
                    {
                        if (recbase is LinkedDataRecord)
                        {
                            LinkedDataRecord link = recbase as LinkedDataRecord;
                            Ptg[]            ptgs = ptgs = link.FormulaOfLink;
                            foreach (Ptg ptg in ptgs)
                            {
                                HSSFSheet sheetptg = PtgCollection.GetPtgSheet(sheet, ptg);
                                if (sheetptg == sheetfill)
                                {
                                    if (ptg is RefPtgBase)
                                    {
                                        RefPtgBase refPtg = ptg as RefPtgBase;

                                        if (refPtg.Row > grid.TemplateEndRow)
                                        {
                                            refPtg.Row        += gridTagRowCount - (grid.TemplateEndRow - grid.StartRow + 1);
                                            link.FormulaOfLink = ptgs;
                                        }
                                    }
                                    if (ptg is AreaPtgBase)
                                    {
                                        AreaPtgBase areaPtg = ptg as AreaPtgBase;
                                        if (areaPtg.FirstRow <= grid.StartRow && areaPtg.LastRow >= grid.TemplateEndRow)
                                        {
                                            areaPtg.LastRow   += gridTagRowCount - (grid.TemplateEndRow - grid.StartRow + 1);
                                            link.FormulaOfLink = ptgs;
                                        }
                                        else if (areaPtg.FirstRow > grid.TemplateEndRow)
                                        {
                                            areaPtg.FirstRow  += gridTagRowCount - (grid.TemplateEndRow - grid.StartRow + 1);
                                            areaPtg.LastRow   += gridTagRowCount - (grid.TemplateEndRow - grid.StartRow + 1);
                                            link.FormulaOfLink = ptgs;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                dataTable.Columns.Add("_gridTemplateIndex", typeof(object));
                foreach (GridBlockTemplate template in grid.GridBlockTemplates)
                {
                    if (String.IsNullOrEmpty(template.Condition))
                    {
                        foreach (DataRow dataRow in dataTable.Rows)
                        {
                            if (Convert.IsDBNull(dataRow["_gridTemplateIndex"]))
                            {
                                dataRow["_gridTemplateIndex"] = template;
                            }
                        }

                        break;
                    }

                    DataView view = new DataView(dataTable);
                    view.RowFilter = template.Condition.Replace("$totalRow", dataTable.Rows.Count.ToString());
                    DataTable rvTable = view.ToTable();
                    foreach (DataRow dataRow in rvTable.Rows)
                    {
                        int     rowIndex   = Convert.ToInt32(dataRow["RowNum"]);
                        DataRow dataRowSrc = dataTable.Rows[rowIndex - 1];
                        dataRowSrc["_gridTemplateIndex"] = template;
                    }
                }

                for (int i = 0; i < gridTagBlockCount; i++)
                {
                    GridBlockTemplate template;

                    if (i >= dataTable.Rows.Count)
                    {
                        template = grid.GridBlockTemplates[grid.GridBlockTemplates.Count - 1];
                    }
                    else
                    {
                        template = dataTable.Rows[i]["_gridTemplateIndex"] as GridBlockTemplate;
                    }

                    int startRowIndex = grid.TemplateEndRow + 1 + i * grid.BlockRowCount;
                    for (int j = 0; j < grid.BlockRowCount; j++)
                    {
                        int     newRowIndex = startRowIndex + j;
                        HSSFRow newRow      = (HSSFRow)sheetfill.GetRow(newRowIndex);
                        if (newRow == null)
                        {
                            newRow = (HSSFRow)sheetfill.CreateRow(newRowIndex);
                        }

                        if (template != null)
                        {
                            HSSFRow srcRow = template.Rows[j];
                            CopyRow(grid.BlockRowCount, j, srcRow, newRow);
                        }
                    }

                    foreach (CellRangeAddress region in template.MergedRegions)
                    {
                        sheetfill.AddMergedRegion(new CellRangeAddress(startRowIndex + region.FirstRow, startRowIndex + region.FirstRow, region.FirstColumn, region.LastColumn));
                    }

                    foreach (SeriesTemplate seriesTemplate in template.SeriesTemplates)
                    {
                        seriesTemplate.CloneSeries(sheetfill, i * grid.BlockRowCount - (template.StartRow - grid.StartRow));
                    }
                }

                foreach (GridBlockTemplate template in grid.GridBlockTemplates)
                {
                    foreach (SeriesTemplate seriesTemplate in template.SeriesTemplates)
                    {
                        seriesTemplate.Chart.RemoveSeries(seriesTemplate.Series);
                    }
                }
            }

            //删除模板
            int addedExcelRowCount = 0;
            if (dataTable != null)
            {
                addedExcelRowCount = dataTable.Rows.Count * grid.BlockRowCount;
            }

            for (int i = grid.StartRow; i <= grid.TemplateEndRow; i++)
            {
                sheetfill.CreateRow(i);
            }

            int firstRow = grid.StartRow;
            int lastRow  = grid.TemplateEndRow;

            ShiftRows(sheetfill, grid.TemplateEndRow + 1, -(grid.TemplateEndRow - grid.StartRow + 1));

            //填充数据
            if (dataTable != null)
            {
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    DataRow row = dataTable.Rows[i];
                    foreach (GridCellFill fill in grid.GridCellFills)
                    {
                        Point cellPos = new Point();
                        cellPos.X = fill.ColumnIndex;
                        cellPos.Y = grid.StartRow + fill.RowOffset + i * grid.BlockRowCount;

                        //获得要填充的cell的行列序号
                        HSSFRow rowfill = (HSSFRow)sheetfill.GetRow(cellPos.Y);
                        if (rowfill == null)
                        {
                            continue;
                        }

                        //获得要填充的cell
                        HSSFCell cellfill = (HSSFCell)rowfill.GetCell(cellPos.X);
                        if (cellfill == null)
                        {
                            cellfill = (HSSFCell)rowfill.CreateCell(cellPos.X);
                        }

                        //获得填充值
                        object valuefill = null;
                        if (dataTable.Columns.Contains(fill.FillColumnName))
                        {
                            valuefill = row[fill.FillColumnName];
                        }

                        //执行填充
                        DoRender(fill.RenderFunction, cellfill, fill.CellVarName, dataset, valuefill, fill.EmptyText);
                    }
                }

                for (int i = dataTable.Rows.Count; i < grid.MinRows; i++)
                {
                    foreach (GridCellFill fill in grid.GridCellFills)
                    {
                        Point cellPos = new Point();
                        cellPos.X = fill.ColumnIndex;
                        cellPos.Y = grid.StartRow + fill.RowOffset + i * grid.BlockRowCount;

                        //获得要填充的cell的行列序号
                        HSSFRow rowfill = (HSSFRow)sheetfill.GetRow(cellPos.Y);
                        if (rowfill == null)
                        {
                            continue;
                        }

                        //获得要填充的cell
                        HSSFCell cellfill = (HSSFCell)rowfill.GetCell(cellPos.X);
                        if (cellfill == null)
                        {
                            cellfill = (HSSFCell)rowfill.CreateCell(cellPos.X);
                        }

                        //获得填充值
                        cellfill.SetCellValue("");
                    }
                }

                MergeCells(sheetfill, grid, grid.Groups, 0, grid.StartRow, gridTagRowCount);
            }
        }
    }