2

We are developing web api from where excel will be downloaded with data. While searching on the net we found libraries like npoi, epplus, closedxml.

Do we really need to use these libraries to work with excel or go with the standard approach?

We are using asp.net core for web api development.

Edit: Basically our front end is in angular 5 from where we are exposing the web api. In web api we have written logic to get data and after getting data we need to place in certain format/template provided(Cell, Column wise, sheet wise etc.). There are quite a number of rows which we need to export in excel.

Also our database and apis are azure based.

Any help on this appreciated !

13
  • 1
    "Do we really need to use these libraries to work with excel" Yes. Or you could use excel interop, but then you'd need excel on the webserver, and interop isn't very scalable. Also, what is "standard approach"? It sounds... too good to be real ;) Commented Aug 31, 2018 at 13:32
  • It also depends on whether you are targeting .NET Framework or .NET Core Commented Aug 31, 2018 at 13:33
  • 1
    what do you define as the "standard approach" exactly? Those libraries are reasonable choices for working with and manipulating Excel files. If you just want to download excel files that are already created then you don't need any of that. You haven't really quite defined the requirements and process clearly enough for us to give you a proper answer. Commented Aug 31, 2018 at 13:33
  • 1
    @XamDev the code in that link creates a HTML document, not an Excel document. You might give it an .xlsx extension when it's downloaded and certainly Excel can read a HTML file, but it's not an Excel file. It can't contain things like formulas, macros etc. Maybe it's sufficient for your requirements, but if you want to create something which is actually in Excel format and can make use of Excel-specific features then you need to use another library. And I wouldn't really call that approach "standard" in any way. It's just one way to do a particular task. Commented Aug 31, 2018 at 13:55
  • 1
    So let's split that process into parts. 1) Create the file from a template and 2) populate with data - assuming you're going to make a real Excel file and not a HTML doc then both of these would be handled by the Excel code library e.g. NPOI or whatever you like. 3) Then, assuming you can get the content as binary from the library, you can stream the file content for download using a standard approach like memorystream. Any tutorial showing how to download a file in .NET should give you the idea. If you can't get binary stream direct from the library you might have to save to a temp file first Commented Aug 31, 2018 at 14:43

2 Answers 2

4

I have used epplus, and i think it works well for this scenario. Let me give you an example. Exporting Data

private ExcelPackage CreateDoc(string title, string subject, string keyword)
    {
        var p = new ExcelPackage();
        p.Workbook.Properties.Title = title;
        p.Workbook.Properties.Author = "Application Name";
        p.Workbook.Properties.Subject = subject;
        p.Workbook.Properties.Keywords = keyword;
        return p;
    }

public ExcelPackage getApplicantsStatistics()
    {
        ExcelPackage p = CreateDoc("Applicant Statistics", "Applicant statistics", "All Applicants");
        var worksheet = p.Workbook.Worksheets.Add("Applicant Statistics");

        //Add Report Header
        worksheet.Cells[1, 1].Value = "Applicant Statistics";
        worksheet.Cells[1, 1, 1, 3].Merge = true;

      //Get the data you want to send to the excel file
        var appProg = _unitOfWork.ApplicantsProgram
                        .AllIncluding(pr => pr.Program1)
                        .GroupBy(ap => ap.Program1.Name)
                        .Select(ap => new { programName = ap.Key, TotalNum = ap.Count() })
                        .ToList();
        //First add the headers
        worksheet.Cells[2, 1].Value = "SR No";
        worksheet.Cells[2, 2].Value = "Program";
        worksheet.Cells[2, 3].Value = "No. of Applicants";

        //Add values
        var numberformat = "#,##0";
        var dataCellStyleName = "TableNumber";
        var numStyle = p.Workbook.Styles.CreateNamedStyle(dataCellStyleName);
        numStyle.Style.Numberformat.Format = numberformat;

        for (int i = 0; i < appProg.Count; i++)
        {
            worksheet.Cells[i + 3, 1].Value = i + 1;
            worksheet.Cells[i + 3, 2].Value = appProg[i].programName;
            worksheet.Cells[i + 3, 3].Value = appProg[i].TotalNum;
        }
        // Add to table / Add summary row
        var rowEnd = appProg.Count + 2;
        var tbl = worksheet.Tables.Add(new ExcelAddressBase(fromRow: 2, fromCol: 1, toRow: rowEnd, toColumn: 3), "Applicants");
        tbl.ShowHeader = true;
        tbl.TableStyle = TableStyles.Dark9;
        tbl.ShowTotal = true;
        tbl.Columns[2].DataCellStyleName = dataCellStyleName;
        tbl.Columns[2].TotalsRowFunction = RowFunctions.Sum;
        worksheet.Cells[rowEnd, 3].Style.Numberformat.Format = numberformat;

        // AutoFitColumns
        worksheet.Cells[2, 1, rowEnd, 3].AutoFitColumns();
        return p;
    }

The returned ExcelPackage object can be sent as a download to the File with MVC

byte[] reportBytes;
            using (var package = _excelRep.getApplicantsStatistics())
            {
                reportBytes = package.GetAsByteArray();
            }
            return File(reportBytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
Sign up to request clarification or add additional context in comments.

1 Comment

nice, but one small detail - OP is using Web API from .NET Core, not MVC, so the last bit about return File won't apply.
1

There are several good libraries for doing so, my favorite ones are EPPlus and OpenXML by Microsoft

https://github.com/JanKallman/EPPlus

https://learn.microsoft.com/en-us/office/open-xml/open-xml-sdk

There is not much difference what your db and frontend are as everything is organized by the backend.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.