EciReport模板导出_Core
# 功能介绍
基于模板导出Excel的功能
1
# 功能使用
有两个类一个是EciReport2003,另一个是EciReport2007。
区别:
EciReport2003 的模板是 xxx.xls的
EciReport2007 的模板是 xxx.xlsx
两个类的属性都是一样的,使用方法也是一样的,主要是根据模板来选择。
1
2
3
4
5
2
3
4
5
# EciReport2003类的属性
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
ForceFormulaRecalculation | 重新计算公式 | bool | false |
Name | 报表名称 | string | |
Type | 报表类型 | string | |
TemplateFile | 模板文件 | string | |
DataSource | 数据源 | DataSet | |
ReportParam | 参数 | EntityBase | |
ListDetail | 明细处理配置 | List:EciReportDetail | |
TargetFileName | 目标文件名 | string | |
multiSheet | 是否是多sheet | bool |
# EciReport2003类的方法
事件名称 | 说明 | 参数 |
---|---|---|
EciReport2003 | 构造函数 | |
AddDataTable | 追加数据表 | DataTable |
AddDataTable | 追加数据表 | DataTable,tableName |
Execute | 执行导出第一页Sheet【EXCEL报表】 | |
ExecuteSheet | 导出指定索引的Sheet | sheet索引:inputSheetIndex,table名称:headTableName |
PrintLandscape | 设置水平打印 | landscape(true:横向打印,false:纵向打印),pagerSize |
ToExcel | 导出Excel | |
Save | 保存到本地目录 | 文件名称:fileName |
# EciReportDetail 类的属性
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
Name | 数据源的名称 | string | |
StartRowIndex | 起始行,注意:第一行的索引是1,不是0 | int | |
RowIndex | 模板行 | int | |
TableIndex | word导出模板专用 | int | 0 |
# 主从 完成
主表字段使用的 $ 开始 ,后面拼接表格列的字段名称
明细字段使用:# 开始 ,后面拼接表格列的字段名称
1
2
2
# 示例
# 模板示例
![[core_01_模板.png]]
# 代码示例1
EciReport2003 npoi = new EciReport2003();
DataSet dataset = new DataSet();
//准备主表信息
DataTable head = new DataTable();
head.TableName = "HEAD";
head.Columns.Add("username");
head.Columns.Add("testNo");
DataRow row = head.NewRow();
row["username"] = "测试";
row["testNo"] = "11";
head.Rows.Add(row);
dataset.Merge(head);
//模板
npoi.TemplateFile = EciCore.Runtime.ServerPath + @"\Template\岗位信息.xls";
npoi.TargetFileName = "岗位信息.xls";//可以不加
npoi.DataSource = dataset;
//设置明细
npoi.ListDetail.Add(new EciReportDetail() { StartRowIndex = 5, Name = "DATA" });
npoi.AddDataTable(dt, "DATA");
npoi.Execute();
//下载excel
return npoi.ToExcel(context);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 导出结果
![[core_01_结果.png]]
# 导出主从(分组) 完成
主表字段使用的 $ 开始 ,后面拼接表格列的字段名称
明细字段使用:# 开始 ,后面拼接表格列的字段名称
在明细表中,通过K列,来进行分组
1
2
3
2
3
# 示例
# 模板示例
在表头,明细表中,添加K列,设置相同的值,代表是一组的 ![[core_02_模板.png]]
# 代码示例
EciReport2003 npoi = new EciReport2003();
//准备主表信息
DataTable head = new DataTable();
head.Columns.Add("username");
head.Columns.Add("K");
head.Columns.Add("SHEET_NAME");
DataRow row = head.NewRow();
row["username"] = "用户1";
row["SHEET_NAME"] = "表单1";
row["K"] = "K1";
head.Rows.Add(row);
DataRow row2 = head.NewRow();
row2["username"] = "用户2";
row2["SHEET_NAME"] = "表单2";
row2["K"] = "K2";
head.Rows.Add(row2);
npoi.AddDataTable(head, "HEAD");
//模板
npoi.TemplateFile = EciCore.Runtime.ServerPath + @"\Template\岗位信息.xls";
npoi.TargetFileName = "岗位信息.xls";//可以不加
//设置明细
npoi.ListDetail.Add(new EciReportDetail() { StartRowIndex = 5, Name = "DATA" });
dt.Columns.Add("K");
dt.Rows[0]["K"] = "K1";
dt.Rows[1]["K"] = "K1";
dt.Rows[2]["K"] = "K2";
dt.Rows[3]["K"] = "K2";
npoi.AddDataTable(dt, "DATA");
npoi.Execute();
//下载excel
return npoi.ToExcel(context);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 导出结果
分不同的sheet显示结果 ![[core_02_结果1.png]] ![[core_02_结果2.png]]
# 仅导出明细
主表字段使用的 $ 开始 ,后面拼接表格列的字段名称
明细字段使用:# 开始 ,后面拼接表格列的字段名称
1
2
2
# 示例
# 模板示例
![[core_03_模板.png]]
# 代码示例
EciReport2003 npoi = new EciReport2003();
//模板
npoi.TemplateFile = EciCore.Runtime.ServerPath + @"\Template\岗位信息.xls";
npoi.TargetFileName = "岗位信息.xls";
//设置明细
npoi.ListDetail.Add(new EciReportDetail() { StartRowIndex = 5, Name = "DATA" });
npoi.AddDataTable(dt, "DATA");
npoi.Execute();
//下载excel
return npoi.ToExcel(context);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 导出结果
![[core_03_结果.png]]
# Excel导出到Table添加超链接
主表字段使用的 $ 开始 ,后面拼接表格列的字段名称
明细字段使用:# 开始 ,后面拼接表格列的字段名称
添加超链接:添加列:columnName_LINK,
1
2
3
2
3
# 示例
# 模板示例
在代码中,给明细表的字段添加超链接: 给字段NAME添加,就是在表格中添加列NAME_LINK,设置url地址。 ![[core_04_模板.png]]
# 代码示例
EciReport2003 npoi = new EciReport2003();
//模板
npoi.TemplateFile = EciCore.Runtime.ServerPath + @"\Template\岗位信息.xls";
npoi.TargetFileName = "岗位信息.xls";
//设置明细
npoi.ListDetail.Add(new EciReportDetail() { StartRowIndex = 5, Name = "DATA" });
dt.Columns.Add("name_LINK");
foreach (DataRow row in dt.Rows)
{
row["name_LINK"] = "http://baidu.com";//测试超链接
}
npoi.AddDataTable(dt, "DATA");
npoi.Execute();
//下载excel
return npoi.ToExcel(context);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 导出结果
![[core_04_结果.png]]
# 导出设置偏移量
主表字段使用的 $ 开始 ,后面拼接表格列的字段名称
明细字段使用:# 开始 ,后面拼接表格列的字段名称
在模板中设置 $字段 % 3:向左偏移3个单元格;
1
2
3
2
3
# 示例
# 模板示例
向左偏移2个单元格:从F列偏移到D列; ![[core_05_模板.png]]
# 代码示例
EciReport2003 npoi = new EciReport2003();
//准备主表信息
DataTable head = new DataTable();
head.TableName = "HEAD";
head.Columns.Add("username");
DataRow row3 = head.NewRow();
row3["username"] = "测试";
head.Rows.Add(row3);
npoi.AddDataTable(head, "HEAD");
//模板
npoi.TemplateFile = EciCore.Runtime.ServerPath + @"\Template\岗位信息.xls";
npoi.TargetFileName = "岗位信息.xls";//可以不加
//设置明细
npoi.ListDetail.Add(new EciReportDetail() { StartRowIndex = 5, Name = "DATA" });
npoi.AddDataTable(dt, "DATA");
npoi.Execute();
//下载excel
return npoi.ToExcel(context);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 导出结果
![[core_05_结果.png]]
# 多模板,多sheet导出
主表字段使用的 $ 开始 ,后面拼接表格列的字段名称
明细字段使用:# 开始 ,后面拼接表格列的字段名称
要执行多次:ExecuteSheet,指定sheet执行导出
1
2
3
2
3
# 示例
# 模板示例
![[core_06_模板1.png]] ![[core_06_模板2.png]]
# 代码示例
EciReport2003 npoi = new EciReport2003();
//准备主表信息
DataTable head = new DataTable();
head.TableName = "HEAD";
head.Columns.Add("username");
DataRow row3 = head.NewRow();
row3["username"] = "测试";
head.Rows.Add(row3);
npoi.AddDataTable(head, "HEAD");
DataTable dataA = dt;
dataA.TableName = "A";
DataTable dataB = new DataTable();
dataB.TableName = "B";
dataB.Columns.Add("name");
DataRow row = dataB.NewRow();
row["name"] = "nameB";
dataB.Rows.Add(row);
npoi.AddDataTable(dataB, "B");
npoi.AddDataTable(dataA, "DATA");
//模板
npoi.TemplateFile = EciCore.Runtime.ServerPath + @"\Template\岗位信息.xls";
npoi.TargetFileName = "岗位信息.xls";
//设置明细
npoi.ListDetail.Add(new EciReportDetail() { StartRowIndex = 5, Name = "DATA" });
npoi.Execute();
npoi.ListDetail = new List<EciReportDetail>() { new EciReportDetail() { Name = "B", StartRowIndex = 2 } };
npoi.ExecuteSheet(1, string.Empty);
//下载excel
return npoi.ToExcel(context);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 导出结果
![[core_06_结果1.png]] ![[core_06_结果2.png]]