树结构关系的数据导出为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;
}
注意点需要知道上面得到的范围内容有多个,因此还需要在这多个值里面得到最大,最小的值,来合并。
树结构关系的数据导出为excel的更多相关文章
- 学习笔记 DataGridView数据导出为Excel
DataGridView数据导出为Excel 怎样把WinForm下的“DGV”里的绑定数据库后的数据导出到Excel中. 比如:在窗体里有个一“DGV”,DataGridView1,绑定了数据源 ...
- 将C1Chart数据导出到Excel
大多数情况下,当我们说将图表导出到Excel时,意思是将Chart当成图片导出到Excel中.如果是这样,你可以参考帮助文档中保存和导出C1Chart章节. 不过,也有另一种情况,当你想把图表中的数据 ...
- vb.net-三种将datagridview数据导出为excel文件的函数
第一种方法较慢,但是数据格式都比较好,需要引用excel的 Microsoft.Office.Interop.Excel.dll office.dll #Region "导出excel函数 ...
- 数据导出至Excel文件--好库编程网http://code1.okbase.net/codefile/SerializeHelper.cs_2012122018724_118.htm
using System; using System.IO; using System.Data; using System.Collections; using System.Data.OleDb; ...
- 数据导出到Excel中
自己修改后的一个数据导出到Excel的方法,粘出来与大家共享. 只需要将 System.Web.HttpContext.Current.Response.Charset = ...
- asp.net将数据导出到excel
本次应用datatable导出,若用gridview(假设gridview设为了分页显示)会出现只导出当前页的情况. protected void btnPrn_Click(object sender ...
- 将datagrid中数据导出到excel中 -------<<工作日志2014-6-6>>
前台datagrid数据绑定 #region 导出到excel中 /// <summary> /// 2014-6-6 /// </summary> / ...
- 机房收费系统——在VB中将MSHFlexGrid控件中的数据导出到Excel
机房收费系统中,好多查询的窗体都包含同一个功能:将数据库中查询到的数据显示在MSHFlexGrid控件中,然后再把MSHFlexGrid控件中的数据导出到Excel表格中. 虽然之前做过学生信息管理系 ...
- Qt中将QTableView中的数据导出为Excel文件
如果你在做一个报表类的程序,可能将内容导出为Excel文件是一项必须的功能.之前使用MFC的时候我就写过一个类,用于将grid中的数据导出为Excel文件.在使用了QtSql模块后,我很容易的将这个类 ...
随机推荐
- (三)类数组对象 NamedNodeMap简单介绍
Ele.attrbutes将返回一个NamedNodeMap对象,即NamedNodeMap存储的是元素的“特性Attribute”集合.而集合中的每一个元素,都是Attr类型的对象. html: & ...
- idea中 maven打包时时报错User setting file does not exist C:\Users\lenevo\.m2\setting.xml,
第一种错误 :idea中 maven打包时时报错User setting file does not exist C:\Users\lenevo\.m2\setting.xml, 解决方案如下:将ma ...
- 三、checkedListBoxControl
一.checkedListBoxControl的使用全选 private void InitDate() { CheckedListBoxItem[] itemArr = { new CheckedL ...
- css居中小技巧
一.行内元素-水平居中 在父元素的样式中添加: text-align:center; 二.定宽块级元素-水平居中 所谓定宽块级元素指块级元素的宽度指定,而不是默认的100%,否则此方法无效: 代码: ...
- faster rcnn训练详解
http://blog.csdn.net/zy1034092330/article/details/62044941 py-faster-rcnn训练自己的数据:流程很详细并附代码 https://h ...
- codevs3044
codevs3044 题面 大意:给出n个矩形求覆盖的总面积 看了hzwer的blog 似懂非懂 链接 可能还要多练点吧qaq #include <bits/stdc++.h> using ...
- bzoj2762-[JLOI2011]不等式组
求 \(x=k\) 时满足一元一次不等式 \(ax+b<c\) 的个数. 解出 \(\frac{c-b}{a}\) 之后取整,得到合法区间,用树状数组维护. 注意 \(a\) 的值域是 \([- ...
- SpringBoot2.0.3 + SpringSecurity5.0.6 + vue 前后端分离认证授权
新项目引入安全控制 项目中新近添加了Spring Security安全组件,前期没怎么用过,加之新版本少有参考,踩坑四天,终完成初步解决方案.其实很简单,Spring Security5相比之前版本少 ...
- BZOJ4514[Sdoi2016]数字配对——最大费用最大流
题目描述 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数, 那么这两个数字可以配对,并获得 ci ...
- 【XSY1538】连在一起的幻想乡 数学 无向连通图计数
题目大意 给你\(n,p\),求\(n\)个点组成的所有无向连通图的边数的平方和模\(p\) \(n\leq 2000,p\leq {10}^9\) 题解 设\(m=\frac{n(n-1 ...