It is usual to find that some data in one column or row is unnecessary in one Excel worksheet. So, we will delete this column or row. Also, we can hide it because the data may be useful in other tables. Besides, we can find that column width and row height is set by default before we export data in worksheet. However, some long data will not be displayed completely if we don’t adjust width and height. Now, in this post, I will introduce something about row and column settings with C#, including hide, delete column and row, set row height and column width.
There is an Excel file which saves some students information, containing Student No., Name, Gender, Birthday, Age and E-mail information. In this file, some columns and rows are not formatted well and I need to hide or delete some unnecessary or important information.
Note: I use Spire.XLS in this example, so I add its DLL file as reference in project.
The following steps describes how to set in details.
Step 1: load the file which I want to set column and row from my computer and initialize worksheet which saves data.
Workbook workbook = new Workbook();
workbook.LoadFromFile(@”D:\work\My Documents\Student Info.xlsx”, ExcelVersion.Version2010);
Worksheet sheet = workbook.Worksheets;
Step 2: delete age column and information of the student who leaves school.
Step 3: hide birthday column and information of the student who studies abroad.
Step 4: auto fit the first column and vice-monitor information row.
Step 5: set column width for E-mail information column and row height for monitor information row.
Step 6: Set format for monitor information row to highlight it.
sheet.Range[“A7:F7”].Style.VerticalAlignment = VerticalAlignType.Center;
sheet.Range[“A7:F7”].Style.Font.IsBold = true;
Step 7: save and launch file.
Click Here to LEARN MORE about Spire.XLS
Click Here to DOWNLOAD Spire.XLS