简体   繁体   English

如何让 closedxml.excel 识别合并的单元格?

[英]How do I get closedxml.excel to recognize merged cells?

I'm trying to make a template excel file and I need to put data at various parts of the file.我正在尝试制作模板 excel 文件,我需要将数据放在文件的各个部分。 I have 2 fields where the data I'm importing is from a list so in the cell I do something like this:我有 2 个字段,其中我要导入的数据来自列表,因此在单元格中我执行以下操作:

{Item.Name}

and I of course name the range of cells that will be populated by this list.我当然会命名将由该列表填充的单元格范围。 I have run into an issue where only the first record in my list will be of the correct format/ cell merge.我遇到了一个问题,我的列表中只有第一条记录的格式/单元格合并是正确的。 Every record after the first completely breaks down all of my merged cells so my formatting is not good.第一次之后的每条记录都完全分解了我所有的合并单元格,所以我的格式不好。 Any ideas of how to get closedxml.excel to recognize there are merged cells?关于如何让 closedxml.excel 识别出合并单元格的任何想法?

I don't know if there is a way to get only the merged cells, but you can check if a cell is merged:我不知道是否有办法只获取合并的单元格,但您可以检查一个单元格是否已合并:

using (var excelFileStream = new FileStream("excelfile.xlsx", FileMode.Open, FileAccess.Read))
{
    using IXLWorkbook workbook = new XLWorkbook(excelFileStream);

    IXLWorksheet worksheet = workbook.Worksheets.Worksheet(1);

    IXLCell cell = worksheet.Cell(row: 1, column: 1);
    IXLRangeAddress range = cell.MergedRange().RangeAddress;

    if (range.ColumnSpan > 1 || range.RowSpan > 1)
    {
        //merged cell
    }
    else
    {
        //non-merged cell
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM