随笔 - 750  文章 - 1  评论 - 107  阅读 - 34万

[转]Epplus 导出Excel 示例

部分内容比较生硬,还需要继续优化

复制代码
try
{
    using (var package = new ExcelPackage())
    {
        ExcelHelper.CreateExcel<ExcelInfo>(package, sheetName, title, titlesName, axs);

        return File(package.GetAsByteArray(), "text/plain", fileName);
    }
}
catch (Exception ex)
{
    return File(Encoding.UTF8.GetBytes(ex.Message), "text/plain", "Error.txt");
}
复制代码

 

复制代码
public static ExcelWorksheet CreateExcel<T>(ExcelPackage package, string sheetName, string title, string[] titles, IEnumerable<T> axs)
{
    // 导出 Excel 
    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(sheetName);
    // 第一行
    worksheet.Cells[1, 1].Value = title;

    // 标题
    var rCount = 2; // 行数
    var cCount = 1; // 列数
    Dictionary<string, string> tis = new Dictionary<string, string>();
    Type type = typeof(T);
    foreach (var item in titles)
    {
        var index = item.IndexOf(':');
        if (index > 0)
        {
            var key = item.Substring(0, index);
            var value = item.Substring(index + 1);
            tis.Add(key, value);
            worksheet.Cells[rCount, ++cCount].Value = value;
        }
        else
        {
            var col = type.GetProperty(item, BindingFlags.Instance | BindingFlags.Public);
            if (col != null && !tis.ContainsKey(item))
            {
                var colName = item;
                var atts = col.GetCustomAttributes(typeof(DisplayNameAttribute), false);
                if (atts != null && atts.Length > 0)
                {
                    var att = atts[0] as DisplayNameAttribute;
                    colName = att.DisplayName;
                }
                tis.Add(item, colName);
                worksheet.Cells[rCount, ++cCount].Value = colName;
            }
        }
    }
    // 第1行格式
    rCount = 1;
    worksheet.Cells[rCount, 1, rCount, cCount].Merge = true;
    AddBorder(worksheet.Cells[rCount, 1, rCount, cCount]);
    AlignmentCenter(worksheet.Cells[rCount, 1, rCount, cCount]);
    worksheet.Row(rCount).Height = 27;
    // 第2行格式
    rCount = 2;
    AddBorder(worksheet.Cells[rCount, 1, rCount, cCount]);
    AlignmentCenter(worksheet.Cells[rCount, 1, rCount, cCount]);
    worksheet.Cells[rCount, 1, rCount, cCount].Style.Fill.PatternType = ExcelFillStyle.Solid;
    worksheet.Cells[rCount, 1, rCount, cCount].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(83, 141, 213));
    worksheet.Cells[rCount, 1, rCount, cCount].Style.Font.Color.SetColor(Color.White);
    worksheet.Cells[rCount, 1, rCount, cCount].Style.Font.Bold = true;
    worksheet.Row(rCount).Height = 27;

    rCount = 3;
    foreach (var row in axs)
    {
        worksheet.Cells[rCount, 1].Value = rCount - 2;
        cCount = 2;
        foreach (var item in tis)
        {
            worksheet.Cells[rCount, cCount].Value = ConvertHelper.GetString(type.GetProperty(item.Key).GetValue(row, null));
            cCount++;
        }
        AddBorder(worksheet.Cells[rCount, 1, rCount, cCount]);
        AlignmentCenter(worksheet.Cells[rCount, 1, rCount, cCount]);
        worksheet.Row(rCount).Height = 23;

        rCount++;
    }

    // 设置列宽
    for (int i = 0; i <= tis.Count; i++)
    {
        worksheet.Column(i + 1).AutoFit();
    }
    return worksheet;
    //return package.GetAsByteArray();
}
复制代码

 

posted on   z5337  阅读(123)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示