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

时间:2017-09-02 09:32:31

标签: c# asp.net excel closedxml

我有多个带数据的数据表,我想在单页中添加空格。我正在使用ClosedXML来开发export excel实用程序。

2 个答案:

答案 0 :(得分:0)

我完成以下代码

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

答案 1 :(得分:0)

添加如何动态执行此操作(假设使用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;