ASP.NET 开源导入导出库Magicodes.IE 完成Excel图片导入导出

编程入门 行业动态 更新时间:2024-10-12 12:27:01
magicodes.ie excel图片导入导出

为了更好的根据实际功能来迭代,从2.2的里程碑规划开始,我们将结合社区的建议和意见来进行迭代,您可以点此链接来提交您的意见和建议:github/dotnetcore/magicodes.ie/issues/46

说明

本章教程主要说明如何使用magicodes.ie.excel进行图片的导入导出。

要点
  • 配置dto进行excel图片导出
  • 配置dto进行excel图片导入
图片导入导出特性说明exportimagefieldattribute
  • height: 高度(默认15)
  • width:宽度(默认50)
  • alt:图片不存在时替换文本
importimagefieldattribute
  • imagedirectory: 图片存储路径(默认存储到临时目录)
  • importimageto:图片导出方式(默认base64),支持的方式如下所示:
/// <summary> /// 图片导入类型 /// </summary> public enum importimageto { /// <summary> /// 导入到临时目录 /// </summary> tempfolder, /// <summary> /// 导入为base64格式 /// </summary> base64 }主要步骤1.安装包magicodes.ie.excelinstall-package magicodes.ie.excel2.使用magicodes.ie.excel导出图片到excel

如下述示例代码所示,我们需要在图片属性上添加exportimagefieldattribute特性,使用特性的“width”属性指定图片宽度,“height”属性指定图片高度,“alt”属性指定替换文本,也就是当图片不存在时则会显示此文本:

[excelexporter(name = "测试")] public class exporttestdatawithpicture { [exporterheader(displayname = "加粗文本", isbold = true)] public string text { get; set; } [exporterheader(displayname = "普通文本")] public string text2 { get; set; } [exporterheader(displayname = "忽略", isignore = true)] public string text3 { get; set; } [exportimagefield(width = 20, height = 120)] [exporterheader(displayname = "图1")] public string img1 { get; set; } [exporterheader(displayname = "数值", format = "#,##0")] public decimal number { get; set; } [exporterheader(displayname = "名称", isautofit = true)] public string name { get; set; } /// <summary> /// 时间测试 /// </summary> [exporterheader(displayname = "日期1", format = "yyyy-mm-dd")] public datetime time1 { get; set; } [exportimagefield(width = 50, height = 120, alt = "404")] [exporterheader(displayname = "图", isautofit = false)] public string img { get; set; } }

值得注意的是:

  • exportimagefieldattribute特性是必须的
  • 图片属性类型必须为string类型,支持本地图片和远程图片地址

接下来,我们就可以使用api来执行导出了。其实除了dto的不同,导出api还是一个,如下述代码所示:

public async task exportpicture_test() { iexporter exporter = new excelexporter(); var url = pathbine("testfiles", "exportertest.png"); for (var i = 0; i < data.count; i++) { var item = data[i]; item.img1 = url; if (i == 4) item.img = null; else item.img = "docs.microsoft/en-us/media/microsoft-logo-dark.png"; } var result = await exporter.export(filepath, data); }

如上述代码所示,目前图片导出可以设置为远程图片地址,以及本地图片地址,并且也可以设置为null(null将会被替代文本所代替)。效果如下图:

3.使用magicodes.ie.excel导入图片

magicodes.ie.excel支持从excel导入图片,仅需使用特性“importimagefield”。其支持两种导入方式:

  • 导入到临时目录
  • 导入为base64

仅需设置importimageto属性。

importimageto枚举

  • base64 导入为base64格式

  • tempfolder 导入到临时目录

值得注意的是,同上文一样,图片属性字段类型也仅支持string类型。

准备待导入的包含图片的excel

在开始之前,我们需准备好导入的excel,本示例中的模板如下图所示:

导入到临时目录

dto模型如下所示:

public class importpicturedto { [importerheader(name = "加粗文本")] public string text { get; set; } [importerheader(name = "普通文本")] public string text2 { get; set; } /// <summary> /// 将图片写入到临时目录 /// </summary> [importimagefield(importimageto = importimageto.tempfolder)] [importerheader(name = "图1")] public string img1 { get; set; } [importerheader(name = "数值")] public string number { get; set; } [importerheader(name = "名称")] public string name { get; set; } [importerheader(name = "日期")] public datetime time { get; set; } /// <summary> /// 将图片写入到临时目录 /// </summary> [importimagefield(importimageto = importimageto.tempfolder)] [importerheader(name = "图")] public string img { get; set; } }

导入还是那个导入,只是dto设置变了:

public async task importpicture_test() { var filepath = pathbine(directory.getcurrentdirectory(), "testfiles", "import", "图片导入模板.xlsx"); var import = await importer.import<importpicturedto>(filepath); if (import.exception != null) _testoutputhelper.writeline(import.exception.tostring()); if (import.rowerrors.count > 0) _testoutputhelper.writeline(jsonconvert.serializeobject(import.rowerrors)); }

如下图所示,excel中的图片就会导入到临时目录。值得注意的是:

  • 图片导入到临时目录之后,如果导入结果符合业务需要,请立即将图片移动到正式存储位置,比如网站目录、云存储等;
  • 图片导入也支持指定位置,不过不推荐。

将图片导入为base64

将图片导入为base64仅需设置“importimageto”属性值为“importimageto.base64”即可:

public class importpicturebase64dto { [importerheader(name = "加粗文本")] public string text { get; set; } [importerheader(name = "普通文本")] public string text2 { get; set; } /// <summary> /// 将图片导入为base64(默认为base64) /// </summary> [importimagefield(importimageto = importimageto.base64)] [importerheader(name = "图1")] public string img1 { get; set; } [importerheader(name = "数值")] public string number { get; set; } [importerheader(name = "名称")] public string name { get; set; } [importerheader(name = "日期")] public datetime time { get; set; } /// <summary> /// 将图片导入到临时目录 /// </summary> [importimagefield(importimageto = importimageto.tempfolder)] [importerheader(name = "图")] public string img { get; set; } }

导入代码同上:

public async task importpicturebase64_test() { var filepath = pathbine(directory.getcurrentdirectory(), "testfiles", "import", "图片导入模板.xlsx"); var import = await importer.import<importpicturebase64dto>(filepath); }

如下图所示,我们就很方便的得到了图片的base64编码的结果:

reference

github/dotnetcore/magicodes.ie

  • 0
  • 0
  • 0
  • 0
  • 0

更多推荐

ASP.NET 开源导入导出库Magicodes.IE 完成Excel图片导入导出

本文发布于:2023-06-11 00:12:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/621994.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:开源   图片   NET   ASP   Magicodes

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!