WPF SpreadSheetGear electronic form, wpfspreadsheetgear
We often encounter the function development that generates an Excel interface and operates on it.
For example, in the following interface, we need To add a menu button "Columns To Rows Transform" in the menu To convert multiple Columns into multiple Rows in batches.
You can use the Interop component to add the menu appending function. I will write a Demo later for you to view. But this is a little troublesome. You need to manually register the Excel environment every time you start Excel.
We can use third-party controls to provide solutions:
1. Install the Telerik Control
If it is convenient, we recommend that you use the next Demo.
2. Create a Telerik project through Telerik in. It is convenient to create a template.
3. Add menu button
4. Add event processing in the background.
Private void ColumnsAndRowsTransformButton_OnClick (object sender, RoutedEventArgs e) {try {var workbook = viewModel. workbook; var sheet = workbook. activeWorksheet; var ranges = sheet. viewState. selectionState. selectedRanges; int insertRowsCount = 0; foreach (var range in ranges) {if (range. isSingleCell) {continue;} var firstRange = range. fromIndex; // Foreach Rows for (int I = 0; I <range. rowCoun T; I ++) {int rowIndex = firstRange. rowIndex + I + insertRowsCount; int columnIndex = firstRange. columnIndex; int currentRowAddRows = 0; // value for (int j = 1; j <range. columnCount; j ++) {var cellValue = sheet. cells [rowIndex, columnIndex + j]. getValue (). value; if (string. isNullOrEmpty (cellValue. rawValue) {continue;} sheet. rows. insert (rowIndex + 1 + currentRowAddRows, 1); currentRowAddRows ++; sheet. ce Lls [rowIndex + j, columnIndex]. setValue (cellValue. rawValue); sheet. cells [rowIndex, columnIndex + j]. setValue ("");} // copy the uplink content of the row's cell int index = 0; do {var upValue = sheet. cells [rowIndex, index]. getValue (). value; if (string. isNullOrEmpty (upValue. rawValue) & index> 50) {break;} if (index! = ColumnIndex) {for (int j = 0; j <currentRowAddRows; j ++) {sheet. cells [rowIndex + 1 + j, index]. setValue (upValue) ;}} index ++;} while (true); insertRowsCount ++ = currentRowAddRows ;}} catch (Exception ex) {MessageBox. show (ex. message );}}
View Code
This Telerik control is quite convenient.