EPPlus 使用小结

EPPlus 使用小结文章目录简介导入导出简单导出样式格式化其他总结简介EPPlus是一个使用OpenOfficeXML(xlsx)文件格式,能读写Excel2007/2010文件的开源组件,在导出Excel的时候不需要电脑上安装office,它的一个缺点就是不支持导出2003版的Excel(xls)。导入这部分相对简单,直接看下代码:using(ExcelPackagepackage=newExcelPackage(existingFile)){ExcelWorksheetworksh

大家好,又见面了,我是你们的朋友全栈君。

简介

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 <= rowCount; row++)
    {
        // 具体的获取数据
        // worksheet.Cells[row, 1].Value.ToString();
    }
}

根据具体的业务情况,将excel中的数据导入到数据库中即可。

导出

简单导出

直接看代码:

// excelPath 为excel文件路径,如果没有,需要使用 FileStream 来创建,而不是使用 FileInfo
FileInfo existingFile = new FileInfo(excelPath);

using (ExcelPackage package = new ExcelPackage(existingFile))
{
    // sheetName 为 sheet 名称
    ExcelWorksheet worksheetIn = package.Workbook.Worksheets.Add(sheetName);
    // 第二参数为true 则会把 lstData定义的属性名称作为excel标题
    worksheetIn.Cells.LoadFromCollection(lstData, false);
    
    package.Save(); //Save the workbook.
}

样式格式化

还是直接看到代码:

Func<ExcelWorksheet, Color, bool> SetHeadStyle = (targetSheet, backgroundColor) =>
{
    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来实现的。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/148431.html原文链接:https://javaforall.net

(2)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • LAMP配置点滴[通俗易懂]

    LAMP配置点滴[通俗易懂]第一次配编译安装LAMP足足弄了两天,各种折腾。参考http://www.lamphowto.com/,其他的可以看源码的README,或官方文档其中让浏览器自动提示语法错误的方法是改php.ini:装好后打开网页:localhost/index.html居然显示:TherequestedURL/index.htmlwasnotfoundon

    2022年5月10日
    40
  • Linux 环境变量配置汇总

    Linux 环境变量配置汇总Linux环境变量配置汇总01、Linux环境变量配置02、Linux读取环境变量03、Linux环境变量配置方法一:exportPATH或者把PATH放在前面04、Linux环境变量配置方法二:vim~/.bashrc在最后一行加上05、Linux环境变量配置方法三:vim~/.bash_profile06、Linux环境变量配置方法四:vim/etc/bashrc07、Linux环境变量配置方法五:vim/etc/profile在最后一行加上08、Linux环境变量配置方法六:vim/et

    2022年5月30日
    51
  • Java8 Stream使用flatMap合并List

    Java8 Stream使用flatMap合并List之前也写过很多篇关于Java8使用的文章了,但是回顾一下,好像还没介绍过Java8Stream的flatMap操作,昨天刚好在工作中遇到一个场景,发现flatMap简直太方便了,这里总结一下flatMap的常规使用。附带讲一下,使用Java8实现集合的并、交、差操作,其实之前也讲过一种使用Guava的实现方式,具体请参考Guava集合工具 flatMap 首先看一下一种场景,存在一个M…

    2022年6月4日
    97
  • TortoiseSVN文件夹及文件图标不显示解决方法

    TortoiseSVN文件夹及文件图标不显示解决方法

    2021年9月20日
    42
  • 解决vscode中文乱码的代码_vscode终端

    解决vscode中文乱码的代码_vscode终端我们现在很多编程开放工作中,VSCode已经成了一款难以绕过去的轻量级完善好用的代码编辑器。功能完善兼容性好体验不错,受到越来越多的开发者的认可。所以涉及到编程工作,VScode的安装是个必备的准备工作。我们本文就介绍下Windows和Ubuntu这样的Linux两个操作系统下安装VScode编辑器并解决打开代码文件的时候其中中文显示乱码的问题。当然Windows版本的VScode安装很简单,我们…

    2022年9月2日
    4
  • mac如何同时录制系统和麦克风声音或只录制系统声音

    mac如何同时录制系统和麦克风声音或只录制系统声音MAC录屏时的系统声音以及麦克风问题推荐直接appstore下载recordit,然后打开软件根据软件提示安装blackhole插件。之后就可以关掉该软件了,不用升级会员,以后我们录屏可以不再打开该软件了。到录屏的时候先调整好扬声器或耳机的音量大小,因为之后是不能调的。调好后选择recordit多输出设备,然后shift+command+5开始录屏,录屏选项选择recordit聚焦设备,就可以同时录制系统声音和麦克风声音了。选择blackhole16ch则是只录制系统声音。…

    2022年5月9日
    180

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号