Home > Backend Development > C++ > How to Efficiently Export Data to Excel from ASP.NET MVC using EPPlus?

How to Efficiently Export Data to Excel from ASP.NET MVC using EPPlus?

Linda Hamilton
Release: 2025-01-06 02:18:40
Original
873 people have browsed it

How to Efficiently Export Data to Excel from ASP.NET MVC using EPPlus?

How to Export Data to Excel Effectively in ASP.NET MVC

The provided guide attempts to export data to Excel using a less efficient HTML-based approach. To create a proper Excel file, it's recommended to utilize a library like EPPlus.

EPPlus Library

EPPlus provides the LoadFromDataTable and LoadFromCollection methods to fill an Excel range with data. Here's how to implement it in your code:

Code:

using (ExcelPackage package = new ExcelPackage())
{
    var ws = package.Workbook.Worksheets.Add("My Sheet");
    //true generates headers
    ws.Cells["A1"].LoadFromDataTable(dataTable, true);
}
Copy after login

By default, the column or property names will be used as headers. You can also apply formatting, table styles, and more using the EPPlus library.

Saving to a File

To save the Excel file, create a custom FileResult called EpplusResult:

Code:

public class EpplusResult:FileResult
{
    public EpplusResult(ExcelPackage package) : base("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    {
        Package = package;
    }

    public ExcelPackage Package { get; private set; }
}
Copy after login

This allows you to write the following action:

Code:

public FileResult ExportData()
{
    ExcelPackage package = new ExcelPackage();
    var ws = package.Workbook.Worksheets.Add("My Sheet");       
    // Load data to the worksheet
    ws.Cells[1, 1].LoadFromDataTable(table, true, TableStyles.Light1);

    return new EpplusResult(package) { FileDownloadName = "SomeFile.xlsx" };
}
Copy after login

Bonus: Exporting Paged Data

To export paged data with PagedList, you can modify the action:

Code:

public ActionResult ExportData()
{
    var pagedList = db.Customers.ToPagedList(pageNumber, pageSize);

    ExcelPackage package = new ExcelPackage();
    var ws = package.Workbook.Worksheets.Add("My Sheet");       
    ws.Cells[1, 1].LoadFromCollection(pagedList, true, TableStyles.Light1);

    return new EpplusResult(package) { FileDownloadName = "Customers.xlsx" };
}
Copy after login

The above is the detailed content of How to Efficiently Export Data to Excel from ASP.NET MVC using EPPlus?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template