获取非连续范围的值数组
本文关键字:数组 范围 连续 获取 | 更新日期: 2023-09-27 17:50:00
我希望能够获得一个数组值只是在工作表上的可见单元格。然而,Range.Value2
属性似乎只适用于连续的单元格。
什么将是一个有效的方法,获得可见的单元格值到一个多维数组?也许我们可以在Range.Areas
上做一些循环?
如果查看下面的示例代码,您将看到.Value2
属性只返回Range
中第一个Area
的值。
All Values:
String1 String2 String3 String4 String5
String6 String7 String8 String9 String10
String11 String12 String13 String14 String15
String16 String17 String18 String19 String20
String21 String22 String23 String24 String25
Visible Values:
String1 String2 String3 String4
String6 String7 String8 String9
_
public static void RunTest()
{
SetData();
HideRowsAndColumns();
Excel.Application excelApp = Globals.ThisAddIn.Application;
Excel.Worksheet sht = excelApp.ActiveSheet;
var usedRange = sht.UsedRange;
var visibleUsedRange = usedRange.SpecialCells(XlCellType.xlCellTypeVisible);
var usedRangeVals = (object[,])usedRange.Value2;
var visibleUsedRangeVals = (object[,]) visibleUsedRange.Value2; //this doesn't get the full set of values because Value2 does not work with non-contiguous ranges
string s = "All Values:'n'n" + ArrayToString(usedRangeVals) + "'n'nVisible Values:'n'n" +
ArrayToString(visibleUsedRangeVals);
MessageBox.Show(s);
}
private static string ArrayToString(object[,] vals)
{
int dim1Start = vals.GetLowerBound(0); //Excel Interop will return index-1 based arrays instead of index-0 based
int dim1End = vals.GetUpperBound(0);
int dim2Start = vals.GetLowerBound(1);
int dim2End = vals.GetUpperBound(1);
var sb = new StringBuilder();
for (int i = dim1Start; i <= dim1End; i++)
{
for (int j = dim2Start; j <= dim2End; j++)
{
sb.Append(vals[i, j]);
if (j != dim2End)
sb.Append("'t");
}
sb.Append("'n");
}
return sb.ToString();
}
private static void SetData()
{
const int rows = 5;
const int cols = 5;
var a = new string[rows, cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
a[i, j] = "String" + (i * cols + j + 1);
Excel.Application excelApp = Globals.ThisAddIn.Application;
Excel.Worksheet sht = excelApp.ActiveSheet;
Excel.Range rng = sht.Range[sht.Cells[1, 1], sht.Cells[rows, cols]];
rng.Value2 = a;
}
private static void HideRowsAndColumns()
{
Excel.Application excelApp = Globals.ThisAddIn.Application;
Excel.Worksheet sht = excelApp.ActiveSheet;
var row = (Excel.Range) sht.Rows[3];
row.Hidden = true;
var col = (Excel.Range) sht.Columns[5];
col.Hidden = true;
}
既然您不想使用我上面给您的链接中提到的任何方法,并且您也不想将数据复制到新工作表中,那么这是我能想到的唯一方法。
- 以只读方式打开工作簿。如果你正在使用隐藏行/列的活动工作表,那么创建一个excel文件的副本,隐藏这些行/列,然后使用该副本。
- 查找工作表上的最后一行/列
- 循环遍历工作表的行/列,并删除不可见的行/列
- 现在只剩下一个连续的范围。复制到数组
- 关闭Excel文件而不保存
注意:我已经向您展示了一个遍历行的示例。对列
也可以做类似的操作int _lastRow = xlWorkSheet.Cells[xlWorkSheet.Rows.Count, 1].End[Microsoft.Office.Interop.Excel.XlDirection.xlUp].Row;
for (int i = 1; i <= _lastRow; i++)
{
if (xlWorkSheet.Cells[i, 1].Entirerow.Hidden == true)
xlWorkSheet.Cells[i, 1].EntireRow.Delete(Microsoft.Office.Interop.Excel.XlDirection.xlUp); ;
}
编辑
我将此添加为单独的部分,而不是删除上面的代码,因为它可能有助于任何未来的访问者。
显然,OP对复制可见单元格到新工作表的想法很好,然后使用.Value2
将数据存储到评论中建议的数组中。
object misValue = System.Reflection.Missing.Value;
xlNewWorkSheet = xlexcel.Worksheets.Add(misValue, misValue, misValue, misValue);
Excel.Range MyRange =
xlWorkSheet.UsedRange.SpecialCells(Excel.XlCellType.xlCellTypeVisible,misValue);
MyRange.Copy(xlNewWorkSheet.get_Range("A1", "A1"));