如何使用 closedxml 将多个数据表添加到一个工作表?

How to add multiple datatable to one worksheet using closedxml?

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

我已完成以下代码

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

添加动态执行此操作的方法(假设一个 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;