Skip to content

Commit 3170790

Browse files
authored
Merge pull request #3 from SyncfusionExamples/ES-975464
ES-975464 - Resolve ReadMe Length Issues in this Repository
2 parents 640bf6f + fb46fe8 commit 3170790

File tree

3 files changed

+155
-3
lines changed

3 files changed

+155
-3
lines changed

ChangingRowHeight.png

52.2 KB
Loading
73.3 KB
Loading

README.md

Lines changed: 155 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,157 @@
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?
22

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.
44

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+
You can change the row height of the exported excel sheet using **Worksheets.UsedRange.RowHeight** property.
6+
7+
### C#
8+
9+
```csharp
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+
### VB
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+
```
78+
79+
![How to Change the Row Height for the Exported Excel Sheet in WinForms DataGrid](ChangingRowHeight.png)
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
86+
private void ExportToExcel_Click(object sender, System.EventArgs e)
87+
{
88+
ExcelExportingOptions options = new ExcelExportingOptions();
89+
var excelEngine = sfDataGrid.ExportToExcel(sfDataGrid.View, options);
90+
var workBook = excelEngine.Excel.Workbooks[0];
91+
92+
//Row height will be set based on the content.
93+
workBook.Worksheets[0].UsedRange.AutofitRows();
94+
95+
SaveFileDialog sfd = new SaveFileDialog
96+
{
97+
FilterIndex = 2,
98+
Filter = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx",
99+
FileName = "Book1"
100+
};
101+
102+
if (sfd.ShowDialog() == DialogResult.OK)
103+
{
104+
using (Stream stream = sfd.OpenFile())
105+
{
106+
if (sfd.FilterIndex == 1)
107+
workBook.Version = ExcelVersion.Excel97to2003;
108+
else
109+
workBook.Version = ExcelVersion.Excel2010;
110+
workBook.SaveAs(stream);
111+
}
112+
113+
//Message box confirmation to view the created spreadsheet.
114+
if (MessageBox.Show("Do you want to view the workbook?", "Workbook has been created", MessageBoxButtons.OKCancel) == DialogResult.OK)
115+
{
116+
//Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer]
117+
System.Diagnostics.Process.Start(sfd.FileName);
118+
}
119+
}
120+
}
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+
![How to Change the Row Height for the Exported Excel Sheet in WinForms DataGrid](ChangingRowHeightBasedOnContent.png)
156+
157+
Take a moment to peruse the [WinForms DataGrid - Excel Export](https://help.syncfusion.com/windowsforms/datagrid/exporttoexcel) documentation, where you can find about DataGrid with code examples.

0 commit comments

Comments
 (0)