欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實現(xiàn)NPOI的Excel導(dǎo)出詳解

 更新時間:2022年01月19日 17:16:04   作者:嘿!等你下課  
這篇文章主要介紹了C#實現(xiàn)NPOI的Excel導(dǎo)出的示例代碼,文中的實現(xiàn)過程講解詳細(xì),對我們的學(xué)習(xí)或工作有一定的幫助,感興趣的可以跟隨小編一起學(xué)習(xí)一下

技術(shù)點:

1.自定義attribute屬性

2.通過反射取類及其屬性的attribute屬性值

3.NPOI包常用屬性及方法(我也僅僅知道用到過的,陌生的要么見名知意,要么百度查)

實現(xiàn)功能點:

List類對象的模板導(dǎo)出,實用場景例子見最后代碼塊(emm...還是比較抽象,代碼見)

EXCEL導(dǎo)出類DTO超類

定義繼承導(dǎo)出類DTO的特性說明類

Excel幫助類

這部分要講的點其實挺多的,關(guān)鍵就是EXCEL導(dǎo)出所用到的數(shù)據(jù)源是強類型的。

可以看出來list其實是EF的Queryable toList()后的類集合,作為數(shù)據(jù)源存在

里面的DTO DesWeeklyReportExcExp繼承ExcelSuper,特性分別加在類及屬性上。

public class XXXXController : CoreController
{
    // 控制器內(nèi)部
    
    [HttpPost]
    public ActionResult export()
    {
        // 控制器接口
        var list = op
                    .GetPagedQuery(PageModel)
                    .Select(s => new DesWeeklyReportExcExp
                    {
                        col1 = s.Project.ProjName,
                        col2 = s.ColAttROPDate1?.ToString("yyyy.MM.dd"),
                        col3 = (s.ColAttROPDate2 == null ? "無" : s.ColAttROPDate2.Value.ToString("yyyy.MM.dd"))
                                                       + "/"
                                                       + (s.ColAttROPDate3 == null ? "無" : s.ColAttROPDate3.Value.ToString("yyyy.MM.dd")),
                        col4 = s.ColAttROPDate4?.ToString("yyyy.MM.dd")
                    }).ToList();
         string filePath = Server.MapPath("~/download/[這是模板名稱].xlsx");
         string filename = Path.GetFileNameWithoutExtension(filePath);// 文件名稱
         string extension = Path.GetExtension(filePath);// 后綴名 帶點(.)
         string fileDownloadName = filename + extension;

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

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

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

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

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

到此這篇關(guān)于C#實現(xiàn)NPOI的Excel導(dǎo)出詳解的文章就介紹到這了,更多相關(guān)C# NPOI的Excel導(dǎo)出內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論