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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 用Dedecms5.7的arclist标签调用文章内容

    用Dedecms5.7的arclist标签调用文章内容

    2021年9月24日
  • 海思Hi3798处理器参数,Hi3798芯片详细信息介绍[通俗易懂]

    海思Hi3798处理器参数,Hi3798芯片详细信息介绍[通俗易懂]Hi3798CV200集成4核64位高性能CortexA53处理器、内置NEON加速引擎,强大的CPU处理能力可以满足各种差异化的业务需求。在码流兼容性、在线视频播放的流畅性、图像质量以及整机性能方面保持业界最好的用户体验。支持4K2KP60@10bit超高清视频解码和显示,支持H.265/HEVC、H.264/AVC、AVS+、MVC、MPEG2、MPEG4、VC-1、VP6、VP…

    2022年6月30日
  • 51单片机最小系统解读

    51单片机最小系统解读提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录前言一、51单片机最小系统模块构成二、电源电路1.电源引脚三、时钟电路1.外部晶振引脚2.晶振(时钟电路)3.时钟电路小tips四、复位电路1.按键复位2.上电复位总结前言在学习51单片机的时候我们最先接触到的就是单片机最小系统,单片机最小系统又叫最小应用系统,顾名思义就是能够使单片机实现简单运行的最少原件的组合。提示:以下将以51单片机最小系统为例进行介绍一、51单片机最小系统模块构成二、电源电路一个系统的

    2022年6月23日
  • SRT字幕格式_手机srt文件怎么加入视频

    SRT字幕格式_手机srt文件怎么加入视频srt字幕以其简单、体积小、易查看、易掌握等优点,深得人们的喜爱,但便利的代价就是样式少,无法实现复杂的特效。本文整理了srt字幕的基本格式以及支持的格式,同时介绍了ffmpeg中srt格式生成和

    2022年8月3日
  • macbookpro双系统分区_macbook双系统分区调整

    macbookpro双系统分区_macbook双系统分区调整一些前序知识:1、Mac系统不是Intel的i386架构,没有Bios,但是有EFI,通过EFI管理系统的引导。2、Mac系统的分区表采用GUID,不是MBR。因此如果硬盘完全交给windows控制,会导致Mac系统无法启动。3、任何对MBR的强行操作,会导致已经装好的双系统引导失效。4、Mac的EFI分区会用GPT锁定,不要尝试去操作这个分区。我们…

    2022年10月5日
  • 基于AWS使用深度学习的10个Linux命令

    基于AWS使用深度学习的10个Linux命令

    2022年3月6日

发表回复

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

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