打开xml sdk电子表格检查合并的单元格

时间:2012-09-21 15:53:12

标签: .net openxml spreadsheet openxml-sdk

我正在尝试使用以下格式通过打开的xml sdk读取excel电子表格,并确定第一行合并的单元格是否存在第二行。

如何检查单元格是否处于合并状态?

------------------------------------------------------------
       Main Heading 1     |     Main heading 2              |
------------------------------------------------------------
Sub Head 1   | sub head 2 |     sub head 3                  |
-------------------------------------------------------------
             |            |                                 |

2 个答案:

答案 0 :(得分:2)

        const string testFileName = @"C:\Users\koo\Desktop\test\test\Test.xlsx";
        string  sheetName = "Sheet1";
        using (SpreadsheetDocument document = SpreadsheetDocument.Open(testFileName, true))
        {
            WorkbookPart wbPart = document.WorkbookPart;

            Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sheetName).FirstOrDefault();

            WorksheetPart wsPart = (WorksheetPart)(wbPart.GetPartById(theSheet.Id));

            MergeCells mcs = wsPart.Worksheet.Descendants<MergeCells>().First();

            var mcl= mcs.ChildElements.ToList();
            foreach (MergeCell mc in mcl)
            {

            }
            document.WorkbookPart.Workbook.Save();
        }

答案 1 :(得分:1)

如果您使用的是ClosedXml库,那么要检查单元格是否合并,您可以使用下面给出的代码。

XLWorkbook excelWorkBook = null;
excelWorkBook = new XLWorkbook(filePath);
var ws = excelWorkBook.Worksheet("Sheet1"); 
bool check= ws.Cell("Cell Name").IsMerged();

检查将返回true或false

伊丽莎白