我遇到以下問題。第一部分代碼通過將所有不同的值拉入列表框供用戶查看。但是,當數據被過濾時,它仍然拉入所有不是我想要的隱藏值。當我試圖只將可見細胞拉入範圍時,它打破了我的範圍轉換爲不同的列表。關於爲什麼打破範圍打破這個任何想法? System.Array myvalues行代碼中斷行「Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:無法將類型'字符串'轉換爲'System.Array'」。對我來說似乎很奇怪。非線性Excel範圍C中的不同列表#
工作代碼:
Range values = sht.Range[cell1, cell2];
System.Array myvalues = (System.Array)values.Cells.Value;
string[] listValues = myvalues.OfType<object>().Select(o => o.ToString()).ToArray();
string[] listValues2 = listValues.Distinct().ToArray();
斷碼:
Range values = sht.Range[cell1, cell2].SpecialCells(XlCellType.xlCellTypeVisible);
System.Array myvalues = (System.Array)values.Cells.Value;
string[] listValues = myvalues.OfType<object>().Select(o => o.ToString()).ToArray();
string[] listValues2 = listValues.Distinct().ToArray();
編輯:
工作的代碼來處理時,範圍是隱藏的,當它不是:
var extractedFromSheet = new List<object>();
foreach (Range area in values.Areas)
{
var areaValue = area.Value;
if (areaValue is Array) // The area contains multiple cells
{
var arr = (Array)areaValue;
extractedFromSheet.AddRange(arr.OfType<object>().Select(o => o .ToString()));
}
else // The area contains one cell
{
extractedFromSheet.Add(areaValue);
}
}
var distinct = extractedFromSheet.Distinct();
無法重現錯誤。我所看到的是當範圍的某些部分被過濾器隱藏時,範圍被劃分爲'Areas'。但是這不會導致錯誤,但是'Value'只返回第一個'Area'。 – dee