代码:

/// <summary>
/// 导出Excel
/// </summary>
/// <typeparam name="T">泛型类</typeparam>
/// <param name="list">数据列</param>
/// <param name="row">插入行索引</param>
/// <param name="column">插入列索引</param>
public static void ExportToExcel<T>(List<T> list,int row, int column)
{
  //模板地址
  var template = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Temp", "A_Login.xlsx");   Workbook book = new Workbook(template);
  Worksheet sheet = book.Worksheets[];
  //设置筛选
  sheet.AutoFilter.SetRange(, , );   Cells cell = sheet.Cells;   for (var i = ; i < list.Count; i++)
  {
    cell.InsertRows(row + ,);
  }   int rowIndex = ;
  int colIndex = column;   Type type = typeof(T);
  PropertyInfo []propertyInfo = type.GetProperties();
  foreach (var pi in propertyInfo)
  {
    rowIndex = row;
    foreach (var li in list)
    {
      var value = pi.GetValue(li);
      cell[rowIndex, colIndex].PutValue(value);
      rowIndex++;
    }
    colIndex++;
  }   var path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Temp", "成品.xlsx");
  book.Save(path);
}
/// <summary>
/// 导出合并单元格的Excel
/// </summary>
/// <typeparam name="T">泛型类</typeparam>
/// <param name="list">需要合并的数据</param>
public void ExportToMergeExcel<T>(List<T> list)
{
Workbook book = new Workbook();
Worksheet sheet = book.Worksheets[];
Cells cell = sheet.Cells;
Type type = typeof(T); int rowIndex = ;
int colIndex = ;
PropertyInfo[] property = type.GetProperties();
//合并的第一个单元格数据
var name = "";
//是否取出第一个单元格数据
var isFirst = true;
//记录需要合并的行数
var num = ;
List<int> mergeIndex = new List<int>();
List<int> mergeCount = new List<int>(); foreach (var pi in property)
{
rowIndex = ;
cell[rowIndex, colIndex].PutValue(pi.Name); foreach (var li in list)
{
++rowIndex;
var value = pi.GetValue(li);
cell[rowIndex, colIndex].PutValue(value); if (pi.Name == "Component")
{
if (isFirst == true)
{
isFirst = false;
continue;
} var v = value.ToString();
if (v == name)
{
num++;
if (rowIndex == list.Count())
{
if (num > )
{
mergeIndex.Add(rowIndex - num + );
mergeCount.Add(num);
}
}
}
else
{
name = v;
mergeIndex.Add(rowIndex - num);
mergeCount.Add(num);
num = ;
}
}
if (pi.Name == "SubComponent")
{
if (value == null)
{
cell.Merge(rowIndex, , , );
}
}
}
colIndex++;
} //合并
for (var i = ; i < mergeIndex.Count(); i++)
{
var f = mergeIndex[i];
var r = mergeCount[i];
cell.Merge(f, , r, );
} SetCellsStyle(cell); //设置筛选行
sheet.AutoFilter.SetRange(, , ); //自动行列宽
sheet.AutoFitColumns();
sheet.AutoFitRows(); var path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Temp", "成品nck.xlsx");
book.Save(path);
} /// <summary>
/// 设置整行/整列样式
/// </summary>
/// <param name="cells"></param>
private void SetCellsStyle(Cells cells)
{
//设置样式
Style style1 = new Style();
//style.Font.Size = 12;
StyleFlag flag1 = new StyleFlag();
flag1.VerticalAlignment = true;
flag1.Font = true;
cells.ApplyColumnStyle(, style1, flag1);
//cell.Columns[0].ApplyStyle(style1, flag1); Style style2 = new Style();
style2.Font.Size = ;
style2.Pattern = BackgroundType.Solid;
style2.ForegroundColor = Color.FromArgb(, , , );
style2.Font.IsBold = true;
style2.HorizontalAlignment = TextAlignmentType.Left;
StyleFlag flag2 = new StyleFlag();
flag2.Font = true;
flag2.All = true;
//flag2.HorizontalAlignment = true;
cells.ApplyRowStyle(, style2, flag2);
} private void SetCellStyle(Cell cell, int fontSize, bool isBold = false, bool isItalic = false, List<int> argb = null)
{
Style style = new Style();
style.Font.Size = fontSize;
style.Font.IsBold = isBold;
style.Font.IsItalic = isItalic;
style.HorizontalAlignment = TextAlignmentType.Left;
style.Pattern = BackgroundType.Solid;
style.ForegroundColor = Color.FromArgb(argb[], argb[], argb[], argb[]);
cell.SetStyle(style, true);
}

需要合并的条目

private List<MergeItem> MergeEfficientItem(List<ProjectItem> list)
{
PropertyInfo[] propertyInfo = typeof(MergeItem).GetProperties();
//var data = _ProjectItemRepository.AsQueryable().where(p => p.ProjectCode == projectCode);
var data = list;
var level1List = data.Where(p => p.ItemLevel == "Level1").ToList();
var level2List = data.Where(p => p.ItemLevel == "Level2").ToList();
var level3List = data.Where(p => p.ItemLevel == "Level3").ToList();
var datalist = new List<MergeItem>(); //一级条目
foreach (var li in level1List)
{
var pitem = new MergeItem();
pitem.Component = li.Component;
datalist.Add(pitem); //三级条目
var citem = level3List.Where(p => p.Component == li.Component).OrderBy(p => p.Seq);
var subitem = new List<MergeItem>();
foreach (var cli in citem)
{
var sitem = new MergeItem();
sitem.Component = cli.SubComponent;
sitem.SubComponent = cli.Description;
sitem.Unit = cli.Unit;
sitem.Remark = cli.Remark;
sitem.FillInstruct = cli.FillInstruct; if (subitem.Any(p => p.Component == cli.SubComponent && p.SubComponent == cli.Description))
{
sitem = subitem.Where(p => p.Component == cli.SubComponent && p.SubComponent == cli.Description).FirstOrDefault();
}
else
{
subitem.Add(sitem);
} foreach (var pi in propertyInfo)
{
foreach (var c in citem)
{
var trim = c.Scenario.Replace(" ", "");
if (pi.Name == trim && c.Description == sitem.SubComponent)
{
pi.SetValue(sitem, c.UnitCost);
}
}
}
}
datalist.AddRange(subitem);
}
_ExcelUtil.ExportToMergeExcel<MergeItem>(datalist); return datalist;
}

