3

我正在尝试通过 open xml sdk 读取具有以下格式的 excel 电子表格,并确定哪些第一行合并单元格对应于第二行。

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

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

2 回答 2

3
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();
}
于 2013-01-21T06:24:39.523 回答
2

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

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

检查将返回真或假

伊丽莎白

于 2014-05-08T06:06:03.033 回答