0

ユーザーがボタンをクリックしたときに、コントローラから.xlsxドキュメントをエクスポートしようとしています。私のモデルは次のようになります。.NETエクスポートをExcelにエクスポートする - リストのプロパティが表示されない

public class ExportOrdersViewModel 
{ 
    public int Id { get; set; } 
    public string CustomerFirstName { get; set; } 
    public string CustomerLastName { get; set; } 
    public string CustomerEmail { get; set; } 
    public string CustomerMobile { get; set; } 
    public string ShippingStreet { get; set; } 
    public string ShippingCity { get; set; } 
    public string ShippingCountry { get; set; } 
    public string ShippingPostalCode { get; set; } 
    public string ShippingTo { get; set; } 
    public bool IsShipped { get; set; } 
    public ICollection<ExportOrdersItemViewModel> Items { get; set; } 

} 

public class ExportOrdersItemViewModel 
{ 
    public string TicketName { get; set; } 
    public string EventName { get; set; } 
    public int Quantity { get; set; } 
    public string CurrencyId { get; set; } 
    public string PaymentCurrencyId { get; set; } 
    public string UnitPrice { get; set; } 
    public string PaymentUnitPrice { get; set; } 
} 

ExportOrdersViewModelには、文字列のプロパティ以外にも、アイテムのリストが含まれています。ファイルが作成されると、行が作成されたときにこのリストをセルに表示します。

私はファイルを作成するためにClosedXMLを使用しています。ファイルが作成される前に、コントローラでは、すべてのExportOrdersViewModelプロパティを含むDataTableと、ExportOrdersItemViewModelのプロパティを含む別のDataTableを参照する行が作成されました。 Parrent DataTableは、これを受けてブックに渡されます。ここで

あなたは、私はそれをやっている方法を参照してくださいすることができます

私は注文のDataTableが、それは対応する項目のDataTableへの参照が含まれているデバッグモードで確認してください、私は合格したときには、ブックにある
 DataTable orders = new DataTable(); 

     orders.Columns.Add("OrderID", typeof(string)); 
     orders.Columns.Add("Items", typeof(DataTable)); 
     orders.Columns.Add("IsShipped", typeof(bool)); 

     DataTable items = new DataTable(); 
     items.Columns.Add("TicketName", typeof(string)); 
     items.Columns.Add("Quantity", typeof(int)); 
     items.Columns.Add("Currency", typeof(string)); 


     exportModels.ForEach(o => orders.Rows.Add(
              o.Id, 
              (DataTable)GetItems(o.Items), 
              o.IsShipped 
              )); 

、ファイル項目の行は空です。ここで

は、私は、ファイルを作成しています方法です:

XLWorkbook workbook = new XLWorkbook(); 
     workbook.Worksheets.Add(orders, "Orders"); 

     Response.Clear(); 
     Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
     Response.AddHeader("content-disposition", "attachment;filename=\"HelloWorld.xlsx\""); 

     using (MemoryStream memoryStream = new MemoryStream()) 
     { 
      workbook.SaveAs(memoryStream); 
      memoryStream.WriteTo(Response.OutputStream); 
      memoryStream.Close(); 
     } 

     Response.End(); 

これは、出力ファイルとPrintScreenを次のとおりです。enter image description here

と私はそれがこのように見えるようにしたい:でhttps://aspblogs.blob.core.windows.net/media/muhanadyounis/Media/linqParentChild_615EF617.jpg

+0

をhttps://aspblogs.blob.core.windows.net/media/muhanadyounis/Media/linqParentChild_615EF617.jpgは、Excelのスクリーンショットですか?それはそうではありません。 –

+0

そうではありません。私はそのように見せたかった –

+0

Excelでは不可能です。 –

答えて

0

最後に、私はこれに適した解決策を見つけました。私はワークシートに毎回新しいテーブルを追加しています。ここで私はそれをやった方法です:

