public async Task<MemoryStream> ExportExcel(IList<fuquestionbank> _list, string pId, string pfid, string fugid)
{
#region 绘制表头
string[] arr = { "序号", "姓名", "性别", "年龄", "联系电话", "随访医生", "最近一次随访", "计划次数", "共随访次数", "病历数", "咨询数", "下次随访时间", "状态" };
HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
//添加一个sheet1
ISheet sheet1 = book.CreateSheet("Sheet1");
CellRangeAddress m_region = new CellRangeAddress(, , , arr.Length - ); //合并0列的n--n+2行
sheet1.AddMergedRegion(m_region);
IRow row = sheet1.CreateRow();
ICell cell = row.CreateCell();
ICellStyle cellstyle = book.CreateCellStyle();//设置垂直居中格式
cellstyle.VerticalAlignment = VerticalAlignment.CENTER;//垂直居中
cellstyle.Alignment = HorizontalAlignment.CENTER;//水平居中 cell.CellStyle = cellstyle;
cell.SetCellValue("基本信息");
IRow row2 = sheet1.CreateRow();
for (int i = ; i < arr.Length; i++)
{
row2.CreateCell(i).SetCellValue(arr[i]);
} var title = _list.GroupBy(x => x.FllowPlan_Name);
List<string> planName = new List<string>();//随访计划名称
List<string> fllowName = new List<string>();//问卷名称
Dictionary<string, string> timu = new Dictionary<string, string>();//当前问卷下的问卷题目
List<string> timuList = new List<string>();
Dictionary<string, int> timuResult = new Dictionary<string, int>();//当前问卷下的题目的个数
//获取随访计划和问卷信息
foreach (var item in title)
{
planName.Add(item.Key.ToString());
foreach (var name in item)
{
if (!fllowName.Contains(name.FollowInfo_Name))
{
fllowName.Add(name.FollowInfo_Name);
}
if (!timu.ContainsKey(name.Question_Name))
{
timu.Add(name.Question_Name, name.FollowInfo_Name);
}
}
}
//获取当前问卷下的题目的个数
var tGroup = timu.GroupBy(x => x.Value);
foreach (var item in tGroup)
{
foreach (var count in item)
{
if (!timuResult.ContainsKey(count.Value))
{
timuResult.Add(count.Value, item.Count());
}
}
}
//获取题目集合
foreach (KeyValuePair<string, string> item in timu)
{
timuList.Add(item.Key);
}
//绘制随访计划表头
//IRow row3 = sheet1.CreateRow(0); int rowOne = arr.Length;
for (int i = ; i < planName.Count; i++)
{
sheet1.AddMergedRegion(new CellRangeAddress(, , rowOne, rowOne + timu.Count - ));
ICell cellPlan = row.CreateCell(rowOne);
cellPlan.CellStyle = cellstyle;
cellPlan.SetCellValue(planName[i]);
// row.CreateCell(rowOne).SetCellValue(planName[i]);
rowOne += timu.Count;
}
//绘制随访问卷表头
IRow row4 = sheet1.CreateRow();
int rowTwo = arr.Length;
int index = ;
for (int i = ; i < planName.Count * fllowName.Count; i++)
{ //获取当前问卷下的题目个数
int r = timuResult[fllowName[index]];
sheet1.AddMergedRegion(new CellRangeAddress(, , rowTwo, rowTwo + r - ));
ICell cellFllow = row4.CreateCell(rowTwo);
cellFllow.CellStyle = cellstyle;
cellFllow.SetCellValue(fllowName[index]);
//row4.CreateCell(rowTwo).SetCellValue(fllowName[index]);
rowTwo += r;
index += ;
if (index > fllowName.Count - )
{
index = ;
}
}
//绘制问卷题目表头
//IRow row5 = sheet1.CreateRow(2);
int index1 = ;
for (int i = arr.Length; i < (timuList.Count * planName.Count) + arr.Length; i++)
{
ICell cellTimu = row2.CreateCell(i);
cellTimu.CellStyle = cellstyle;
cellTimu.SetCellValue(timuList[index1].ToString()); // row2.CreateCell(i).SetCellValue(timuList[index1].ToString());
index1 += ;
if (index1 >= timuList.Count - )
{
index1 = ;
}
}
 
            }
// 写入到客户端
System.IO.MemoryStream ms = new System.IO.MemoryStream();
book.Write(ms);
ms.Seek(, SeekOrigin.Begin);
return ms;
}
            //表头数据
var tablleTitle = await patientBLL.getFllowInfoList(fugid);
IList<fuquestionbank> tableTil = tablleTitle.OrderBy(x => x.FllowPlan_id).ThenBy(x => x.FollowInfo_Name).ToList();
//导出
excelBll excelBll = new BLL.excelBll();
MemoryStream ms = await excelBll.ExportExcel(tableTil, pId, pfid, fugid);
string SavaName = DateTime.Now.ToString("yyyyMMddhhmmss");
return File(ms, "application/vnd.ms-excel", "患者管理" + SavaName + ".xls");
//list集合转datatable
public DataTable IListOut(IList<excelModel> _list)
{
DataTable dtReturn = new DataTable();
PropertyInfo[] oProps = null;
foreach (excelModel rec in _list)
{
if (oProps == null)
{
oProps = ((Type)rec.GetType()).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType; if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[];
}
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}
DataRow dr = dtReturn.NewRow(); foreach (PropertyInfo pi in oProps)
{
dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null);
}
dtReturn.Rows.Add(dr);
}
return (dtReturn);
}

