Create Excel Workbook with Custom Row Height and Column Width in C#

When creating Excel spreadsheets programmatically, customization plays a vital role in improving readability and presentation. The ability to control row height, column width, and cell values allows developers to tailor their spreadsheets to specific formatting requirements. Whether for reports, dashboards, or data summaries, precise layout control is a fundamental part of professional spreadsheet development.

In this blog post, we’ll walk through a practical C# example using the Openize.OpenXML-SDK to demonstrate how to create a new Excel workbook with custom row heights, column widths, and formatted content. This is especially useful when generating dynamic spreadsheets as part of enterprise or automated applications.

Formatting Excel Programmatically with Openize.OpenXML

In many business applications, there is a need to generate spreadsheets on the fly—for example, generating reports, invoices, or data exports with consistent formatting. The Openize.OpenXML-SDK allows for detailed customization while abstracting away the complexity of the Excel file format.

Here’s a sample code snippet that accomplishes these tasks:

Code Explanation

Let’s break down what this code is doing and how each part contributes to generating a customized Excel workbook.

  1. Workbook Initialization The program starts by initializing a new instance of the Workbook class from the Openize.OpenXML-SDK. This creates a blank Excel workbook in memory.
  2. Accessing the First Worksheet By default, the new workbook contains at least one worksheet. The program accesses the first worksheet using index 0.
  3. Setting Row Height The height of the first row is explicitly set to 40 points. This can be useful when you want the first row to stand out or accommodate large text or images.
  4. Setting Column Width Next, the code adjusts the width of column “B” to 75 points. Column width customization ensures the content within cells isn’t cut off or overly compressed, improving the overall appearance of the sheet.
  5. Inserting Cell Values Two cell values are inserted:
  • "Value in A1" is placed in cell A1.
  • "Styled Text" is inserted into cell B2. While not styled in this snippet, the cell’s value demonstrates flexibility in placing specific content.
  1. Saving the Workbook Finally, the file is saved to a specified path on the local file system. At this point, all customizations are written into the new Excel file.

Real-World Scenarios for Custom Excel Formatting

Here are a few situations where custom formatting is extremely helpful:

  • Financial Reports: Bold headers, specific row heights, and wide columns for figures.
  • Invoice Generation: Company details and client info need structured spacing.
  • Survey Exports: Adjust columns for multi-line responses or long questions.
  • Dashboards: Highlight key performance indicators using larger cells.

Benefits of Using Openize.OpenXML-SDK

  1. No Need for Excel Installation

The SDK works without requiring Excel to be installed on the host machine, making it perfect for server environments.

  1. Rich Excel Manipulation API

Openize.OpenXML-SDK offers complete control over Excel files including styles, charts, data validation, and formulas in addition to formatting features.

  1. Performance & Reliability

It supports creating and modifying large files efficiently. You can use it in production systems with confidence in its performance and stability.

  1. Developer-Friendly

The SDK is well-documented and follows intuitive .NET design principles. This reduces the learning curve and speeds up implementation.

Conclusion

Generating Excel files with custom formatting is a necessity in many modern software applications. Using Openize.OpenXML-SDK, C# developers can create highly customized spreadsheets without needing to rely on external applications like Microsoft Excel.

This tutorial covered how to create a new workbook, adjust row height and column width, and insert cell values. Whether you’re building an invoicing engine, a reporting module, or a document generator, Openize.OpenXML-SDK provides the flexibility and power you need.

References