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> @order.Id</td>
<td colspan="6"><b>Buy date:</b> @order.CreatedDate</td>
</tr>
foreach (var osku in order.OrderSKUViewModels)
{
<tr>
<td colspan="3"><b>Product name:</b> @osku.SKUName</td>
<td colspan="5"><b>Quantity:</b> @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>