如何使用EPPlus C#和MVC添加新的行和列?

时间:2016-01-02 22:08:02

标签: c# asp.net export-to-excel epplus-4

我使用EPPlus 4.0.4版在ASP.net MVC5网站中创建一个Excel文件,在完成第一行之后,我根本不知道如何添加新的行和列。

很难清楚地解释,所以让我告诉你我的代码。这很容易理解。

    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();
    }

起初,我在想产品.NewRow()会做这个工作,但情况并非如此。我不容易找到一个简单的操作。我想避免对类似o_O

之类的代码进行重大更改

任何人都可以提供帮助吗?

大卫

修改

我无法添加图片,但我可以共享用于显示数据的HTML代码。我尝试用Excel重现相同的架构。所以我们可以理解我认为product.NewLine()可以产生一种换行......:

<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>

0 个答案:

没有答案