上周六我发表的文章《分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility》受到了大家的热烈支持与推荐,再此表示感谢,该ExcelUtility类库自上次发文起,又经过了多次的改进,增加了许多的功能与方法,可以全面满足大家的需求,下面先来看一下新方法的测试结果:
第一个新增功能,列宽自适应,当超过30个字符则将单元格内容设为换行
任意一个无模板的导出方法均支持该功能,示例代码如下:
1
2
3
4
5
6
7
8
9
10
|
/// <summary>
/// 测试方法:测试将DataTable导出到EXCEL,无模板
/// </summary>
[TestMethod]
public
void
TestExportToExcelByDataTable()
{
DataTable dt = GetDataTable();
string
excelPath = ExcelUtility.Export.ToExcel(dt,
"导出结果"
);
Assert.IsTrue(File.Exists(excelPath));
}
|
结果如下图示:
第二个新增功能,依据数据源(DataTable、DataGridView)的列类型自动将与之对应的EXCEL列的单元格式设为相同的格式内容显示,如:整数类型显示在单元格内无小数的数字格式,有小数位的类显示在单元格内2位小数数字格式,日期类型显示在单元格内日期+时间的日期格式,布尔类型显示在单元格内布尔格式,任意一个无模板的导出方法均支持该功能,示例代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/// <summary>
/// 测试方法:测试将DataTable导出到EXCEL,无模板,且指定导出的列名,以及导出列名的重命名
/// </summary>
[TestMethod]
public
void
TestExportToExcelByDataTable3()
{
DataTable dt = GetDataTable();
string
[] expColNames = {
"Col1"
,
"Col2"
,
"Col3"
,
"Col4"
,
"Col5"
,
"Col7"
};
Dictionary<
string
,
string
> expColAsNames =
new
Dictionary<
string
,
string
>() {
{
"Col1"
,
"列一"
},
{
"Col2"
,
"列二"
},
{
"Col3"
,
"列三"
},
{
"Col4"
,
"数字列"
},
{
"Col5"
,
"列五"
},
{
"Col7"
,
"日期列"
}
};
string
excelPath = ExcelUtility.Export.ToExcel(dt,
"导出结果"
,
null
, expColNames, expColAsNames);
Assert.IsTrue(File.Exists(excelPath));
}
|
结果如下图示:
第三个新增功能,在第二个新增功能的基础上,增加可以自定义设置列的单元格显示格式(支持日期类型、数字类型),任意一个无模板的导出方法均支持该功能,示例代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/// <summary>
/// 测试方法:测试将DataTable导出到EXCEL,无模板,且指定某些列的显示格式
/// </summary>
[TestMethod]
public
void
TestExportToExcelByDataTable6()
{
DataTable dt = GetDataTable();
var
colDataFormatDic =
new
Dictionary<
string
,
string
>
{
{
"Col4"
,
"0.000"
},
//将Col4列DOUBLE类型的EXCEL对应列格式设置为显示成3位小数(默认为2位小数)
{
"Col7"
,
"yyyy-mm-dd"
}
//将Col7列DateTime类型的EXCEL对应列格式设置为年月日(默认为yyyy/mm/dd hh:mm:ss)
};
//更多设置格式可在EXCEL的设置单元格格式中的数字选项卡中的自定义格式列表(若无,可自定义,建议先在EXCEL中测试好格式字符串后再用于程序中)
string
excelPath = ExcelUtility.Export.ToExcel(dt,
"导出结果"
, colDataFormats: colDataFormatDic);
Assert.IsTrue(File.Exists(excelPath));
}
|
结果如下图示:
换种格式定义测试:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/// <summary>
/// 测试方法:测试将DataTable导出到EXCEL,无模板,且指定某些列的显示格式
/// </summary>
[TestMethod]
public
void
TestExportToExcelByDataTable7()
{
DataTable dt = GetDataTable();
var
colDataFormatDic =
new
Dictionary<
string
,
string
>
{
{
"Col4"
,
"¥#,##0.00_);(¥#,##0.00)"
},
//将Col4列DOUBLE类型的EXCEL对应列格式设置为显示成包含货币格式,如:¥5.00(默认为2位小数)
{
"Col7"
,
"yyyy\"年\"m\"月\"d\"日\";@"
}
//将Col7列DateTime类型的EXCEL对应列格式设置为中文年月日,如:2015年12月5日(默认为yyyy/mm/dd hh:mm:ss)
};
//更多设置格式可在EXCEL的设置单元格格式中的数字选项卡中的自定义格式列表(若无,可自定义,建议先在EXCEL中测试好格式字符串后再用于程序中)
string
excelPath = ExcelUtility.Export.ToExcel(dt,
"导出结果"
, colDataFormats: colDataFormatDic);
Assert.IsTrue(File.Exists(excelPath));
}
|
结果如下图示:
注意事项说明:想要实现导出的EXCEL单元格依据数据类型自动设置或手动指定格式,需首先确保数据源的列与自动或手动设置的格式相符,即列类型必需是数字类型、日期类型、布尔类型,不能是以字符串的形式存在的这些所谓的“数字类型、日期类型、布尔类型”
第四个新增的功能,可指定DataGridView是否可以导出隐藏列(不显示的列)、及指定依据DataGridView标题列名导出相应列数据,示例代码如下:
1
2
3
4
5
6
7
8
9
10
|
/// <summary>
/// 测试方法:测试将DataGridView数据导出到EXCEL文件,无模板,且不导出隐藏列
/// </summary>
[TestMethod]
public
void
TestToExcelByDataGridView()
{
var
grid = GetDataGridViewWithData();
string
excelPath = ExcelUtility.Export.ToExcel(grid,
"导出结果"
,
null
,
false
);
Assert.IsTrue(File.Exists(excelPath));
}
|
结果如下图示:
第五个新增功能,DataGridView若改变列的显示位置,导出的数据也能与界面显示的数据同步调整,示例代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/// <summary>
/// 测试方法:测试将DataGridView数据导出到EXCEL文件,无模板,改变列的显示位置,导出隐藏列
/// </summary>
[TestMethod]
public
void
TestToExcelByDataGridView2()
{
var
grid = GetDataGridViewWithData();
//模拟改变列的显示位置
grid.Columns[0].DisplayIndex = 1;
grid.Columns[1].DisplayIndex = 0;
string
excelPath = ExcelUtility.Export.ToExcel(grid,
"导出结果"
,
null
,
true
);
Assert.IsTrue(File.Exists(excelPath));
}
|
结果如下图示:
以下是GetDataGridViewWithData模拟数据方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
private
DataGridView GetDataGridViewWithData()
{
var
grid =
new
DataGridView();
var
dt = GetDataTable();
foreach
(DataColumn col
in
dt.Columns)
{
bool
v = col.Ordinal > 4 ?
false
:
true
;
grid.Columns.Add(
new
DataGridViewTextBoxColumn() { DataPropertyName = col.ColumnName, HeaderText =
"列名"
+ col.ColumnName , Visible = v,ValueType=col.DataType });
}
foreach
(DataRow row
in
dt.Rows)
{
ArrayList values =
new
ArrayList();
foreach
(DataColumn col
in
dt.Columns)
{
values.Add(row[col]);
}
grid.Rows.Add(values.ToArray());
}
return
grid;
}
|
我相信这些功能加上上次的功能,应该能满足大家日常工作中所遇到的各种导出EXCEL场景吧,下面重新公布一下两个核心的与导出相关类源代码,以供大家参考,若有不足之处,敬请指出,谢谢!
ExcelUtility.Export:
ExcelUtility.Base.Common:
第六个新增功能,支持模板中包含多个工作薄导出(目前这个方法还没有模拟测试,所以无法保证其一定有效,大家可以试试),该方法定义如下:
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
|
/// <summary>
/// 由SheetFormatterContainer集合导出基于EXCEL模板(多工作薄)的文件
/// </summary>
/// <param name="templatePath"></param>
/// <param name="formatterContainers"></param>
/// <param name="filePath"></param>
/// <returns></returns>
public
static
string
ToExcelWithTemplate(
string
templatePath,IDictionary<
string
,SheetFormatterContainer<dynamic>> formatterContainers,
string
filePath =
null
)
{
if
(!File.Exists(templatePath))
{
throw
new
FileNotFoundException(templatePath +
"文件不存在!"
);
}
if
(
string
.IsNullOrEmpty(filePath))
{
filePath = Common.GetSaveFilePath();
}
if
(
string
.IsNullOrEmpty(filePath))
return
null
;
string
templateConfigFilePath = Common.GetTemplateConfigFilePath(templatePath,
false
);
var
workbookParameterContainer =
new
WorkbookParameterContainer();
workbookParameterContainer.Load(templateConfigFilePath);
List<SheetFormatter> sheetFormatterList =
new
List<SheetFormatter>();
foreach
(
var
item
in
formatterContainers)
{
SheetParameterContainer sheetParameterContainer = workbookParameterContainer[item.Key];
sheetFormatterList.Add(
new
SheetFormatter(item.Key, item.Value.GetFormatters(sheetParameterContainer)));
}
ExportHelper.ExportToLocal(templatePath, filePath,sheetFormatterList.ToArray());
return
filePath;
}
|
该类库源码已分享到该路径中:http://git.oschina.net/zuowj/ExcelUtility GIT Repository路径:git@git.oschina.net:zuowj/ExcelUtility.git (会持续更新及修正BUG)
本文转自 梦在旅途 博客园博客,原文链接:http://www.cnblogs.com/zuowj/p/5133935.html ,如需转载请自行联系原作者