|
1 | | -# How to change the row height for the exported excel sheet? |
| 1 | +# How to Change the Row Height for the Exported Excel Sheet |
2 | 2 |
|
3 | 3 | # About the example |
4 | 4 |
|
5 | | -This example illustrates how to change the row height of the excel sheet exported from the DataGrid and also shows how to auto adjust the row height of the exported excel sheet based on its content. |
| 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. |
| 6 | + |
| 7 | +You can change the row height of the exported excel sheet using `Worksheets.UsedRange.RowHeight` property. |
| 8 | + |
| 9 | +```C# |
| 10 | +private void ExportToExcel_Click(object sender, System.EventArgs e) |
| 11 | +{ |
| 12 | + ExcelExportingOptions options = new ExcelExportingOptions(); |
| 13 | + var excelEngine = sfDataGrid.ExportToExcel(sfDataGrid.View, options); |
| 14 | + var workBook = excelEngine.Excel.Workbooks[0]; |
| 15 | + |
| 16 | + //Set row height. |
| 17 | + workBook.Worksheets[0].UsedRange.RowHeight = 30; |
| 18 | + |
| 19 | + SaveFileDialog sfd = new SaveFileDialog |
| 20 | + { |
| 21 | + FilterIndex = 2, |
| 22 | + Filter = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx", |
| 23 | + FileName = "Book1" |
| 24 | + }; |
| 25 | + |
| 26 | + if (sfd.ShowDialog() == DialogResult.OK) |
| 27 | + { |
| 28 | + using (Stream stream = sfd.OpenFile()) |
| 29 | + { |
| 30 | + if (sfd.FilterIndex == 1) |
| 31 | + workBook.Version = ExcelVersion.Excel97to2003; |
| 32 | + else |
| 33 | + workBook.Version = ExcelVersion.Excel2010; |
| 34 | + workBook.SaveAs(stream); |
| 35 | + } |
| 36 | + |
| 37 | + //Message box confirmation to view the created spreadsheet. |
| 38 | + if (MessageBox.Show("Do you want to view the workbook?", "Workbook has been created", MessageBoxButtons.OKCancel) == DialogResult.OK) |
| 39 | + { |
| 40 | + //Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer] |
| 41 | + System.Diagnostics.Process.Start(sfd.FileName); |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +You can use `AutofitRows` method to adjust the row height of the exported excel sheet based on the content. |
| 48 | + |
| 49 | + |
| 50 | +```C# |
| 51 | +private void ExportToExcel_Click(object sender, System.EventArgs e) |
| 52 | +{ |
| 53 | + ExcelExportingOptions options = new ExcelExportingOptions(); |
| 54 | + var excelEngine = sfDataGrid.ExportToExcel(sfDataGrid.View, options); |
| 55 | + var workBook = excelEngine.Excel.Workbooks[0]; |
| 56 | + |
| 57 | + //Row height will be set based on the content. |
| 58 | + workBook.Worksheets[0].UsedRange.AutofitRows(); |
| 59 | + |
| 60 | + SaveFileDialog sfd = new SaveFileDialog |
| 61 | + { |
| 62 | + FilterIndex = 2, |
| 63 | + Filter = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx", |
| 64 | + FileName = "Book1" |
| 65 | + }; |
| 66 | + |
| 67 | + if (sfd.ShowDialog() == DialogResult.OK) |
| 68 | + { |
| 69 | + using (Stream stream = sfd.OpenFile()) |
| 70 | + { |
| 71 | + if (sfd.FilterIndex == 1) |
| 72 | + workBook.Version = ExcelVersion.Excel97to2003; |
| 73 | + else |
| 74 | + workBook.Version = ExcelVersion.Excel2010; |
| 75 | + workBook.SaveAs(stream); |
| 76 | + } |
| 77 | + |
| 78 | + //Message box confirmation to view the created spreadsheet. |
| 79 | + if (MessageBox.Show("Do you want to view the workbook?", "Workbook has been created", MessageBoxButtons.OKCancel) == DialogResult.OK) |
| 80 | + { |
| 81 | + //Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer] |
| 82 | + System.Diagnostics.Process.Start(sfd.FileName); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
0 commit comments