SoFunction
Updated on 2025-03-06

Detailed explanation of Excel export of C# implements NPOI

Technical points:

1. Customizeattributeproperty

2. Retrieve the class and its properties through reflectionattributeAttribute value

Common properties and methods of packages (I only know what I have used. Those who are unfamiliar can either see the name and know the meaning or search on Baidu)

Implementation function points:

Template export of List class objects, see the last code block for practical scenario examples (emm... is still relatively abstract, see the code)

EXCEL export class DTO superclass

Define the feature description class that inherits and exports the class DTO

Excel Help Class

There are actually quite a lot of points to talk about in this part. The key is that the data source used for EXCEL export is strongly typed.

It can be seenlistActually it's EFQueryable toList()The following class collection exists as a data source

DTO insideDesWeeklyReportExcExpinheritExcelSuper, the characteristics are added to the class and attributes respectively.

public class XXXXController : CoreController
{
    // Inside the controller    
    [HttpPost]
    public ActionResult export()
    {
        // Controller interface        var list = op
                    .GetPagedQuery(PageModel)
                    .Select(s => new DesWeeklyReportExcExp
                    {
                        col1 = ,
                        col2 = s.ColAttROPDate1?.ToString(""),
                        col3 = (s.ColAttROPDate2 == null ? "none" : s.(""))
                                                       + "/"
                                                       + (s.ColAttROPDate3 == null ? "none" : s.("")),
                        col4 = s.ColAttROPDate4?.ToString("")
                    }).ToList();
         string filePath = ("~/download/[This is the template name].xlsx");
         string filename = (filePath);// File name         string extension = (filePath);// Suffix name with dots (.)         string fileDownloadName = filename + extension;

         var fs = (list, filePath).ToArray();
         return File(fs, "application/ms-excel", fileDownloadName);
    }
}

[ExcelExpClassAttribute(2, 0, 2, 0)]
public class DesWeeklyReportExcExp : ExcelSuper
{
    /// <summary>
    /// Column 1    /// </summary>
    [ExcelExp(SortIndex = 0, ColName = "Column 1")]
    public string col1 { get; set; }

    /// <summary>
    /// Column 2    /// </summary>
    [ExcelExp(SortIndex = 0, ColName = "Column 2")]
    public string col2 { get; set; }

    /// <summary>
    /// Column 3    /// </summary>
    [ExcelExp(SortIndex = 0, ColName = "Column 3")]
    public string col3 { get; set; }

    /// <summary>
    /// Column 4    /// </summary>
    [ExcelExp(SortIndex = 0, ColName = "Column 4")]
    public string col4 { get; set; }
}

This is the end of this article about the detailed explanation of C#’s implementation of NPOI’s Excel export. For more information about C# NPOI’s Excel export content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!