树结构关系的数据导出为excel

该文针对的是关于树结构的数据的导出,每一个节点都可以创建不定数的子节点,在选择好某个节点进行导出
时,会把该节点以及子节点的数据都导出来。导出后的excel的格式大概如下图的形式,这个是一个比较理想
的状态,合并的空单元格也比较顺利,但实际的子节点数肯定不可能是一样的,合并必要的单元格也肯定不会
这么简单。

难点就在于得到数据和合并必要的空单元格。其实现在的数据导出有很多的博文,并且有关的一些坑也踩了好
多。导出像上图的excel格式的话,首先需要通过深度优先得到该子节点下的数据,然后得到不合并单元格的
excel格式,再然后通过从得到的树的深度的倒数第二层来看看得到需要合并的范围。依据就是首先查出该节
点的子节点数,在遍历子节点的一列,记录下合并的起始位置,结束位置来进行合并

通过深度优先得到要导出的数据

       //遍历tree
        public List<Dictionary<string, object>> getTree(string id, int width)
        {
            List<ft_topic> list = getChild(id);
            width++;
            int hex = height + 1;
            Dictionary<string, object> map = new Dictionary<string, object>();
            ft_topic entity = db.ft_topic.Where(t => t.id == id).Single();
            map.Add("name", entity.name);
            map.Add("id", entity.id);
            map.Add("width", width);
            map.Add("height", height);
            entities.Add(map);
            for (int i = 0; i < list.Count; i++)
            {
                getTree(list[i].id, width);
            }
            if(list.Count == 0)
            {
                height++;
            }
            if (depth < width)
            {
                depth = width;
            }
            return entities;
        }

合并必要的空单元格

        //合并单元格sheet, depth, height
        public void mergeCell(HSSFSheet sheet, int depth, int height)
        {
            //合并单元格, 起始行号,终止行号, 起始列号,终止列号
            if (depth >= 3)
            {
                for (int j = depth - 2; j >= 0; j--)
                {
                    if (j == 0)
                    {
                        sheet.AddMergedRegion(new CellRangeAddress(1, height, 0, 0));
                    }
                    else
                    {
                        int cell = 2 * j - 1;
                        for (int i = 1; i <= height; i++)
                        {
                            String id = sheet.GetRow(i).GetCell(cell).StringCellValue;
                            if (id != null && !"".Equals(id))
                            {
                                List<ft_topic> topics = db.ft_topic.Where(o => o.pid == id).Where(o => o.status == "A").ToList();
                                if (topics.Count() > 0)
                                {
                                    //得到merge范围
                                    List<int> range = mergeCellRange(sheet, id, cell + 2, height, topics.Count());
                                    range = getRealRanage(range);
                                    if (range[0] != range[1])
                                    {
                                        sheet.AddMergedRegion(new CellRangeAddress(range[0], range[1], cell, cell));
                                        String duplicateId = sheet.GetRow(range[0]).GetCell(cell).StringCellValue;
                                        for (int k = range[0] + 1; k <= range[1]; k++)
                                        {
                                            String tmpId = sheet.GetRow(k).GetCell(cell).StringCellValue;
                                            if (tmpId == null || "".Equals(tmpId))
                                            {
                                                sheet.GetRow(k).GetCell(cell).SetCellValue(duplicateId);
                                            }
                                        }
                                        sheet.AddMergedRegion(new CellRangeAddress(range[0], range[1], cell + 1, cell + 1));
                                    }

                                }
                            }
                        }

                    }

                }
            }
            else
            {
                sheet.AddMergedRegion(new CellRangeAddress(1, height, 0, 0));
            }
        }

得到合并范围

     //得到合并范围
        public List<int> mergeCellRange(HSSFSheet sheet, String pid, int cell, int height, int childnum)
        {
            List<int> res = new List<int>();
            List<String> ids = new List<string>();
            for (int i = 1; i <= height; i++)
            {
                String id = sheet.GetRow(i).GetCell(cell).StringCellValue;
                if (!ids.Contains(id))
                {
                    if (db.ft_topic.Where(o => o.pid == pid).Where(t => t.id == id).Where(t => t.status == "A").SingleOrDefault() != null)
                    {
                        ids.Add(id);
                        if (ids.Count() == 1)
                        {
                            res.Add(i);
                        }
                        if (ids.Count() == childnum)
                        {
                            res.Add(i);
                            //break;
                        }
                    }
                }
                else
                {
                    res.Add(i);
                }

            }
            return res;
        }

注意点需要知道上面得到的范围内容有多个,因此还需要在这多个值里面得到最大,最小的值,来合并。

posted @ 2017-06-26 19:23  飞鸿踏雪不留痕  阅读(2891)  评论(0编辑  收藏  举报