Exemple #1
0
        public Exporter NewExport(IExport export)
        {
            if (_title == null)
            {
                _title = new List <List <Column> >();
                _title.Add(new List <Column>());
                EachHelper.EachListHeader(_data, (i, field, type) => _title[0].Add(new Column()
                {
                    title = field, field = field, rowspan = 1, colspan = 1
                }));
            }

            Dictionary <int, int>            currentHeadRow = new Dictionary <int, int>();
            Dictionary <string, List <int> > fieldIndex     = new Dictionary <string, List <int> >();
            Func <int, int> GetCurrentHeadRow = cell => currentHeadRow.ContainsKey(cell) ? currentHeadRow[cell] : 0;
            // var currentRowindex = 2;
            var currentRow  = 2;
            var currentCell = 0;

            export.Init(_data);

            //生成多行题头
            for (var i = 0; i < _title.Count; i++)
            {
                currentCell = 0;

                for (var j = 0; j < _title[i].Count; j++)
                {
                    var item = _title[i][j];
                    if (item.hidden)
                    {
                        continue;
                    }

                    while (currentRow < GetCurrentHeadRow(currentCell) + 2)
                    {
                        currentCell++;
                    }
                    if (item.field != "ck")
                    {
                        export.FillData(currentCell, currentRow, "title_" + item.field, item.title);

                        if (item.rowspan + item.colspan > 2)
                        {
                            export.MergeCell(currentCell, currentRow, currentCell + item.colspan - 1, currentRow + item.rowspan - 1);
                        }

                        if (!string.IsNullOrEmpty(item.field))
                        {
                            if (!fieldIndex.ContainsKey(item.field))
                            {
                                fieldIndex[item.field] = new List <int>();
                            }
                            fieldIndex[item.field].Add(currentCell);
                        }

                        for (var k = 0; k < item.colspan; k++)
                        {
                            currentHeadRow[currentCell] = GetCurrentHeadRow(currentCell++) + item.rowspan;
                        }
                    }
                }
                currentRow++;
            }

            export.FillTitleData(0, 0, "", _toptile);
            export.MergeCell(0, 0, currentCell - 1, 1);
            //设置题头样式
            export.SetHeadStyle(0, 0, currentCell - 1, currentRow - 1);

            //设置数据样式
            var dataCount = 0;

            dataCount = _datatable.Rows.Count;
            //EachHelper.EachListRow(_data, (i, r) => dataCount++);
            export.SetRowsStyle(0, currentRow, currentCell - 1, currentRow + dataCount - 1);

            //填充数据
            //            EachHelper.EachListRow(_data, (rowIndex, rowData) =>
            //            {
            //                EachHelper.EachObjectProperty(rowData, (i, name, value) =>
            //                {
            //                    if (fieldIndex.ContainsKey(name))
            //                        foreach (int cellIndex in fieldIndex[name])
            //                        {
            //                            if (_fieldFormatter.ContainsKey(name))
            //                                value = _fieldFormatter[name].Format(value);
            //                            export.FillData(cellIndex, currentRow, name, value);
            //                        }
            //                });
            //                currentRow++;
            //            });
            int cellIndex = 0;
            int cellnum   = currentCell;
            int h         = 0;

            if (_datatable.Columns.Contains("ck"))
            {
                cellnum++;
                h++;
            }
            if (_datatable.Columns.Contains("ID"))
            {
                cellnum++;
                h++;
            }
            foreach (DataRow row in _datatable.Rows)
            {
                int k = 0;
                for (int i = h; i < cellnum; i++)
                {
                    export.FillData(k, currentRow, "", row[i]);
                    k++;
                }


                currentRow++;
            }
            export.SaveAsStream(Path);

            //_suffix = export.suffix;
            //if (string.IsNullOrEmpty(_fileName))
            //    _fileName = DateTime.Now.ToString("yyyyMMddHHmmss");

            return(this);
        }