Практическое руководство. Программное изменение форматирования в строках листа, содержащих выбранные ячейки
Можно изменить шрифт всей строки, содержащей выбранную ячейку, сделав ее текст жирным.
Применение. Сведения этого раздела применяются к проектам уровня документа и уровня приложения для Excel 2013 и Excel 2010. Дополнительные сведения см. в разделе Доступность функций по типам приложений Office и проектов.
Преобразование текущей строки в жирную и возвращение предыдущей жирной строке нормального форматирования
Объявите статическую переменную для отслеживания предыдущей выбранной строки.
Static previousRow As Integer = 0
static int previousRow = 0;
Извлеките ссылку на текущую ячейку с помощью свойства ActiveCell.
Dim currentCell As Excel.Range = Me.Application.ActiveCell
Excel.Range currentCell = this.Application.ActiveCell;
Примените к текущей строке стиль жирного написания с помощью свойства активной ячейки EntireRow.
currentCell.EntireRow.Font.Bold = True
currentCell.EntireRow.Font.Bold = true;
Убедитесь в том, что текущее значение элемента previousRow не равно 0.Значение 0 (ноль) указывает на то, что код выполняется впервые.
If previousRow <> 0 Then
if (previousRow != 0)
Убедитесь, что текущая строка отличается от предыдущей.
If currentCell.Row <> previousRow Then
if (currentCell.Row != previousRow)
Извлеките ссылку на диапазон, представляющий ранее выбранную строку, а затем отмените жирное начертание для этой строки.
Dim rng As Excel.Range = DirectCast(ws.Rows(previousRow), Excel.Range) rng.EntireRow.Font.Bold = False
Excel.Range rng = (Excel.Range)ws.Rows[previousRow]; rng.EntireRow.Font.Bold = false;
Сохраните текущую строку, чтобы она стала предыдущей строкой при следующем проходе.
previousRow = currentCell.Row
previousRow = currentCell.Row;
Ниже приведен полный пример метода.
Пример
Private Sub BoldCurrentRow(ByVal ws As Excel.Worksheet)
' Keep track of the previously bolded row.
Static previousRow As Integer = 0
' Work with the current active cell.
Dim currentCell As Excel.Range = Me.Application.ActiveCell
' Bold the current row.
currentCell.EntireRow.Font.Bold = True
' If a pass has been done previously, make the old row not bold.
' Make sure previousRow is not 0 (otherwise this is your first pass through).
If previousRow <> 0 Then
' Make sure the current row is not the same as the previous row.
If currentCell.Row <> previousRow Then
Dim rng As Excel.Range = DirectCast(ws.Rows(previousRow), Excel.Range)
rng.EntireRow.Font.Bold = False
End If
End If
' Store the new row number for the next pass.
previousRow = currentCell.Row
End Sub
// Keep track of the previously bolded row.
static int previousRow = 0;
private void BoldCurrentRow(Excel.Worksheet ws)
{
// Work with the current active cell.
Excel.Range currentCell = this.Application.ActiveCell;
// Bold the current row.
currentCell.EntireRow.Font.Bold = true;
// If a pass has been done previously, make the old row not bold.
// Make sure previousRow is not 0 (otherwise this is your first pass through).
if (previousRow != 0)
// Make sure the current row is not the same as the previous row.
if (currentCell.Row != previousRow)
{
Excel.Range rng = (Excel.Range)ws.Rows[previousRow];
rng.EntireRow.Font.Bold = false;
}
// Store the new row number for the next pass.
previousRow = currentCell.Row;
}
См. также
Задачи
Практическое руководство. Программное применение стилей к диапазонам в книгах
Практическое руководство. Программное копирование данных и форматирование на листах