ASP.NET Open Source Import and Export Library Magicodes.IE Completes Excel Picture Import and Export

Magicodes.IE Excel Picture Import and Export

To better iterate based on actual functionality, starting with the 2.2 Milestone Plan, we will iterate with community suggestions and ideas, which you can submit by clicking on this link:
https://github.com/dotnetcore/Magicodes.IE/issues/46

Explain

This tutorial focuses on how to import and export pictures using Magicodes.IE.Excel.

Main points

  • Configure DTO for Excel Picture Export
  • Configure DTO for Excel Picture Import

Description of Picture Import and Export Features

ExportImageFieldAttribute

  • Height: Height (default 15)
  • Width: Width (default 50)
  • Alt: Replace text when a picture does not exist

ImportImageFieldAttribute

  • ImageDirectory: Picture storage path (default to temporary directory)
  • ImportImageTo: Picture export method (default Base64), supported as follows:
    /// <summary>
    ///Picture import type
    /// </summary>
    public enum ImportImageTo
    {
        /// <summary>
        ///Import to Temporary Directory
        /// </summary>
        TempFolder,

        /// <summary>
        ///Import in base64 format
        /// </summary>
        Base64
    }

Main Steps

1. Installation package Magicodes.IE.Excel

Install-Package Magicodes.IE.Excel

2. Export pictures to Excel using Magicodes.IE.Excel

As shown in the example code below, we need to add the ExportImageFieldAttribute attribute to the picture attribute, use the Width attribute of the attribute to specify the width of the picture, the Height attribute to specify the height of the picture, and the Alt attribute to specify the replacement text, which is displayed when the picture does not exist:

   [ExcelExporter(Name = "test")]
    public class ExportTestDataWithPicture
    {
        [ExporterHeader(DisplayName = "Bold Text", IsBold = true)]
        public string Text { get; set; }

        [ExporterHeader(DisplayName = "Plain text")] public string Text2 { get; set; }
        [ExporterHeader(DisplayName = "ignore", IsIgnore = true)]
        public string Text3 { get; set; }

        [ExportImageField(Width = 20, Height = 120)]
        [ExporterHeader(DisplayName = "Figure 1")]
        public string Img1 { get; set; }
        [ExporterHeader(DisplayName = "numerical value", Format = "#,##0")]
        public decimal Number { get; set; }
        [ExporterHeader(DisplayName = "Name", IsAutoFit = true)]
        public string Name { get; set; }
        /// <summary>
        ///Time Test
        /// </summary>
        [ExporterHeader(DisplayName = "Date 1", Format = "yyyy-MM-dd")]
        public DateTime Time1 { get; set; }

        [ExportImageField(Width = 50, Height = 120, Alt = "404")]
        [ExporterHeader(DisplayName = "chart", IsAutoFit = false)]
        public string Img { get; set; }
    }

It is worth noting that:

  • The ExportImageFieldAttribute attribute is required
  • Picture property type must be string type, support local picture and remote picture address

Next, we can use the API to perform the export.In fact, apart from Dto, the export API is still one, as shown in the following code:

        public async Task ExportPicture_Test()
        {
            IExporter exporter = new ExcelExporter();
            var url = Path.Combine("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 = "https://docs.microsoft.com/en-us/media/microsoft-logo-dark.png";
            }
            var result = await exporter.Export(filePath, data);
        }

As shown in the code above, the current picture export can be set to a remote picture address, as well as a local picture address, and can also be set to null (null will be replaced by alternative text).The effect is as follows:

3. Import pictures using Magicodes.IE.Excel

Magicodes.IE.Excel supports importing pictures from Excel using only the feature ImportImageField.It supports two ways of importing:

  • Import to Temporary Directory
  • Import as Base64

You only need to set the ImportImageTo property.

ImportImageTo enumeration

  • base64 imported in base64 format

  • TempFolder Import to Temp Directory

It is worth noting that, as above, the Picture Attribute Field type only supports the string type.

Prepare Excel with Pictures to Import

Before we start, we need to prepare Excel for import, and the template in this example is shown in the following figure:

Import to Temporary Directory

The Dto model is as follows:

    public class ImportPictureDto
    {
        [ImporterHeader(Name = "Bold Text")]
        public string Text { get; set; }
        [ImporterHeader(Name = "Plain text")]
        public string Text2 { get; set; }

        /// <summary>
        ///Write pictures to temporary directory
        /// </summary>
        [ImportImageField(ImportImageTo = ImportImageTo.TempFolder)]
        [ImporterHeader(Name = "Figure 1")]
        public string Img1 { get; set; }
        [ImporterHeader(Name = "numerical value")]
        public string Number { get; set; }
        [ImporterHeader(Name = "Name")]
        public string Name { get; set; }
        [ImporterHeader(Name = "date")]
        public DateTime Time { get; set; }

        /// <summary>
        ///Write pictures to temporary directory
        /// </summary>
        [ImportImageField(ImportImageTo = ImportImageTo.TempFolder)]
        [ImporterHeader(Name = "chart")]
        public string Img { get; set; }
    }

Import is still that import, but the Dto settings have changed:

        public async Task ImportPicture_Test()
        {
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "Import", "Picture Import Template.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));
        }

Pictures from Excel are imported into the temporary directory as shown in the following image.It is worth noting that:

  • After importing pictures into temporary catalogs, if the import results meet business needs, immediately move the pictures to official storage locations, such as site catalogs, cloud storage, etc.
  • Picture import also supports specified locations, but is not recommended.

Import Picture as base64

To import a picture into base64, simply set the ImportImageTo property value to ImportImageTo.Base64:

    public class ImportPictureBase64Dto
    {
        [ImporterHeader(Name = "Bold Text")]
        public string Text { get; set; }
        [ImporterHeader(Name = "Plain text")]
        public string Text2 { get; set; }

        /// <summary>
        ///Import picture as base64 (default is base64)
        /// </summary>
        [ImportImageField(ImportImageTo = ImportImageTo.Base64)]
        [ImporterHeader(Name = "Figure 1")]
        public string Img1 { get; set; }

        [ImporterHeader(Name = "numerical value")]
        public string Number { get; set; }
        [ImporterHeader(Name = "Name")]
        public string Name { get; set; }
        [ImporterHeader(Name = "date")]
        public DateTime Time { get; set; }

        /// <summary>
        ///Import pictures into temporary directory
        /// </summary>
        [ImportImageField(ImportImageTo = ImportImageTo.TempFolder)]
        [ImporterHeader(Name = "chart")]
        public string Img { get; set; }
    }

The import code is the same as above:

public async Task ImportPictureBase64_Test()
        {
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "Import", "Picture Import Template.xlsx");
            var import = await Importer.Import<ImportPictureBase64Dto>(filePath);
        }

As shown in the following figure, we can easily get the base64 encoding result of the picture:

Reference

https://github.com/dotnetcore/Magicodes.IE

Tags: ASP.NET Excel IE Attribute github

Posted on Tue, 17 Mar 2020 17:54:40 -0700 by praxiz