【问题标题】:.Net Core Export excell.Net Core 导出excel
【发布时间】:2020-11-18 11:21:00
【问题描述】:

您好,我正在尝试使用 ClosedXml 包进行导出和返回 Excel 函数。它在演示项目上运行良好。我必须为原始项目制作它,所以这是代码。这是一个 IActionResult

 public class HomeController : Controller
    {
        private List<Employee> employees = new List<Employee>
        {
            new Employee{ Id=1,Name="Jr.Dev",Role="Dev"},
            new Employee{ Id=2,Name="Jr.Dev2",Role="Dev2"},
            new Employee{ Id=3,Name="Jr.Dev3",Role="Dev3"},
            new Employee{ Id=4,Name="Jr.Dev4",Role="Dev4"},
            new Employee{ Id=5,Name="Jr.Dev5",Role="Dev5"},
        };



        public IActionResult Index()
        {

            return Excel();
        }
        public IActionResult Excel()
        {
            using (var workbook = new XLWorkbook())
            {

                var worksheet = workbook.Worksheets.Add("Employees");

                var currentRow = 1;

                worksheet.Cell(currentRow, 1).Value = "Id";
                worksheet.Cell(currentRow, 2).Value = "Name";
                worksheet.Cell(currentRow, 3).Value = "Role";


                foreach (var item in employees)
                {
                    currentRow++;
                    worksheet.Cell(currentRow, 1).Value = item.Id;
                    worksheet.Cell(currentRow, 2).Value = item.Name;
                    worksheet.Cell(currentRow, 3).Value = item.Role;

                }
                using (var stream = new MemoryStream())
                {
                    workbook.SaveAs(stream);
                    var content = stream.ToArray();
                    return File(content, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Employee.xlsx");
                }
            }
        }

    }

真的很好用,还能下载 xlsx 文件。

我只想从导出 Excel 之类的视图中调用并使用视图模型调用它。我怎样才能用最佳实践来实施它。谢谢

【问题讨论】:

  • 这个函数单独工作很好,但是如何让它可以调用
  • “最佳”是什么意思? “最佳”的定义因人而异。
  • 嗨@pandakun,我的回答是否帮助您解决了您的问题?如果可以,请您接受作为答案吗?如果没有,请您跟进让我知道吗?参考:How to accept as answer .谢谢。

标签: asp.net-core closedxml


【解决方案1】:

这是一个工作演示:

索引.cshtml:

使用@Html.DisplayFor 显示列表数据,隐藏的输入可以发布到操作中。

@model IEnumerable<Employee>
    
<h1>Index</h1>

<form asp-action="Excel" asp-controller="Home">
    <input type="submit" value="Export" class="btn btn-primary" />
    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Name)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Role)
                </th>
            </tr>
        </thead>
        <tbody>
            @for (var i = 0; i < Model.Count(); i++)
            {
                <input hidden asp-for="@Model.ElementAt(i).Id" name="[@i].Id" />
                <tr>
                    <td>
                        @Html.DisplayFor(x => x.ElementAt(i).Name)
                        <input hidden asp-for="@Model.ElementAt(i).Name" name="[@i].Name" />
                    </td>
                    <td>
                        @Html.DisplayFor(x => x.ElementAt(i).Role)
                        <input hidden asp-for="@Model.ElementAt(i).Role" name="[@i].Role" />
                    </td>
                </tr>
            }

        </tbody>
    </table>

</form>

控制器:

public class HomeController : Controller
{
    public IActionResult Index()
    {
        //for easy testing,I hard coded the data
        List<Employee> employees = new List<Employee>
        {
            new Employee{ Id=1,Name="Jr.Dev",Role="Dev"},
            new Employee{ Id=2,Name="Jr.Dev2",Role="Dev2"},
            new Employee{ Id=3,Name="Jr.Dev3",Role="Dev3"},
            new Employee{ Id=4,Name="Jr.Dev4",Role="Dev4"},
            new Employee{ Id=5,Name="Jr.Dev5",Role="Dev5"},
        };
        //you could also get the data from database
        //var employees = _context.Employee.ToList());
       
        return View(employees);  //render the employees in the Index.cshtml
    }
    
    [HttpPost]
    public IActionResult Excel(List<Employee> employees)//receive the list from view
    {
        using (var workbook = new XLWorkbook())
        {

            var worksheet = workbook.Worksheets.Add("Employees");

            var currentRow = 1;

            worksheet.Cell(currentRow, 1).Value = "Id";
            worksheet.Cell(currentRow, 2).Value = "Name";
            worksheet.Cell(currentRow, 3).Value = "Role";


            foreach (var item in employees)
            {
                currentRow++;
                worksheet.Cell(currentRow, 1).Value = item.Id;
                worksheet.Cell(currentRow, 2).Value = item.Name;
                worksheet.Cell(currentRow, 3).Value = item.Role;

            }
            using (var stream = new MemoryStream())
            {
                workbook.SaveAs(stream);
                var content = stream.ToArray();
                return File(content, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Employee.xlsx");
            }
        }
    }
}

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多