EPPlus Excel 导出 - 每条记录的所有数据值都进入 A 列?

EPPlus Excel Export - All data values going into Column A for each record?

我正在尝试通过 EPPlus 库和 System.Linq.Dynamic 向我的 MVC5/Code-First Entity Framework 应用程序添加 Excel 导出功能。在我的导出 View 中,我用每个 INV_Assets 模型属性填充了一个 MultiSelectList。然后将这些传递到我的 ExportController 以指定要导出模型的哪些字段。

我已将 Headers 导出到第 1 行,但我仍然无法导出要导出的数据。目前,我的 selected 字段的数据都出现了,但每个值都作为长字符串导出到 A 列中它自己的行中。例如,如果我 select 以下字段(状态,ip_address、mac_address、注释、所有者、成本、po_number 和描述)我得到以下信息:

第 1 行:[Status][ip_address][mac_address][note][owner][cost][po_number][description]

第 2 行:[{Status=SIGNEDOUT, ip_address=10.10.121.25, mac_address=10.10.134.11, note=, owner=John Smith, cost=35.00, po_number=G348, description=This is a description of the item.}][][][][][][][]

有谁知道如何将其分解为值实际进入正确列单元格的位置,而不是作为一个长长的巨型字符串?

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using InventoryTracker.DAL;
using OfficeOpenXml;
using InventoryTracker.Models;
using System.Linq.Dynamic;


namespace InventoryTracker.Controllers
{
    public class ExportController : Controller
    {
        InventoryTrackerContext _db = new InventoryTrackerContext();
        public static List<DynamicColumns> DynamicColumnsCollection = new List<DynamicColumns>();

        [HttpPost]
        public ActionResult ExportUsingEPPlus(ExportAssetsViewModel model)
        {
            ExcelPackage package = new ExcelPackage();
            var ws = package.Workbook.Worksheets.Add("TestExport");

            var exportFields = new List<string>();
            foreach (var selectedField in model.SelectedFields)
            {
                // Adds selected fields to [exportFields] List<string>
                exportFields.Add(model.ListOfExportFields.First(s => s.Key == selectedField).Value);
            }

            IQueryable selectStatement = DynamicSelectionColumns(exportFields);

            // Loops to insert column headings into Row 1 of Excel
            for (int i = 0; i < exportFields.Count(); i++)
            {
                ws.Cells[1, i + 1].Value = exportFields[i].ToString();
            }

            // Insert Data -- Currently all one long string in Column A per record
            if (selectStatement.Count() > 0)
            {
                ws.Cells["A2"].LoadFromCollection(selectStatement.Cast<object>(), true);
            }

            var memoryStream = new MemoryStream();
            package.SaveAs(memoryStream);

            string fileName = "Exported-InventoryAssets-" + DateTime.Now + ".xlsx";
            string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

            memoryStream.Position = 0;
            return File(memoryStream, contentType, fileName);
        }

        public IQueryable DynamicSelectionColumns(List<string> fieldsForExport)
        {
            using (var db = new InventoryTrackerContext())
            {
                string fieldIds = "," + "4,5,3,2,6,17,11,12" + ",";

                var taskColum = Enum.GetValues(typeof(EnumTasks)).Cast<EnumTasks>().Where(e => fieldIds.Contains("," + ((int)e).ToString() + ",")).Select(e => e.ToString().Replace("_", ""));

                string select = "new (  " + string.Join(", ", fieldsForExport) + ")";

                return db.INV_Assets.ToList().Select(t => new DynamicColumns()
                {
                    Id = t.Id,
                    Manufacturer = Convert.ToString(t.Manufacturer.manufacturer_description),
                    Type = t.Type.type_description,
                    Location = t.Location.location_room,
                    Vendor = t.Vendor.vendor_name,
                    Status = t.Status.status_description,
                    ip_address = t.ip_address,
                    mac_address = t.mac_address,
                    note = t.note,
                    owner = t.owner,
                    //Module = t.Module != null ? t.Module.Name : "", 
                    cost = t.cost,
                    po_number = t.po_number,
                    description = t.description,
                    invoice_number = t.invoice_number,
                    serial_number = t.serial_number,
                    asset_tag_number = t.asset_tag_number,
                    acquired_date = t.acquired_date,
                    disposed_date = t.disposed_date,
                    verified_date = t.verified_date,
                    created_date = t.created_date,
                    created_by = t.created_by,
                    modified_date = t.modified_date,
                    modified_by = t.modified_by
                }).ToList().AsQueryable().Select(select);
            }
        }
    }

