question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Worksheet per item in List

See original GitHub issue

I’d like to create one worksheet per item from a list.

For example in case I pass an IEnumerable to the template template.AddVariable(customers); and the name of the template worksheet is something like {{ item.Name }} the report generator should iterate through the customers list and create one worksheet for every item in the list.

Another approach: passing a variable data that contains an IEnumerable<Customer> Customers template.AddVariable(data); and worksheet with label {{ Customers.Name }} should trigger the generation of a single worksheet per customer. tab template

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
fingers10commented, Aug 26, 2019

I was also having a similar requirement. This is how I ended up doing, I used the same excel template while generating multiple sheets. This way we dont need to rename ranges in our template.

private (bool, byte[]) GetExcelBytesFromTemplate<T>(string templatePath, ExcelReportModel<T> templateVariable, bool createSeparateSheets = false)
        {
            byte[] excelBytes = null;
            var result = true;

            var finaltemplate = new XLWorkbook(templatePath); // load your template
            finaltemplate.Worksheets.Delete(1); // delete the first sheet. Now excel is clean.

            try
            {
                if (createSeparateSheets) // if you need as separate sheet
                {
                    foreach (var record in templateVariable.Records) // your records
                    {
                        var template = new XLTemplate(templatePath); // loading same template again
                        templateVariable.Records = new List<T> { record };
                        template.AddVariable(templateVariable);
                        template.Generate();
                        template.Workbook.Worksheet(1).Name = record.ToString(); // name the sheet
                        template.Workbook.Worksheet(1).CopyTo(finaltemplate, record.ToString()); // copy to final excel
                    }
                }
                else // else is self explanatory as shown in copy except I copy to my final excel
                {
                    var template = new XLTemplate(templatePath);
                    template.AddVariable(templateVariable);
                    template.Generate();
                    var worksheet = template.Workbook.Worksheet(1);
                    worksheet.CopyTo(finaltemplate, worksheet.Name);
                }

                using (var stream = new MemoryStream())
                {
                    finaltemplate.SaveAs(stream);
                    excelBytes = stream.ToArray();
                }
            }
            catch (Exception e)
            {
                result = false;
            }

            return (result, excelBytes);
        }

Then use the excel bytes to download as excel,

public IActionResult OnGetExcelReport()
        {
            return new ExcelReportFromTemplate(excelBytes, fileName);
        }

ExcelReportFromTemplate Action Result:

public class ExcelReportFromTemplate : IActionResult
    {
        public ExcelReportFromTemplate(byte[] excelBytes, string fileName)
        {
            ExcelBytes = excelBytes;
            FileName = fileName;
        }

        public byte[] ExcelBytes { get; set; }
        public string FileName { get; set; }

        public async Task ExecuteResultAsync(ActionContext context)
        {
            context.HttpContext.Response.Headers["content-disposition"] =
                $"attachment; filename={FileName}{Constants.ExcelFileFormat}";

            await context.HttpContext.Response.Body.WriteAsync(ExcelBytes, 0, ExcelBytes.Length);
        }
    }

Hope this helps someone.

Thanks, Abdul

3reactions
b0bi79commented, Jul 15, 2019

I have long thought that someone would need this feature and need to do it. Unfortunately, this is not yet implemented. I will try to find time in the next couple of weeks and implement it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Manage Feature Requests [Template included]
The best way to categorize feature requests is by considering how they change the functionality of the product. A feature request either ...
Read more >
Feature Request Template: How to Collect User Feedback ...
A feature request template is a standardized form that allows users and customers to submit feature requests for your product.
Read more >
How to create the ultimate feature request list in Jira
Within your project you'll then have a Board, which will be the place that you'll be able to view and manage all the...
Read more >
How do I write a good feature request?
I've moved your question from the title to the body. It is bad form to write a post without any mention of the...
Read more >
Speed up development with a free feature request template
Get a free feature request template to help you gather product feedback throughout development for a fast and smooth process.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found