Set fixed Column width for all columns while creating the Excel using OpenXml

Elaya Raja 96 Reputation points
2022-07-11T16:49:25.447+00:00

I have created the Excel sheet using openxml and I have attached the code.
I want to set each column width is some fixed value.

219527-sourcecode-edit.txt

@Anonymous

Thanks in advance.
JO

Developer technologies ASP.NET ASP.NET Core
Developer technologies .NET Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-07-12T06:19:47.283+00:00

    Hi @layaRaja-3410,

    To set the column widths you need to create a Columns element which can contain one or more Column children.

    Each Column class can apply to more than one column in the Excel file. The Min and Max properties define the first and last column that the Column applies to (inclusive).

    For example, you could use the following code to set the column width in the WriteExcelFile method:

    219715-image.png

    The WriteExcelFile method: 219735-sourcecode.txt
    Then the generated excel like this: by using the above columns, the first and second columns' width is 20, the third and fourth columns' width is 50, and other columns is the default width.

    219660-image.png

    Note1: Any columns not covered by the Column classes will have a default width.

    Note2: All properties of the column (Min, Max, Width, CustomWidth) should be specified. Otherwise Excel will decide that the file is corrupted.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.