//HELPER METHODS 
    private DataTable GetItems(ICollection<ExportOrdersItemViewModel> list) 
    { 
     var items = new DataTable(); 

     items.Columns.Add(Resources.Names.OrderTicketName, typeof(string)); 
     items.Columns.Add(Resources.Names.OrderEventName, typeof(string)); 
     items.Columns.Add(Resources.Names.Quantity, typeof(int)); 
     items.Columns.Add(Resources.Names.Currency, typeof(string)); 
     items.Columns.Add(Resources.Names.PaymentCurrencyId, typeof(string)); 
     items.Columns.Add(Resources.Names.UnitPrice, typeof(string)); 
     items.Columns.Add(Resources.Names.PaymentUnitPrice, typeof(string)); 
     list.ForEach(i => items.Rows.Add(i.TicketName, i.EventName, i.Quantity, i.CurrencyId, i.PaymentCurrencyId, i.UnitPrice, i.PaymentUnitPrice)); 

     return items; 
    } 

    private void SetOrdersProperties(DataTable dataTable) 
    { 
     dataTable.Columns.Add(Resources.Names.OrderId, typeof(string)); 
     dataTable.Columns.Add(Resources.Names.OrderDate, typeof(string)); 
     dataTable.Columns.Add(Resources.Names.InvoicedDate, typeof(string)); 
     dataTable.Columns.Add(Resources.Names.CustomerFirstname, typeof(string)); 
     dataTable.Columns.Add(Resources.Names.CustomerLastName, typeof(string)); 
     dataTable.Columns.Add(Resources.Names.CustomerEmail, typeof(string)); 
     dataTable.Columns.Add(Resources.Names.CustomerPhone, typeof(string)); 
     dataTable.Columns.Add(Resources.Names.ShipTo, typeof(string)); 
     dataTable.Columns.Add(Resources.Names.ShippingStreet, typeof(string)); 
     dataTable.Columns.Add(Resources.Names.City, typeof(string)); 
     dataTable.Columns.Add(Resources.Names.ShippingPostalCode, typeof(string)); 
     dataTable.Columns.Add(Resources.Names.ShippingCountry, typeof(string)); 
     dataTable.Columns.Add(Resources.Names.IsShipped, typeof(string)); 
    } 

    private void SetItemsProperties(DataTable dataTable) 
    { 
     dataTable.Columns.Add(Resources.Names.OrderTicketName, typeof(string)); 
     dataTable.Columns.Add(Resources.Names.OrderEventName, typeof(string)); 
     dataTable.Columns.Add(Resources.Names.Quantity, typeof(int)); 
     dataTable.Columns.Add(Resources.Names.Currency, typeof(string)); 
     dataTable.Columns.Add(Resources.Names.PaymentCurrencyId, typeof(string)); 
     dataTable.Columns.Add(Resources.Names.UnitPrice, typeof(string)); 
     dataTable.Columns.Add(Resources.Names.PaymentUnitPrice, typeof(string)); 
    } 

私はアイテムとのDataTableを作成するために、これらのメソッドを使用して注文とアイテムのためtableHeadersを設定します。コードの次の部分で は私が私のファイル作成方法です:

DataTable orders = new DataTable(); 
     SetOrdersProperties(orders); 

     DataTable items = new DataTable(); 
     SetItemsProperties(items); 

     XLWorkbook workbook = new XLWorkbook(); 
     var workSheet = workbook.Worksheets.Add("Orders"); 


     var index = 1; 
     exportModels.ForEach(o => 
          { 
           orders.Rows.Add(
              o.Id, 
              o.OrderDate, 
              o.InvoicedDate, 
              o.CustomerFirstName, 
              o.CustomerLastName, 
              o.CustomerEmail, 
              o.CustomerMobile, 
              o.ShippingTo, 
              o.ShippingStreet, 
              o.ShippingCity, 
              o.ShippingPostalCode, 
              o.ShippingCountry, 
              o.IsShipped ? "YES":"NO" 
              ); 
           workSheet.Cell(index, 1).InsertTable(orders); 
           index += orders.Rows.Count+1; 
           orders = new DataTable(); 
           SetOrdersProperties(orders); 
           workSheet.Cell(index, 2).InsertTable(GetItems(o.Items)); 
           workSheet.Cell(index, 1).Value = Resources.Names.Tickets; 
           workSheet.Range(index, 1, index + o.Items.Count,1).Merge(); 
           index += o.Items.Count()+1; 
           index++; 
          }); 

     workSheet.Tables.ForEach(t => t.ShowAutoFilter = false); 
     workSheet.Style.Alignment.Vertical = XLAlignmentVerticalValues.Center; 
     workSheet.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; 
     workSheet.Tables.ForEach(t => t.Theme = XLTableTheme.TableStyleLight13); 
関連する問題