我们使用silverlight和电子表格设备构建了一个实用程序。使用此实用程序,我们允许用户从后端读取数据并允许他们修改数据,然后实用程序会将修改后的字段更新回后端。
我现在遇到的问题是,当用户执行过滤器并尝试在过滤的行中粘贴某些值时,这些值将被复制到过滤行之间的行中。
我不确定当我们进行复制/粘贴时如何限制它,它应该仅粘贴在过滤后的行上而不是过滤行之间的行上。
我在spreadhseet gear网站上搜索过,但我找不到与此问题相关的任何帖子。
非常感谢任何帮助?
答案 0 :(得分:1)
此行为是设计使然。请注意,在这种情况下,Excel也会粘贴到隐藏的单元格中。如果您需要复制/粘贴以跳过隐藏的行,您需要编写自己的例程来执行此操作。下面是对此的快速尝试,它适用于我构建的示例场景。您可能需要对其进行修改以支持您自己的更高级用例:
using SpreadsheetGear;
...
...
// Create a new workbook and some local variables for convenience.
IWorkbookSet workbookSet = Factory.GetWorkbookSet();
IWorkbook workbook = workbookSet.Workbooks.Add();
IWorksheet worksheet = workbook.ActiveWorksheet;
IRange cells = worksheet.Cells;
// Populate A1:A7 with some data used for the AutoFiltered area of the sheet.
cells["A1:A7"].Value = new object[,] { { "AutoFilter Header" }, { "a" }, { "b" }, { "c" }, { "d" }, { "e" }, { "f" } };
// This will be our "source" range when we do a copy.
cells["D10:D12"].Value = new object[,] { { 1 }, { 2 }, { 3 } };
// Filter out a, c and e, leaving b, d and f visible.
cells["A1:A7"].AutoFilter(0, new object[] { "b", "d", "f" }, AutoFilterOperator.Values, null, true);
IRange sourceRange = cells["D10:D12"];
IRange destination = cells["A2"];
// This will paste into hidden rows.
//sourceRange.Copy(destination);
// Instead, write our own copy routine to skip over hidden rows.
CopySkipHiddenRows(sourceRange, destination);
...
...
...
// One unaddressed edge case--exception could get thrown if hidden rows extend to the
// very bottom of the worksheet (i.e., Row 1,048,576) and we still have data
// to paste.
public void CopySkipHiddenRows(IRange sourceRange, IRange topLeftDestinationCell)
{
// Loop through each row of the source range.
for (int row = 0; row < sourceRange.RowCount; row++)
{
// Get row from source range
IRange sourceRow = sourceRange[row, 0, row, sourceRange.ColumnCount - 1];
// Skip over hidden rows.
while (topLeftDestinationCell.EntireRow.Hidden)
topLeftDestinationCell = topLeftDestinationCell.Offset(1, 0);
// Copy into next visible row
sourceRow.Copy(topLeftDestinationCell);
// Move to next row
topLeftDestinationCell = topLeftDestinationCell.Offset(1, 0);
}
}