【问题标题】:How to add multiple datatable to one worksheet using closedxml?如何使用 closedxml 将多个数据表添加到一个工作表?
【发布时间】:2017-09-02 09:32:30
【问题描述】:

我有多个包含数据的数据表,我想将它们添加到带有空格的单张表中。我正在使用 ClosedXML 开发导出 excel 实用程序。

【问题讨论】:

    标签: c# asp.net excel closedxml


    【解决方案1】:

    我已经完成了下面的代码

     wb.Worksheets.Add(dt);
     wb.Worksheet(1).Cell(5, 1).InsertTable(dt1);
    

    【讨论】:

      【解决方案2】:

      添加如何动态执行此操作(假设为 List):

      int currentRow = 1;//counter to keep track of what row we're on
      IXLWorksheet worksheet = wb.AddWorksheet(sheetName: settings.sheetName);
      foreach (DataTable table in tables)
      {
          //Use the table name, and add it to the first cell above the table before insert
          worksheet.Cell(currentRow, 1).Value = table.TableName;
          worksheet.Cell(currentRow, 1).Style.Font.FontSize = 20;
          worksheet.Cell(currentRow, 1).Style.Font.SetBold();
          currentRow++;
          //now that the title is inserted and formatted, insert table
          worksheet.Cell(currentRow, 1).InsertTable(table);
          currentRow += table.Rows.Count + 3;
      }
      //optional for adjusting columns to their contents and setting wrap text
      var cols = worksheet.Columns();
      cols.AdjustToContents();
      foreach(var a in cols)
      {//set mas width to 50
          a.Width = a.Width > 50 ? 50 : a.Width;
      }
      cols.Style.Alignment.WrapText = true;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-12
        • 2015-11-04
        • 2021-11-20
        • 2018-01-13
        • 2018-07-03
        • 2020-02-29
        相关资源
        最近更新 更多