Use the Excel Export API to Specify the Cell Selection
- 2 minutes to read
You can use the following API properties to select cells in a generated worksheet:
Property | Description |
---|---|
IXlSheet.Selection | Provides access to the IXlSheetSelection object that allows you to specify the cell selection. |
IXlSheetSelection.SelectedRanges | Specifies a list of cell ranges to select in a worksheet. |
IXlSheetSelection.ActiveCell | Specifies an active cell. |
You should specify the cell selection before you generate a worksheet’s content.
// Create a new document and write it to the stream.
using (IXlDocument document = exporter.CreateDocument(stream))
{
// Add a new worksheet to the document.
using (IXlSheet sheet = document.CreateSheet())
{
// Specify the cell ranges you want to select.
IList<XlCellRange> selectedRanges = sheet.Selection.SelectedRanges;
selectedRanges.Add(XlCellRange.FromLTRB(0, 1, 2, 3)); // A2:C4
selectedRanges.Add(XlCellRange.FromLTRB(2, 5, 3, 8)); // C6:D9
// Set the active cell.
sheet.Selection.ActiveCell = new XlCellPosition(1, 2); // B3
// Generate the document's content.
// ...
}
}
The image below shows the result.