Aspose------导出Excel的更多相关文章

  1. ASPose导出excel简单操作

    string file = FileDialogHelper.SaveExcel(string.Format("{0}.xls", excelName)); if (!string ...

  2. Aspose 导出excel小demo

    //转为pdf         private void CelltoPDF(string cellPath, string pdfPath)         {             Workbo ...

  3. aspose导出excel文件

    using Aspose.Cells; using System; using System.Collections.Generic; using System.Data; using System. ...

  4. webapi aspose导出excel表格

    API 通过get请求,注意用到一个[FromUri]特性,使GET接收实体参数 /// <summary> /// 导出 /// </summary> /// <par ...

  5. 使用Aspose.Cell.dll导出Excel总结

    这两天项目上用Aspose导出Excel来着.开始感觉挺简单的,但是实际操作起来还是挺复杂的,调试占的时间很长.主要是动态生成列.合并单元格.调样式占了很长时间,还是总结一下吧. 基础操作: //EX ...

  6. C#使用Aspose.Cells导出Excel简单实现

    首先,需要添加引用Aspose.Cells.dll,官网下载地址:http://downloads.aspose.com/cells/net 将DataTable导出Xlsx格式的文件下载(网页输出) ...

  7. Aspose.Cells导出Excel(1)

    利用Aspose.Cells导出excel 注意的问题 1.DataTable的处理 2.进行编码,便于中文名文件下载 3.别忘了Aspose.Cells.dll(可以自己在网上搜索) public ...

  8. NPOI、MyXls、Aspose.Cells 导入导出Excel(转)

    Excel导入及导出问题产生: 从接触.net到现在一直在维护一个DataTable导s出到Excel的类,时不时还会维护一个导入类.以下是时不时就会出现的问题: 导出问题: 如果是asp.net,你 ...

  9. 使用aspose.cell导出excel需要注意什么?

    1.如果导出的数据源是汇总出来的,最好方法是将数据源放到缓存里面,当基本数据源变化的时候,在改变数据2.使用模板导出EXCEL,这样很多样式可以在模板文件里面直接设置,例如:默认打开页签,让列头固定3 ...

  10. 【转】 (C#)利用Aspose.Cells组件导入导出excel文件

    Aspose.Cells组件可以不依赖excel来导入导出excel文件: 导入: public static System.Data.DataTable ReadExcel(String strFi ...

随机推荐

  1. [转]MS SQL Server 数据库连接字符串详解

    http://blog.csdn.net/jackiehome/article/details/8668121 问题 : 超时时间已到.在从池中获取连接之前超时时间已过.出现这种情况可能是因为所有池连 ...

  2. 【嵌入式】——arm裸机开发 step by step 之 按键控制 LED 和 蜂鸣器

    一.arm-9 TQ2440 key.h #ifndef __KEY_H__ #define __KEY_H__ #define GPFCON (*(volatile unsigned long *) ...

  3. iptable 限制ip访问

    -- 查找所有规则iptables -L INPUT --line-numbers -- 删除一条规则iptables -D INPUT 11 (注意,这个11是行号,是iptables -L INP ...

  4. QTcpSocket使用过程中的一些问题记录

    目前,在将原来C的socket通讯改为使用Qt类库QTcpSocket通讯,在修改过程中遇到不少问题,在此将问题一并记录,以备后面使用. 采用的通讯方式:QTimer定时器.QThread多线程和QT ...

  5. C++中,int a = 10的后面的操作

    在C++中,int a = 10的内存表现形式取决于你的具体代码和优化级别,主要的几种形式: 不存在于内存中.比如a从未改变,被编译器当成常量,所有代码中的a直接替换成10: 存在于寄存器中:比如对a ...

  6. Policy Gradient

    Policy Gradient是区别于Q-Learning为代表的value based的方法.policy gradient又可以叫reinforce算法(Williams, 1992). 如今的A ...

  7. C++ STL Maps

    Maps定义 --> 个人理解为python的字典 C++ Maps are sorted associative containers the contian unique key/value ...

  8. 大型网站的SEO引爆点

    网站越大,SEO服务做起来就轻松,因为大型网站都有很好的执行团队,你只需要找准他们网站的SEO爆破点,就能够迅速获得非常理想的SEO效果.本文将结合我最近两年的几个经典案例:腾讯拍拍.金山爱词霸.中青 ...

  9. selenium+java+chrome环境搭建

    我只能说因为版本冲突,简直太折腾了,而搜了无数个博友的帖子才找到正确条案,就不能好好的写篇文章吗? 最近真的是太闲太闲了,平时没事总得搞点技术,不然心里感觉好空虚, 最近看上了selenium,所以试 ...

  10. Android 耳机插入过程分析

    Android 耳机插入过程分析 参考链接: https://www.jianshu.com/p/d82a8dabb3e7 初始化: 10-26 07:40:43.932 1414 1414 I Sy ...