簡體   English   中英

如何檢查哪些單元格屬於合並單元格並使用 c# Excel Interop 獲取合並單元格的值?

[英]How do I check which cells belong to a merged cell and get the merged cell's value using c# Excel Interop?

如何將 3 列數據的列表創建為 List<List> data = new() ,其中合並了一些單元格? 例如,我想提取數據如下:

List<List<string>> data = new();

data.Add(new() List<string> {"something", "418", "The value"});
data.Add(new() List<string> {"something1", "400", "The value"});
...

等等。 問題出現在合並的單元格中——我怎樣才能得到合並單元格的計數及其值? 如何獲取合並單元格范圍內的項目(某物、418、某物 1、400 等?

如果有更好的技術來提取數據(肯定有),那么收到一個好的提示真的很有幫助。

非常感謝任何答案。

包含要提取的數據的圖像

要合並行號:

int iRow, iCol;
iRow=1;
iCol=3;
int Num=oSheet.Cells(iRow, iCol).MergeArea.Rows.Count;

要獲得 Cell merged 的值:

string val=oSheet.Cells(iRow, iCol).MergeArea.Cells(1,1).Value;

要獲得合並單元格的第一行:

 int iRow1=oSheet.Cells(iRow, iCol).MergeArea.Cells(1,1).row;

如果 iRow,= iRow1 ,它是行合並到 iRow1

閱讀您的 excel: 例如:

For int iRow = 1 To oSheet.UsedRange.Rows.Count
   string val1=oSheet.Cells(iRow, 1).Value;
   string val2=oSheet.Cells(iRow, 2).Value;
   string val3=oSheet.Cells(iRow, 3).MergeArea.Cells(1,1).Value;   
   //Add value to List
   
Next

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM