|
1 | | -# How to Change the Row Height for the Exported Excel Sheet |
| 1 | +# How to Change the Row Height for the Exported Excel Sheet in WinForms DataGrid? |
2 | 2 |
|
3 | | -# About the example |
| 3 | +This example illustrates how to change the row height of the excel sheet exported from the [WinForms DataGrid](https://www.syncfusion.com/winforms-ui-controls/datagrid) (SfDataGrid) and also shows how to auto adjust the row height of the exported excel sheet based on its content. |
4 | 4 |
|
5 | | -This example illustrates how to change the row height of the excel sheet exported from the [WinForms DataGrid](https://www.syncfusion.com/winforms-ui-controls/datagrid) and also shows how to auto adjust the row height of the exported excel sheet based on its content. |
| 5 | +You can change the row height of the exported excel sheet using **Worksheets.UsedRange.RowHeight** property. |
6 | 6 |
|
7 | | -You can change the row height of the exported excel sheet using `Worksheets.UsedRange.RowHeight` property. |
| 7 | +### C# |
8 | 8 |
|
9 | | -```C# |
| 9 | +```csharp |
10 | 10 | private void ExportToExcel_Click(object sender, System.EventArgs e) |
11 | 11 | { |
12 | 12 | ExcelExportingOptions options = new ExcelExportingOptions(); |
@@ -44,10 +44,45 @@ private void ExportToExcel_Click(object sender, System.EventArgs e) |
44 | 44 | } |
45 | 45 | ``` |
46 | 46 |
|
47 | | -You can use `AutofitRows` method to adjust the row height of the exported excel sheet based on the content. |
| 47 | +### VB |
48 | 48 |
|
| 49 | +``` vb |
| 50 | +Private Sub ExportToExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button2.Click |
| 51 | + Dim options As New ExcelExportingOptions() |
| 52 | + Dim excelEngine = sfDataGrid.ExportToExcel(sfDataGrid.View, options) |
| 53 | + Dim workBook = excelEngine.Excel.Workbooks(0) |
| 54 | + |
| 55 | + 'Set row height. |
| 56 | + workBook.Worksheets(0).UsedRange.RowHeight = 30 |
| 57 | + |
| 58 | + Dim sfd As SaveFileDialog = New SaveFileDialog With {.FilterIndex = 2, .Filter = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx", .FileName = "Book1"} |
| 59 | + |
| 60 | + If sfd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then |
| 61 | + Using stream As Stream = sfd.OpenFile() |
| 62 | + If sfd.FilterIndex = 1 Then |
| 63 | + workBook.Version = ExcelVersion.Excel97to2003 |
| 64 | + Else |
| 65 | + workBook.Version = ExcelVersion.Excel2010 |
| 66 | + End If |
| 67 | + workBook.SaveAs(stream) |
| 68 | + End Using |
| 69 | + |
| 70 | + 'Message box confirmation to view the created spreadsheet. |
| 71 | + If MessageBox.Show("Do you want to view the workbook?", "Workbook has been created", MessageBoxButtons.OKCancel) = System.Windows.Forms.DialogResult.OK Then |
| 72 | + 'Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer] |
| 73 | + System.Diagnostics.Process.Start(sfd.FileName) |
| 74 | + End If |
| 75 | + End If |
| 76 | +End Sub |
| 77 | +``` |
49 | 78 |
|
50 | | -```C# |
| 79 | + |
| 80 | + |
| 81 | +You can use **AutofitRows** method to adjust the row height of the exported excel sheet based on the content. |
| 82 | + |
| 83 | +### C# |
| 84 | + |
| 85 | +``` csharp |
51 | 86 | private void ExportToExcel_Click(object sender, System.EventArgs e) |
52 | 87 | { |
53 | 88 | ExcelExportingOptions options = new ExcelExportingOptions(); |
@@ -84,3 +119,37 @@ private void ExportToExcel_Click(object sender, System.EventArgs e) |
84 | 119 | } |
85 | 120 | } |
86 | 121 | ``` |
| 122 | + |
| 123 | +### VB |
| 124 | + |
| 125 | +``` vb |
| 126 | +Private Sub ExportToExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button2.Click |
| 127 | + Dim options As New ExcelExportingOptions() |
| 128 | + Dim excelEngine = sfDataGrid.ExportToExcel(sfDataGrid.View, options) |
| 129 | + Dim workBook = excelEngine.Excel.Workbooks(0) |
| 130 | + |
| 131 | + 'Row height will be set based on the content. |
| 132 | + workBook.Worksheets(0).UsedRange.AutofitRows() |
| 133 | + |
| 134 | + Dim sfd As SaveFileDialog = New SaveFileDialog With {.FilterIndex = 2, .Filter = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx", .FileName = "Book1"} |
| 135 | + |
| 136 | + If sfd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then |
| 137 | + Using stream As Stream = sfd.OpenFile() |
| 138 | + If sfd.FilterIndex = 1 Then |
| 139 | + workBook.Version = ExcelVersion.Excel97to2003 |
| 140 | + Else |
| 141 | + workBook.Version = ExcelVersion.Excel2010 |
| 142 | + End If |
| 143 | + workBook.SaveAs(stream) |
| 144 | + End Using |
| 145 | + |
| 146 | + 'Message box confirmation to view the created spreadsheet. |
| 147 | + If MessageBox.Show("Do you want to view the workbook?", "Workbook has been created", MessageBoxButtons.OKCancel) = System.Windows.Forms.DialogResult.OK Then |
| 148 | + 'Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer] |
| 149 | + System.Diagnostics.Process.Start(sfd.FileName) |
| 150 | + End If |
| 151 | + End If |
| 152 | +End Sub |
| 153 | +``` |
| 154 | + |
| 155 | + |
0 commit comments