文章目录
简介
- 简介
- 导入
- 导出
- 简单导出
- 样式格式化
- 其他
- 总结
EPPlus是一个使用Open Office XML(xlsx)文件格式,能读写Excel 2007/2010 文件的开源组件,在导出Excel的时候不需要电脑上安装office,它的一个缺点就是不支持导出2003版的Excel(xls)。
.net core 通过nuget 包管理器添加
这部分相对简单,直接看下代码:
using (ExcelPackage package = new ExcelPackage(existingFile))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
//获取表格的列数和行数
int rowCount = worksheet.Dimension.Rows;
int colCount = worksheet.Dimension.Columns;
for (int row = 1; row
{
var col = targetSheet.Dimension.Columns;
// cells参数:第一个是开始行索引,第二个是开始列索引
// 第三个是结束行索引,第四个是结束列的索引,注意:结束索引不能比开始索引小
using (ExcelRange rng = targetSheet.Cells[1, 1, 1, col - 1])
{
// 对字体的设置
rng.Style.Font.Bold = true;
rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
rng.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
rng.Style.Font.Size = 8;
rng.Style.Font.Color.SetColor(Color.FromArgb(0, 0, 128));
// 单元格背景色的设置
rng.Style.Fill.PatternType = ExcelFillStyle.Solid;
rng.Style.Fill.BackgroundColor.SetColor(backgroundColor);
//单独设置单元格底部边框样式和颜色(上下左右均可分开设置)
rng.Style.Border.BorderAround(ExcelBorderStyle.Thin);
rng.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
rng.Style.Border.Right.Style = ExcelBorderStyle.Thin;
// 自当使用文字大小
rng.AutoFitColumns();
}
//单独设置单元格底部边框样式和颜色(上下左右均可分开设置)
targetSheet.Cells[1, 3].Style.Border.Right.Style = ExcelBorderStyle.Thick;
return true;
};
// 表示第一,二行,第一列就进行单元格合并
worksheetIn.Cells[1, 1, 2, 1].Merge = true;
使用方式:
SetHeadStyle(worksheetIn, Color.FromArgb(146, 208, 80));
也可参考此文https://www.jianshu.com/p/b7f588ccf26b
其他EPPlus对现有excel的操作好像不是很好,即如果你对已经存在的sheet进行操作,然后保存的时候是报错的,但是添加和删除sheet都是没问题的。所以如果要对现有的sheet做操作可以先删除在添加即可。如下
var hasSheet = package.Workbook.Worksheets[sheetName];
if (hasSheet != null)
{
package.Workbook.Worksheets.Delete(sheetName);
}
ExcelWorksheet worksheetIn = package.Workbook.Worksheets.Add(sheetName);
如果有对sheet位置有要求的,EPPlus默认添加sheet都是 Add 或者 Append 方法,没有插入到指定的位置的方法。但是经过查看源码,其提供了移动到指定为的方法,代码如下:
package.Workbook.Worksheets.MoveAfter(package.Workbook.Worksheets.Count - 1, 1); // 索引默认从1开始
源码定义如下:
//
// 摘要:
// Moves the source worksheet to the position after the target worksheet
//
// 参数:
// sourceName:
// The name of the source worksheet
//
// targetName:
// The name of the target worksheet
public void MoveAfter(string sourceName, string targetName);
//
// 摘要:
// Moves the source worksheet to the position after the target worksheet
//
// 参数:
// sourcePositionId:
// The id of the source worksheet
//
// targetPositionId:
// The id of the target worksheet
public void MoveAfter(int sourcePositionId, int targetPositionId);
//
// 摘要:
// Moves the source worksheet to the position before the target worksheet
//
// 参数:
// sourcePositionId:
// The id of the source worksheet
//
// targetPositionId:
// The id of the target worksheet
public void MoveBefore(int sourcePositionId, int targetPositionId);
//
// 摘要:
// Moves the source worksheet to the position before the target worksheet
//
// 参数:
// sourceName:
// The name of the source worksheet
//
// targetName:
// The name of the target worksheet
public void MoveBefore(string sourceName, string targetName);
//
// 摘要:
// Moves the source worksheet to the end of the worksheets collection
//
// 参数:
// sourceName:
// The name of the source worksheet
public void MoveToEnd(string sourceName);
//
// 摘要:
// Moves the source worksheet to the end of the worksheets collection
//
// 参数:
// sourcePositionId:
// The position of the source worksheet
public void MoveToEnd(int sourcePositionId);
//
// 摘要:
// Moves the source worksheet to the start of the worksheets collection
//
// 参数:
// sourceName:
// The name of the source worksheet
public void MoveToStart(string sourceName);
//
// 摘要:
// Moves the source worksheet to the start of the worksheets collection
//
// 参数:
// sourcePositionId:
// The position of the source worksheet
public void MoveToStart(int sourcePositionId);
总结
总体上来说,EPPlus操作相对还是毕竟简单的,而且从导出数据量上来看,也是很有优势的。有兴趣的可自行度娘了解其优劣情况。而如果想在.net core项目里面完成excel 的导入导出,也可考虑使用Magicodes.IE。这个是一个开源的项目,完全不用担心商用的问题,而且其内部实现也使用了EPPlus来实现的。