    public class DynamicColumns : INV_Assets
    {
        public string Model { get; set; }
        public string Manufacturer { get; set; }
        public string Type { get; set; }
        public string Location { get; set; }
        public string Vendor { get; set; }
        public string Status { get; set; }
        public string ip_address { get; set; }
        public string mac_address { get; set; }
        public string note { get; set; }
        public string owner { get; set; }
        public decimal cost { get; set; }
        public string po_number { get; set; }
        public string description { get; set; }
        public int invoice_number { get; set; }
        public string serial_number { get; set; }
        public string asset_tag_number { get; set; }
        public DateTime? acquired_date { get; set; }
        public DateTime? disposed_date { get; set; }
        public DateTime? verified_date { get; set; }
        public DateTime created_date { get; set; }
        public string created_by { get; set; }
        public DateTime? modified_date { get; set; }
        public string modified_by { get; set; }
    }

    public enum EnumTasks
    {
        Model = 1,
        Manufacturer = 2,
        Type = 3,
        Location = 4,
        Vendor = 5,
        Status = 6,
        ip_address = 7,
        mac_address = 8,
        note = 9,
        owner = 10,
        cost = 11,
        po_number = 12,
        description = 13,
        invoice_number = 14,
        serial_number = 15,
        asset_tag_number = 16,
        acquired_date = 17,
        disposed_date = 18,
        verified_date = 19,
        created_date = 20,
        created_by = 21,
        modified_date = 22,
        modified_by = 23
    }
}

编辑:

感谢 João Silva 的建议,这些字段现在在适当的 selected 列中出现在它们自己的单独单元格中:

代码:

    [HttpPost]
    public ActionResult ExportUsingEPPlus(ExportAssetsViewModel model)
    {
        ExcelPackage package = new ExcelPackage();
        var ws = package.Workbook.Worksheets.Add("TestExport");

        var exportFields = new List<string>();
        foreach (var selectedField in model.SelectedFields)
        {
            // Adds selected fields to [exportFields] List<string>
            exportFields.Add(model.ListOfExportFields.First(s => s.Key == selectedField).Value);
        }

       {
            ws.Cells[1, i + 1].Value = exportFields[i].ToString();
        }

        var membersToShow = typeof(INV_Assets).GetMembers().Where(p => exportFields.Contains(p.Name)).ToArray();

        ws.Cells["A2"].LoadFromCollection(_db.INV_Assets.ToList(), false, TableStyles.None, BindingFlags.Default, membersToShow);

        var memoryStream = new MemoryStream();
        package.SaveAs(memoryStream);

        string fileName = "Exported-InventoryAssets-" + DateTime.Now + ".xlsx";
        string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

        memoryStream.Position = 0;
        return File(memoryStream, contentType, fileName);
    }

输出:

我仍然遇到的唯一问题是模型中的 ForeignKey 字段。例如,[Status] 在我的 INV_Assets 模型中定义为:

    [Required]
    public int Status_Id { get; set; }
    [ForeignKey("Status_Id")]
    public virtual INV_Statuses Status { get; set; }

当当前导出执行时,excel 中的 [Status] 列在每个单元格中都包含 InventoryTracker.Models.INV_Statuses,而不是说 AVAILABLE/RECYCLED对于每条导出的记录。

任何人都可以深入了解如何不仅导出直接 INV_Assets 字段,而且 Model, Location, Status, Vendor, Manufacturer, and TypeForeignKey 值也导出到 Excel 中吗?

