VintaSoft Imaging .NET SDK 15.1: Документация для .NET разработчика
В этом разделе
XLSX: Работа с ячейками на странице XLSX
В этом разделе
UI-контролы SpreadsheetEditorControl и WpfSpreadsheetEditorControl позволяют работать (просматривать, добавлять, редактировать и удалять) с ячейками рабочего листа XLSX в настольном приложении (WinForms, WPF).
Ячейки можно изменять визуально вручную с помощью мыши/клавиатуры или программно.


Добавление ячейки в рабочий лист XLSX

Вот C#/VB.NET код, который демонстрирует, как добавить ячейку в сфокусированный рабочий лист XLSX в:
public void AddCellToXlsxWorksheet(Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl editorControl)
{
    // get visual editor for spreadsheet document
    Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor spreadsheetVisualEditor = editorControl.VisualEditor;

    // insert empty cells in range of selected cells and shift columns to the right
    spreadsheetVisualEditor.InsertCellsAndShiftRight();

    // insert empty cells in range of selected cells and shift rows to down
    spreadsheetVisualEditor.InsertCellsAndShiftDown();
}
Public Sub AddCellToXlsxWorksheet(editorControl As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl)
    ' get visual editor for spreadsheet document
    Dim spreadsheetVisualEditor As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor = editorControl.VisualEditor

    ' insert empty cells in range of selected cells and shift columns to the right
    spreadsheetVisualEditor.InsertCellsAndShiftRight()

    ' insert empty cells in range of selected cells and shift rows to down
    spreadsheetVisualEditor.InsertCellsAndShiftDown()
End Sub


Копирование и вставка ячейки рабочего листа XLSX

Если вы хотите скопировать и вставить выбранные ячейки рабочего листа XLSX с помощью мыши, вам следует выполнить следующие шаги:
Вот C#/VB.NET код, который демонстрирует, как копировать и вставлять ячейки рабочего листа XLSX:
public void CopyAndPasteXlsxCell(Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl editorControl)
{
    // get visual editor for spreadsheet document
    Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor spreadsheetVisualEditor = editorControl.VisualEditor;

    // copy content (selected cells, cell text, etc) to the clipboard
    spreadsheetVisualEditor.CopyCells();

    // paste content (selected cells, cell text, etc) from the clipboard
    spreadsheetVisualEditor.PasteCells();
}
Public Sub CopyAndPasteXlsxCell(editorControl As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl)
    ' get visual editor for spreadsheet document
    Dim spreadsheetVisualEditor As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor = editorControl.VisualEditor

    ' copy content (selected cells, cell text, etc) to the clipboard
    spreadsheetVisualEditor.CopyCells()

    ' paste content (selected cells, cell text, etc) from the clipboard
    spreadsheetVisualEditor.PasteCells()
End Sub


Вырезание и вставка ячейки рабочего листа XLSX

Если вы хотите вырезать и вставить выбранные ячейки листа XLSX с помощью мыши, вам следует выполнить следующие шаги:
Вот C#/VB.NET код, который демонстрирует, как вырезать и вставить ячейку листа XLSX:
public void CopyAndPasteXlsxCell(Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl editorControl)
{
    // get visual editor for spreadsheet document
    Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor spreadsheetVisualEditor = editorControl.VisualEditor;

    // cut content(selected cells, cell text, etc) to the clipboard
    spreadsheetVisualEditor.CutCells();

    // paste content (selected cells, cell text, etc) from the clipboard
    spreadsheetVisualEditor.PasteCells();
}
Public Sub CopyAndPasteXlsxCell(editorControl As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl)
    ' get visual editor for spreadsheet document
    Dim spreadsheetVisualEditor As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor = editorControl.VisualEditor

    ' cut content(selected cells, cell text, etc) to the clipboard
    spreadsheetVisualEditor.CutCells()

    ' paste content (selected cells, cell text, etc) from the clipboard
    spreadsheetVisualEditor.PasteCells()
End Sub


Изменение значения выделенной ячейки программно

Вот C#/VB.NET код, который демонстрирует, как изменить значение выделенной ячейки рабочего листа XLSX:
public void ChangeValueOfXlsxCell(Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl editorControl)
{
    // get visual editor for spreadsheet document
    Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor spreadsheetVisualEditor = editorControl.VisualEditor;

    // get value or formula of focused cell
    string cellValue = spreadsheetVisualEditor.FocusedCellValue;

    // set value or formula of focused cell
    spreadsheetVisualEditor.FocusedCellValue = "0";
}
Public Sub ChangeValueOfXlsxCell(editorControl As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl)
    ' get visual editor for spreadsheet document
    Dim spreadsheetVisualEditor As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor = editorControl.VisualEditor

    ' get value or formula of focused cell
    Dim cellValue As String = spreadsheetVisualEditor.FocusedCellValue

    ' set value or formula of focused cell
    spreadsheetVisualEditor.FocusedCellValue = "0"
End Sub


Изменение значения выделенной ячейки с помощью текстового поля в области ячейки (внутренний редактор)

Если вы хотите изменить значение выделенной ячейки листа XLSX с помощью мыши, вам следует выполнить следующие шаги:

Изменение числового формата выбранных ячеек рабочего листа XLSX

Вот C#/VB.NET код, который демонстрирует, как изменить числовой формат выбранной ячейки рабочего листа XLSX:
public void ChangeNumberFormatOfXlsxCell(Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl editorControl)
{
    // get visual editor for spreadsheet document
    Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor spreadsheetVisualEditor = editorControl.VisualEditor;

    // set the number format of selected cells
    spreadsheetVisualEditor.NumberFormat = "0";
}
Public Sub ChangeNumberFormatOfXlsxCell(editorControl As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl)
    ' get visual editor for spreadsheet document
    Dim spreadsheetVisualEditor As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor = editorControl.VisualEditor

    ' set the number format of selected cells
    spreadsheetVisualEditor.NumberFormat = "0"
End Sub


Изменение свойств шрифта выбранных ячеек рабочего листа XLSX

Вот C#/VB.NET код, который демонстрирует, как изменить свойства шрифта выделенных ячеек рабочего листа XLSX:
public void ChangeFontPropertiesOfXlsxCell(Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl editorControl)
{
    // get visual editor for spreadsheet document
    Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor spreadsheetVisualEditor = editorControl.VisualEditor;

    // get the font name for focused cell
    var fontName = spreadsheetVisualEditor.FontName;
    // set new font name for selected cells
    spreadsheetVisualEditor.FontName = "Impact";

    // get the font size for focused cell
    var fontSize = spreadsheetVisualEditor.FontSize;
    // set new font size for selected cells
    spreadsheetVisualEditor.FontSize = 16;


    // get the bold status of text of focused cell
    var isBold = spreadsheetVisualEditor.IsFontBold;
    // set new bold status for text in selected cells
    spreadsheetVisualEditor.IsFontBold = true;

    // get the italic status of text of focused cell
    var isItalic = spreadsheetVisualEditor.IsFontItalic;
    // set new italic status for text in selected cells
    spreadsheetVisualEditor.IsFontItalic = true;

    // get the underline status of text of focused cell
    var isUnderline = spreadsheetVisualEditor.IsFontUnderline;
    // set new underline status for text in selected cells
    spreadsheetVisualEditor.IsFontUnderline = true;

    // get the strikeout status of text of focused cell
    var isStrikeout = spreadsheetVisualEditor.IsFontStrikeout;
    // set new strikeout status for text in selected cells
    spreadsheetVisualEditor.IsFontStrikeout = true;


    // get font color of focused cell
    var fontColor = spreadsheetVisualEditor.FontColor;
    // set new font color for selected cells
    spreadsheetVisualEditor.FontColor = Vintasoft.Primitives.VintasoftColor.FromRgb(255, 0, 0);
}
Public Sub ChangeFontPropertiesOfXlsxCell(editorControl As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl)
    ' get visual editor for spreadsheet document
    Dim spreadsheetVisualEditor As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor = editorControl.VisualEditor

    ' get the font name for focused cell
    Dim fontName = spreadsheetVisualEditor.FontName
    ' set new font name for selected cells
    spreadsheetVisualEditor.FontName = "Impact"

    ' get the font size for focused cell
    Dim fontSize = spreadsheetVisualEditor.FontSize
    ' set new font size for selected cells
    spreadsheetVisualEditor.FontSize = 16


    ' get the bold status of text of focused cell
    Dim isBold = spreadsheetVisualEditor.IsFontBold
    ' set new bold status for text in selected cells
    spreadsheetVisualEditor.IsFontBold = True

    ' get the italic status of text of focused cell
    Dim isItalic = spreadsheetVisualEditor.IsFontItalic
    ' set new italic status for text in selected cells
    spreadsheetVisualEditor.IsFontItalic = True

    ' get the underline status of text of focused cell
    Dim isUnderline = spreadsheetVisualEditor.IsFontUnderline
    ' set new underline status for text in selected cells
    spreadsheetVisualEditor.IsFontUnderline = True

    ' get the strikeout status of text of focused cell
    Dim isStrikeout = spreadsheetVisualEditor.IsFontStrikeout
    ' set new strikeout status for text in selected cells
    spreadsheetVisualEditor.IsFontStrikeout = True


    ' get font color of focused cell
    Dim fontColor = spreadsheetVisualEditor.FontColor
    ' set new font color for selected cells
    spreadsheetVisualEditor.FontColor = Vintasoft.Primitives.VintasoftColor.FromRgb(255, 0, 0)
End Sub


Изменение свойств текста выбранных ячеек листа XLSX

Вот C#/VB.NET код, который демонстрирует, как изменить свойства текста выделенных ячеек листа XLSX:
public void ChangeTextPropertiesOfXlsxCell(Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl editorControl)
{
    // get visual editor for spreadsheet document
    Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor spreadsheetVisualEditor = editorControl.VisualEditor;

    // get the text horizontal alignment for focused cell
    var textHorizontalAlign = spreadsheetVisualEditor.TextHorizontalAlign;

    // set new text horizontal alignment for selected cells
    spreadsheetVisualEditor.TextHorizontalAlign = Vintasoft.Imaging.Office.Spreadsheet.Document.TextHorizontalAlign.Center;


    // get the text vertical alignment for focused cell
    var textVerticalAlign = spreadsheetVisualEditor.TextVerticalAlign;

    // set new text vertical alignment for selected cells
    spreadsheetVisualEditor.TextVerticalAlign = Vintasoft.Imaging.Office.Spreadsheet.Document.TextVerticalAlign.Bottom;
}
Public Sub ChangeTextPropertiesOfXlsxCell(editorControl As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl)
    ' get visual editor for spreadsheet document
    Dim spreadsheetVisualEditor As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor = editorControl.VisualEditor

    ' get the text horizontal alignment for focused cell
    Dim textHorizontalAlign = spreadsheetVisualEditor.TextHorizontalAlign

    ' set new text horizontal alignment for selected cells
    spreadsheetVisualEditor.TextHorizontalAlign = Vintasoft.Imaging.Office.Spreadsheet.Document.TextHorizontalAlign.Center


    ' get the text vertical alignment for focused cell
    Dim textVerticalAlign = spreadsheetVisualEditor.TextVerticalAlign

    ' set new text vertical alignment for selected cells
    spreadsheetVisualEditor.TextVerticalAlign = Vintasoft.Imaging.Office.Spreadsheet.Document.TextVerticalAlign.Bottom
End Sub


Изменение цвета фона выбранных ячеек рабочего листа XLSX

Вот C#/VB.NET код, который демонстрирует, как изменить цвет фона выделенных ячеек рабочего листа XLSX:
public void ChangeBackgroundOfXlsxCell(Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl editorControl)
{
    // get visual editor for spreadsheet document
    Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor spreadsheetVisualEditor = editorControl.VisualEditor;

    // get the background color of focused cell
    var backgroundColor = spreadsheetVisualEditor.FillColor;

    // set new background color for selected cells
    spreadsheetVisualEditor.FillColor = Vintasoft.Primitives.VintasoftColor.FromRgb(200, 150, 200);
}
Public Sub ChangeBackgroundOfXlsxCell(editorControl As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl)
    ' get visual editor for spreadsheet document
    Dim spreadsheetVisualEditor As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor = editorControl.VisualEditor

    ' get the background color of focused cell
    Dim backgroundColor = spreadsheetVisualEditor.FillColor

    ' set new background color for selected cells
    spreadsheetVisualEditor.FillColor = Vintasoft.Primitives.VintasoftColor.FromRgb(200, 150, 200)
End Sub


Изменение границы выбранных ячеек рабочего листа XLSX

