Aspose------导出Excel
代码:
/// <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的更多相关文章
- ASPose导出excel简单操作
string file = FileDialogHelper.SaveExcel(string.Format("{0}.xls", excelName)); if (!string ...
- Aspose 导出excel小demo
//转为pdf private void CelltoPDF(string cellPath, string pdfPath) { Workbo ...
- aspose导出excel文件
using Aspose.Cells; using System; using System.Collections.Generic; using System.Data; using System. ...
- webapi aspose导出excel表格
API 通过get请求,注意用到一个[FromUri]特性,使GET接收实体参数 /// <summary> /// 导出 /// </summary> /// <par ...
- 使用Aspose.Cell.dll导出Excel总结
这两天项目上用Aspose导出Excel来着.开始感觉挺简单的,但是实际操作起来还是挺复杂的,调试占的时间很长.主要是动态生成列.合并单元格.调样式占了很长时间,还是总结一下吧. 基础操作: //EX ...
- C#使用Aspose.Cells导出Excel简单实现
首先,需要添加引用Aspose.Cells.dll,官网下载地址:http://downloads.aspose.com/cells/net 将DataTable导出Xlsx格式的文件下载(网页输出) ...
- Aspose.Cells导出Excel(1)
利用Aspose.Cells导出excel 注意的问题 1.DataTable的处理 2.进行编码,便于中文名文件下载 3.别忘了Aspose.Cells.dll(可以自己在网上搜索) public ...
- NPOI、MyXls、Aspose.Cells 导入导出Excel(转)
Excel导入及导出问题产生: 从接触.net到现在一直在维护一个DataTable导s出到Excel的类,时不时还会维护一个导入类.以下是时不时就会出现的问题: 导出问题: 如果是asp.net,你 ...
- 使用aspose.cell导出excel需要注意什么?
1.如果导出的数据源是汇总出来的,最好方法是将数据源放到缓存里面,当基本数据源变化的时候,在改变数据2.使用模板导出EXCEL,这样很多样式可以在模板文件里面直接设置,例如:默认打开页签,让列头固定3 ...
- 【转】 (C#)利用Aspose.Cells组件导入导出excel文件
Aspose.Cells组件可以不依赖excel来导入导出excel文件: 导入: public static System.Data.DataTable ReadExcel(String strFi ...
随机推荐
- NHibernate 知识点整理
1.实体bool类型属性与数据库映射.如实体中有bool属性IsDelete.配置如下 <property column="IsDelete" name="IsDe ...
- c# 反射取其他项目的资源文件
反射获取其他项目里面的资源文件. dll或exe里面 try { System.Reflection.Assembly dll = System.Reflection.Assembly.LoadFil ...
- [转]Java动态代理
动态代理在Java中有着广泛的应用,比如Spring AOP,Hibernate数据查询.测试框架的后端mock.RPC,Java注解对象获取等.静态代理的代理关系在编译时就确定了,而动态代理的代理关 ...
- Django添加防跨站请求伪造中间件
第一步: 在全局设置中打开此中间件: MIDDLEWARE_CLASSES = [ ... 'django.middleware.csrf.CsrfViewMiddleware', ... ] ...
- 再谈git的http服务
因为git服务器搬迁,需要重新安装git服务器,在网上搜索了下,发现之前的方法太复杂,复杂到自己都没彻底弄明白.其实通过git自带的git-http-backend脚本配合apache2的http服务 ...
- R-table和tapply函数
table可统计数据的频数 tapply可根据因子.向量和要计算的函数计算 > class<-c(1,2,3,2,1,2,1,3) > class[1] 1 2 3 > c(8 ...
- Qt封装QTcpServer参考资料--QTcpServer多线程实现
目的:每个客户端连接的tcpSocket分别分配一个专门的线程来处理. 实现时分别继承QTcpServer和QTcpScoket实现出自己需要的类. 继承QTcpServer为每个客户端连接时分配线程 ...
- QTcpSocket 发送和接收数据的几种方法
1.QTcpSocket 继承于QAbstractSocket继承于QIODevice 2.QTcpSocket 提供的几种接收和发送数据方法 write ( const char *, qint64 ...
- USB2.0学习笔记连载(十九):EZ-USB TRM手册重要部分介绍
TRM手册中给出了所有的寄存器配置,在 slave fifo模式或者 GPIF模式等,所以对于用到的各种寄存器配置需要查看此手册,当然还可以配合着应用手册<AN61345>. ...
- java 模拟一个单向链表
class Node { //当前节点 private String data; //下个节点 private Node next; //当前节点 public void setData(String ...