1

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

起初,我认为 products.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>
4

0 回答 0