NPOI的操作的更多相关文章

  1. C# 之 用NPOI类库操作Excel

    1.需引用以下命名空间: using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.HPSF; using NPOI.HSSF.Ut ...

  2. asp.net(C#)之NPOI&quot;操作Excel

    1.首先到网上下载"NPOI.DLL".引用. 2.新建一个操作类"ExcelHelper.cs": using System.Collections.Gene ...

  3. NPOI:操作总结

    1.套路 使用了NPOI一段时间,也慢慢了解了操作的流程,或者说套路: a.创建Workbook: HSSFWorkbook Workbook = new HSSFWorkbook(); b.在Wor ...

  4. NPOI:初次操作(新建Excel)

    1. 由于在某些电脑上没有安装office,或者有权限限制,使用COM组件进行读写Excel的话会出现问题, 为此,NPOI是一个很好的选择,NPOI可以在上述环境中满足Office的操作需求,并且功 ...

  5. c# NPOI文件操作

    public static Byte[] RenderDataToExcel<T>(List<T> SourceList, List<String> filter) ...

  6. C#开发之基于NPOI的操作Excel开发体验

    最近遇到一个数据导入的需求,语言是.net framework 4.7的C#.但是,这次主要探讨NPOI的体验,原则就是向前兼容.所以采用.xls的支持.网上的资料,我稍微整合了一些. #1 单元格下 ...

  7. NPOI简单操作excel

    本文仅当是个记录文件,仅供初学者参考. 首先得using几个npoi的空间名如下: using NPOI.HSSF.UserModel;using NPOI.HSSF.Util;using NPOI. ...

  8. NPOI读取操作excel

    .读取using (FileStream stream = new FileStream(@"c:\客户资料.xls", FileMode.Open, FileAccess.Rea ...

  9. NPOI office操作

    写excel FileStream file = new FileStream(@"test.xls",FileMode.Create); hssfworkbook.write(f ...

随机推荐

  1. 物联网框架ServerSuperIO(SSIO)更新、以及增加宿主程序和配置工具,详细介绍

    一.更新内容 1.修改*Server类,以及承继关系.2.增加IRunDevice的IServerProvider接口继承.3.修复增加COM设备驱动可能造成的异常.4.修复网络发送数据可能引发的异常 ...

  2. 面向对象设计模式纵横谈:Singelton单件模式(笔记记录)

       李建忠老师讲的<面向对象设计模式纵横谈>,早就看过了,现在有了时间重新整理一下,以前的博客[赛迪网]没有了,现在搬到博客园,重新过一遍,也便于以后浏览. 设计模式从不同的角度分类会得 ...

  3. SpringMVC类型转换器、属性编辑器

    对于MVC框架,参数绑定一直觉得是很神奇很方便的一个东西,在参数绑定的过程中利用了属性编辑器.类型转换器 参数绑定流程 参数绑定:把请求中的数据,转化成指定类型的对象,交给处理请求的方法 请求进入到D ...

  4. H5 本地存储一

    localStorage(本地存储),可以长期存储数据,没有时间限制,一天,一年,两年甚至更长,数据都可以使用.sessionStorage(会话存储),只有在浏览器被关闭之前使用,创建另一个页面时同 ...

  5. Listview的Item中有CheckBox、Button等的焦点处理

    ListView的item布局中有CheckBox.Button等会获取焦点的控件会抢走焦点,造成ListView的item点击事件相应不了. 解决方法:控件设置 android:clickable= ...

  6. javaScript基础语法(上)

    相关理论概念: 直接量的概念:直接描述某个(些)存储空间的值的量,如变量的值.对象的值.数组的值. 数据类型:在数据结构中的定义是一个值的集合以及定义在这个值集上的一组操作. 1.变量的声明和使用 变 ...

  7. UDAD 用户故事驱动的敏捷开发 – 演讲实录

    敏捷发展到今天已经在软件行业得到了广泛认可,但大多数敏捷方法都是为了解决某一特定问题而总结出来的特定方法或实践,一直缺乏一个可以将整个开发过程串接起来的成体系的方法.用户故事驱动的敏捷开发(User ...

  8. GitLab CI持续集成配置方案(补)

    上篇文章介绍了GitLab CI的持续集成配置方法,本篇文章将主要介绍NUnit的持续集成和遇到的一些坑 1.NUnit单元测试持续集成 下载NUnit.3.4.1.msi,https://githu ...

  9. Java 条形码 二维码 的生成与解析

    Barcode简介 Barcode是由一组按一定编码规则排列的条,空符号,用以表示一定的字符,数字及符号组成的,一种机器可读的数据表示方式. Barcode的形式多种多样,按照它们的外观分类: Lin ...

  10. Perforce 与Source Insight, Visual Studio集成

    转自:http://shashanzhao.com/archives/837.html 1.Perforce 首先需要为perforce设置系统环境变量,以便perforce命令行可以正常使用. 环境 ...