Вот C#/VB.NET код, который демонстрирует, как изменить границу выбранных ячеек рабочего листа XLSX:
public void ChangeBorderOfXlsxCell(Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl editorControl)
{
    // get visual editor for spreadsheet document
    Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor spreadsheetVisualEditor = editorControl.VisualEditor;

    // create border style
    Vintasoft.Imaging.Office.Spreadsheet.Document.CellBorderStyle borderStyle = 
        Vintasoft.Imaging.Office.Spreadsheet.Document.CellBorderStyle.Dashed;
    // create border color
    Vintasoft.Primitives.VintasoftColor borderColor = Vintasoft.Primitives.VintasoftColor.Blue;
    // create border
    Vintasoft.Imaging.Office.Spreadsheet.Document.CellBorder border =
        new Vintasoft.Imaging.Office.Spreadsheet.Document.CellBorder(borderStyle, borderColor);
    // create outside borders (you can use different border for each side)
    Vintasoft.Imaging.Office.Spreadsheet.Document.CellBorders outsideBorders =
        new Vintasoft.Imaging.Office.Spreadsheet.Document.CellBorders(border, border, border, border);
    // create all borders
    Vintasoft.Imaging.Office.Spreadsheet.Document.CellsBorders allBorders =
        new Vintasoft.Imaging.Office.Spreadsheet.Document.CellsBorders(outsideBorders, border, border);
    // set borders to focused cells
    spreadsheetVisualEditor.CellsBorders = allBorders;


    // set new border color for selected cells
    spreadsheetVisualEditor.BordersColor = Vintasoft.Primitives.VintasoftColor.FromRgb(200, 150, 200);
}
Public Sub ChangeBorderOfXlsxCell(editorControl As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl)
    ' get visual editor for spreadsheet document
    Dim spreadsheetVisualEditor As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor = editorControl.VisualEditor

    ' create border style
    Dim borderStyle As Vintasoft.Imaging.Office.Spreadsheet.Document.CellBorderStyle = Vintasoft.Imaging.Office.Spreadsheet.Document.CellBorderStyle.Dashed
    ' create border color
    Dim borderColor As Vintasoft.Primitives.VintasoftColor = Vintasoft.Primitives.VintasoftColor.Blue
    ' create border
    Dim border As New Vintasoft.Imaging.Office.Spreadsheet.Document.CellBorder(borderStyle, borderColor)
    ' create outside borders (you can use different border for each side)
    Dim outsideBorders As New Vintasoft.Imaging.Office.Spreadsheet.Document.CellBorders(border, border, border, border)
    ' create all borders
    Dim allBorders As New Vintasoft.Imaging.Office.Spreadsheet.Document.CellsBorders(outsideBorders, border, border)
    ' set borders to focused cells
    spreadsheetVisualEditor.CellsBorders = allBorders


    ' set new border color for selected cells
    spreadsheetVisualEditor.BordersColor = Vintasoft.Primitives.VintasoftColor.FromRgb(200, 150, 200)
End Sub


Очистка содержимого выбранных ячеек рабочего листа XLSX

Если вы хотите очистить содержимое выбранных ячеек рабочего листа XLSX с помощью мыши, вам следует выполнить следующие шаги:
Вот C#/VB.NET код, который демонстрирует, как очистить содержимое ячеек рабочего листа XLSX:
public void ClearXlsxCell(Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl editorControl)
{
    // get visual editor for spreadsheet document
    Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor spreadsheetVisualEditor = editorControl.VisualEditor;

    // clear contents (value and formula) of selected cells
    spreadsheetVisualEditor.ClearCellsContents();
}
Public Sub ClearXlsxCell(editorControl As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl)
    ' get visual editor for spreadsheet document
    Dim spreadsheetVisualEditor As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor = editorControl.VisualEditor

    ' clear contents (value and formula) of selected cells
    spreadsheetVisualEditor.ClearCellsContents()
End Sub


Объединение и разделение ячеек рабочего листа XLSX

Вот C#/VB.NET код, который демонстрирует, как объединить и разделить ячейки рабочего листа XLSX:
public void MergeAndUnmergeXlsxCell(Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl editorControl)
{
    // get visual editor for spreadsheet document
    Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor spreadsheetVisualEditor = editorControl.VisualEditor;

    // merges the selected cells
    spreadsheetVisualEditor.MergeCells();

    // unmerges the selected cells
    spreadsheetVisualEditor.UnmergeCells();
}
Public Sub MergeAndUnmergeXlsxCell(editorControl As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl)
    ' get visual editor for spreadsheet document
    Dim spreadsheetVisualEditor As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor = editorControl.VisualEditor

    ' merges the selected cells
    spreadsheetVisualEditor.MergeCells()

    ' unmerges the selected cells
    spreadsheetVisualEditor.UnmergeCells()
End Sub


Удаление ячейки из рабочего листа XLSX

Вот C# и VB.NET код, демонстрирующий, как удалить ячейку из листа XLSX:
public void DeleteCellFromXlsxWorksheet(Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl editorControl)
{
    // get visual editor for spreadsheet document
    Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor spreadsheetVisualEditor = editorControl.VisualEditor;

    // remove selected cells and shift columns to the left
    spreadsheetVisualEditor.RemoveCellsAndShiftLeft();

    // remove selected cells and shift rows to up
    spreadsheetVisualEditor.RemoveCellsAndShiftUp();
}
Public Sub DeleteCellFromXlsxWorksheet(editorControl As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetEditorControl)
    ' get visual editor for spreadsheet document
    Dim spreadsheetVisualEditor As Vintasoft.Imaging.Office.Spreadsheet.UI.SpreadsheetVisualEditor = editorControl.VisualEditor

    ' remove selected cells and shift columns to the left
    spreadsheetVisualEditor.RemoveCellsAndShiftLeft()

    ' remove selected cells and shift rows to up
    spreadsheetVisualEditor.RemoveCellsAndShiftUp()
End Sub