您不需要 DynamicQuery。您可以在 LoadFromCollection 中指定要显示的字段。 我没有编译这个(因为这是你的代码)但它在我的机器上用假数据工作。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;
    using InventoryTracker.DAL;
    using OfficeOpenXml;
    using InventoryTracker.Models;
    using System.Reflection;
    using OfficeOpenXml.Table;


    namespace InventoryTracker.Controllers
    {
        public class ExportController : Controller
        {
            private InventoryTrackerContext _db = new InventoryTrackerContext();

            [HttpPost]
            public ActionResult ExportUsingEPPlus(ExportAssetsViewModel model)
            {
                //FileInfo newExcelFile = new FileInfo(output);
                ExcelPackage package = new ExcelPackage();
                var ws = package.Workbook.Worksheets.Add("TestExport");

                var exportFields = new List<string>();
                foreach (var selectedField in model.SelectedFields)
                {
                    // Adds selected fields to [exportFields] List<string>
                    exportFields.Add(model.ListOfExportFields.First(s => s.Key == selectedField).Value);
                }

                // Loops to insert column headings into Row 1 of Excel
                for (int i = 0; i < exportFields.Count(); i++)
                {
                    ws.Cells[1, i + 1].Value = exportFields[i].ToString();
                }

                var membersToShow = typeof(INV_Asset).GetMembers()
                    .Where(p => exportFields.Contains(p.Name))
                    .ToArray();

                ws.Cells["A2"].LoadFromCollection(_db.INV_Assets.ToList(), false, TableStyles.None, BindingFlags.Default, membersToShow);


                var memoryStream = new MemoryStream();
                package.SaveAs(memoryStream);

                string fileName = "Exported-InventoryAssets-" + DateTime.Now + ".xlsx";
                string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

                memoryStream.Position = 0;
                return File(memoryStream, contentType, fileName);
            }
        }
    }

在我想要的每个模型上添加了一个 ToString() 覆盖以确保值通过而不仅仅是对象类型:

public class INV_Models
{

    public override string ToString()
    {
        return this.model_description;
    }
}

public class INV_Manufacturers
{
    public override string ToString()
    {
        return this.manufacturer_description;
    }
}

public class INV_Locations
{
    public override string ToString()
    {
        return this.location_dept + "|" + this.location_room;
    }
}

public class INV_Vendors
{
    public override string ToString()
    {
        return this.vendor_name;
    }
}

public class INV_Types
{
    public override string ToString()
    {
        return this.type_description;
    }
}

public class INV_Statuses
{
    public override string ToString()
    {
        return this.status_description;
    }
}

ExportController:

    [HttpPost]
    public ActionResult ExportUsingEPPlus(ExportAssetsViewModel model)
    {
        ExcelPackage package = new ExcelPackage();
        var ws = package.Workbook.Worksheets.Add("TestExport");

        var exportFields = new List<string>();
        foreach (var selectedField in model.SelectedFields)
        {
            // Adds selected fields to [exportFields] List<string>
            exportFields.Add(model.ListOfExportFields.First(s => s.Key == selectedField).Value);
        }

        // Loops to insert column headings into Row 1 of Excel
        for (int i = 0; i < exportFields.Count(); i++)
        {
            ws.Cells[1, i + 1].Value = exportFields[i].ToString();
        }

        var membersToShow = typeof(INV_Assets).GetMembers().Where(p => exportFields.Contains(p.Name)).ToArray();

        ws.Cells["A2"].LoadFromCollection(_db.INV_Assets.ToList(), false, TableStyles.None, BindingFlags.Default, membersToShow);



        var membersToShow = typeof(INV_Assets).GetMembers().Where(p => exportFields.Contains(p.Name)).ToArray();

        var memoryStream = new MemoryStream();
        package.SaveAs(memoryStream);

        string fileName = "Exported-InventoryAssets-" + DateTime.Now + ".xlsx";
        string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

        memoryStream.Position = 0;
        return File(memoryStream, contentType, fileName);
    }

并输出Excel文件: