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 ...
随机推荐
- Spark算子---实战应用
Spark算子实战应用 数据集 :http://grouplens.org/datasets/movielens/ MovieLens 1M Datase 相关数据文件 : users.dat --- ...
- Swift的函数与函数指针、闭包Closure等相关内容介绍
<span style="font-size:24px;">//函数 //demo1 无參数类型 func testConcat(){ println("測试 ...
- XML(五)dom4j增删改查
book2.xml <? xml version="1.0" encoding="UTF-8"?> <书架> <书> < ...
- TF-IDF词项权重计算
一.TF-IDF 词项频率: df:term frequency. term在文档中出现的频率.tf越大,词项越重要. 文档频率: tf:document frequecy.有多少文档包括此term, ...
- sql server 2012 删除服务器名称
SQL Server 2008 R2及以前版本: http://blog.csdn.net/downmoon/article/details/5678468 SQL Server 2012: 删除这两 ...
- js json处理 双引号
在数据传输流程中,json是以文本,即字符串的形式传递的,而JS操作的是JSON对象 JSON字符串: var str1 = '{ "name": "cxh", ...
- Navi.Soft31.WebMVC框架(含示例地址)
1概述 1.1应用场景 互联网高速发展,互联网软件也随之越来越多,Web程序越来越被广泛使用.它部署简单,维护方便,深得众多软件公司使用 Bootstrap前端框架,是最近非常流行的框架之一.它简洁, ...
- Qt之QLocalServer
简述 QLocalServer提供了一个基于本地socket的server. QLocalServer可以接受来自本地socket的连接.通过调用listen(),让server监听来自特定key的连 ...
- Selenium常用操作汇总二——如何处理alert、confirm、prompt对话框
alert.confirm.prompt这样的js对话框在selenium1.X时代也是难啃的骨头,常常要用autoit来帮助处理. 试用了一下selenium webdriver中处理这些对话框十分 ...
- CI框架 -- 开发环境、生产环境
开发者常常希望当系统运行在开发环境或生产环境中时能有不同的行为, 例如,在开发环境如果程序能输出详细的错误信息将非常有用,但是在 生产环境这将造成一些安全问题. ENVIRONMENT 常量 Code ...