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 ...
随机推荐
- android开发(37) android使用android_serialport_api 操作串口,解决权限问题
最近有个项目,要使用android设备操作串口的 斑马GK888T打印机,使用打印机打印二维码. 硬件设备连接方式: 安卓设备 通过 串口RS232 连接 斑马打印机的串口 那么就要解决:使用安卓设备 ...
- Centos7.3防火墙配置
1.查看firewall服务状态 systemctl status firewalld 2.查看firewall的状态 firewall-cmd --state 3.开启.重启.关闭.firewall ...
- 微信小程序——引用阿里云字体
阿里图标官网:http://www.iconfont.cn 使用阿里云图标大致的方法就是:选中你的图标——保存至你的项目——下载你的图标项目——在项目中引用字体文件. 具体方法可以参考:引用阿里云矢量 ...
- windows 下安装 redis
Redis 是一个高性能的key-value数据库, 使用内存作为主存储,数据访问速度非常快,当然它也提供了两种机制支持数据持久化存储.比较遗憾的是,Redis项目不直接支持Windows,Windo ...
- Android学习之发送及接收广播
1.使用标准广播 1.1 定义广播接收器 public class MyBroadcastReceiver extends BroadcastReceiver { @Override public v ...
- savReaderWriter 模块的使用
作用: 由于python可以辅助数据分析和数据挖掘,读取文件, 而savReaderWriter模块就是为此而设计. 官网 :http://pythonhosted.org/savReaderWrit ...
- VIM下的普通模式的相关知识
什么为一次操作? 从进行插入模式开始,直到返回普通模式为止,在此期间的任何修改都视为一次操作: 使用 u 可以撤销最新的修改: 所以呢,控制好在插入模式的操作就可以控制好撤销命令的粒度: 另外,最 ...
- Keystone-all 命令
本文档介绍Icehouse版keystone-all命令 keystone-all命令在一个进程中同时启动服务和管理API,为openstack提供服务目录,授权和身份认证服务. 用法 $ keyst ...
- Castle.Windsor依赖注入的高级应用_Castle.Windsor.3.1.0
[转]Castle.Windsor依赖注入的高级应用_Castle.Windsor.3.1.0 1. 使用代码方式进行组件注册[依赖服务类] using System; using System.Co ...
- HTML5 Canvas 超炫酷烟花绽放动画教程
这是一个很酷的HTML5 Canvas动画,它将模拟的是我们现实生活中烟花绽放的动画特效,效果非常逼真,但是毕竟是电脑模拟,带女朋友看就算了,效果还是差了点,呵呵.这个HTML5 Canvas动画有一 ...