Repeat Values
The document model allows you to automatically repeat data that has already been entered in your worksheet. The auto fill feature is very useful when you would like to copy the contents of a row or a column into its adjacent rows or columns respectively. Thus, you can easily spread the values into a specified range instead of populating the cells manually.
To repeat the values, first you need to create a CellSelection for the range of cells that you want to populate. Note that the range should include the values that you would like to repeat. Then, you need to invoke the FillData() method of the CellSelection instance and pass appropriate FillDirection as an argument. There are four FillDirection values:
Left: The values in the rightmost column are copied to the rest of the columns in the range.
Up: The values in the bottom row are copied to the rest of the rows in the range.
Right: The values in the leftmost column are copied to the rest of the columns in the range.
Down: The values in the top row are copied to the rest of the rows in the range.
Example 1 illustrates how the contents of column A can be copied to the rest of the columns in the range A1:D4. The code creates a new worksheet and populates the cells A1, A2, A3 and A4 with the values 5, 8, 13 and 21 respectively. Further, it invokes the FillData() method for the specified range with FillDirection Right.
Example 1: Fill right
Workbook workbook = new Workbook();
workbook.Worksheets.Add();
Worksheet activeWorksheet = workbook.ActiveWorksheet;
activeWorksheet.Cells[0, 0].SetValue(5);
activeWorksheet.Cells[1, 0].SetValue(8);
activeWorksheet.Cells[2, 0].SetValue(13);
activeWorksheet.Cells[3, 0].SetValue(21);
CellRange range = new CellRange(0, 0, 3, 3);
activeWorksheet.Cells[range].FillData(FillDirection.Right);
Figure 1 demonstrates the result of Example 1.
Figure 1: Data filled right
Similarly, you can automatically copy the values of a row to its adjacent rows.
Example 2 invokes the FillData() method with FillDirection Down for the range B2:D4. The sample code creates an empty worksheet and enters values in the range B2:D2. These values are propagated to the rest of the rows in the specified region.
Example 2: Fill down
Workbook workbook = new Workbook();
workbook.Worksheets.Add();
Worksheet activeWorksheet = workbook.ActiveWorksheet;
activeWorksheet.Cells[1, 1].SetValue(34);
activeWorksheet.Cells[1, 2].SetValue(55);
activeWorksheet.Cells[1, 3].SetValue(89);
CellRange range = new CellRange(1, 1, 3, 3);
activeWorksheet.Cells[range].FillData(FillDirection.Down);
Figure 2 demonstrates the result of Example 2.