1

I use EPPlus version 4.0.4 to create an Excel file within an ASP.net MVC5 website and I simply don't know how to add a new rows and columns after completing the very first row.

It's difficult to explain clearly so let's me show you my code. It's easy enough to understand.

    var products = new DataTable("1km subscriptions");

    // SO HERE THE FIRST COLUMS with ROWS INFORMATION
    products.Columns.Add("", typeof(int));
    products.Columns.Add("Full Name", typeof(string));
    products.Columns.Add("BirthDate", typeof(string));

    int i = 0;
    foreach (var item in GetData())
    {
        // Primary user info
        products.Rows.Add(i, item.FullName, item.BirthDate);
        i++;

        // HERE I HAVE TO GO TO NEXT LIGNE NUMBER AND WRITE TWO COLUMNS AND ROWS INFO
//products.NewRow();
        products.Columns.Add("ORDER #", typeof(int));
        products.Columns.Add("BUY DATE", typeof(string));

        foreach (var order in item.OrderViewModels)
        {
            products.Rows.Add(order.Id, order.CreatedDate);

//products.NewRow();
            // THEN, I HAVE TO DO THE SAME THING. GO TO THE NEXT LIGNE AND ADD TWO OTHERS COLUMNS WITH INFO
            products.Columns.Add("PRODUCT NAME", typeof(string));
            products.Columns.Add("Quantity", typeof(int));
            foreach (var osku in order.OrderSKUViewModels)
            {
                products.Rows.Add(osku.SKUName, osku.Quantity);
            }
        }
    }

    using (ExcelPackage pck = new ExcelPackage())
    {
        ExcelWorksheet ws = pck.Workbook.Worksheets.Add("FIRST TAB NAME");
        ws.Cells["A1"].LoadFromDataTable(products, true)
        // Write it back to the client
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;  filename=ExcelDemo.xlsx");
        Response.BinaryWrite(pck.GetAsByteArray());
        Response.Flush();
        Response.End();
    }

At first, I was thinking the products.NewRow() will do the job but it's not the case. I don't find an easy we to do what that easy operation. I would like to avoid to do major changes to the code for something like that o_O

Anyone can help ?

David

Edit

I can't add a picture but I can share the HTML code I use to display the data. I try to reproduce the same architecture with Excel. So we can understand that I thought that product.NewLine() could produce a kind of line change... :

<table class="table table-hover" style="border: 1px solid #ddd;">
            @foreach (var item in Model)
            {
                <tr class="success">
                    <th style="width:122px;">Full Name</th>
                    <th style="width:118px;">BirthDate</th>
                </tr>
                <tr class="active">
                    <td>@item.FullName</td>  
                    <td>@item.BirthDate</td>
                </tr>
                foreach (var order in item.OrderViewModels)
                {
                    <tr>
                        <td><b>Commande:</b></td>
                        <td><b>#</b>&nbsp;@order.Id</td>
                        <td colspan="6"><b>Buy date:</b>&nbsp;@order.CreatedDate</td>
                    </tr>
                        foreach (var osku in order.OrderSKUViewModels)
                        {
                        <tr>
                            <td colspan="3"><b>Product name:</b>&nbsp;@osku.SKUName</td>
                            <td colspan="5"><b>Quantity:</b>&nbsp;@osku.Quantity</td>
                        </tr>
                        }
                    <tr>
                        @if (order.GuestViewModels.Count() != 0)
                        {
                            <th colspan="8" class="success">Guests</th>
                        }
                    </tr>

                    if (order.GuestViewModels.Count() > 0)
                    {
                        <tr class="warning">
                            <th style="width:122px;">FullName</th>
                            <th style="width:118px;">BirthDate</th>
                        </tr>
                    }
                    foreach (var guest in order.GuestViewModels)
                    {
                        <tr style="border-top: 2px double  #0094ff;border-left:2px solid #0094ff;border-right:2px solid #0094ff">
                            <td>@guest.FullName</td>
                            <td>@guest.BirthDate</td>
                        </tr>
                    }
                }
            }
        </table>
David Létourneau
  • 1,250
  • 2
  • 19
  • 39
  • 1
    A `DataTable` is a single set of columns and a single set of rows. It looks to me like you're adding two new columns for *every order*, which means you'll have duplicates of those two columns. You would be better off creating all your columns at the beginning, and then only populating them when you need to. If you need multiple related tables, you should use a `DataSet` containing multiple DataTables with relations between them. – Ryan Lundy Jan 02 '16 at 22:12
  • Nice, I will try with DataSet then. – David Létourneau Jan 02 '16 at 22:18
  • Did you know how I can use the DataSet with LoadFromDataTable method ? I'm able to build the file but I only have one ligne of data even if I increment cells position. – David Létourneau Jan 02 '16 at 23:03
  • It's hard to say without knowing exactly what you intend the data to look like. It makes sense that the library is based on a `DataTable`, because an Excel sheet is also one set of rows and columns. It looks like you're trying to represent hierarchical data. There are ways to do that in a spreadsheet, but the data still needs to go in one sheet. Again, I'd set up your columns first, and then consider how to load your data based on those columns. Can you edit to include a picture of how you intend the data to look in the spreadsheet? – Ryan Lundy Jan 03 '16 at 03:13
  • I shared the HTML structure I use. It's almost the same I try to do by exporting the data in Excel. – David Létourneau Jan 03 '16 at 13:48
  • did you use EPPLUs and LoadFromDataTable method ? – Kiquenet Jul 25 '18 at 08:43

0 Answers0