Excel formatting issue in C#

Pratham Jain 281 Reputation points
2025-03-02T16:15:19.0533333+00:00

Hi All,

I have a WPF application in which I am exporting data to excel. I have merged some cells and added some data to it. After printing I observed some formatting issue where some of the text is cutted and missing:

User's image

 _excelApp = new Excel.Application();

            _excelApp.StandardFont = FontName;

            _excelApp.StandardFontSize = Convert.ToDouble(FontSize);

            _books = (Excel.Workbooks)_excelApp.Workbooks;

            _book = (Excel._Workbook)(_books.Add(_optionalValue));

            _sheets = (Excel.Sheets)_book.Worksheets;

            _sheet = (Excel._Worksheet)(_sheets.get_Item(1));

            _sheet.PageSetup.Zoom = false;

            _sheet.PageSetup.FitToPagesWide = 1;

Please advise how can I fix this issue ASAP.

Regards,

Pratham

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,339 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hongrui Yu-MSFT 4,930 Reputation points Microsoft External Staff
    2025-03-03T09:03:25.97+00:00

    Hi, @Pratham Jain. Welcome to Microsoft Q&A. 

    If the text is truncated due to insufficient cell length, you could refer to the following solution: Solution 1: Manually control the cell size according to the text size.

    var range = _sheet.Range[_sheet.Cells[30, 10], _sheet.Cells[30, 12]];
    range.Merge();
    range.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
    
    string value = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
    double columnWidth = value.Length / 3 * 1.2;     
    range.Columns.ColumnWidth = columnWidth;
    range.Value = value;
    

    ColumnWidth requires the width of each cell. Here, since you merged 3 cells, you divided it by 3 when calculating columnWidth. Since you want to reserve some space for the cell, you multiplied it by 1.2. You could also consider not merging cells and calculating ColumnWidth according to your ideas. (Note: Here we consider the width of English words, the width of words in other languages may be different, you could multiply columnWidth by a value to ensure that the width is sufficient).

    Solution 2: Manually calculate the number of cells to be merged according to the text size to ensure that the width is sufficient.

            string value = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
    
            //The size of the current cell that needs to be merged
            int currentCell_startPosition = 10;
            int currentCell_endPosition = currentCell_startPosition + (int)(value.Length / 9.0);  //10
            
    
            var range = _sheet.Range[_sheet.Cells[30, currentCell_startPosition], _sheet.Cells[30, currentCell_endPosition]];
            range.Merge();
            range.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
    
            range.Value = value;
    
            //NextCell
            int nextCell_startPosition = currentCell_endPosition + 1;
            Excel.Range nextCell = (Excel.Range)_sheet.Cells[30, nextCell_startPosition];
            nextCell.Value = "abcdef";
    

    currentCell_startPosition and currentCell_endPosition are the starting and ending positions of the cells to be merged. In the case of English words, calculate currentCell_endPosition. A default cell could contain approximately 10 words. Here, 9 is written to ensure that there is enough reserved space. If you use other languages, you could further adjust.

    The following is a supplement

    Solution 3: Do not use Merge() to merge cells, and use WrapText to automatically adjust the height of the cell.

            _sheet.Columns.WrapText = true;
    
            string value = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
    
            var currentCell = (Excel.Range)_sheet.Cells[30, 10];
            currentCell.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
    
            currentCell.Value = value;
    

    Solution 4: Do not use Merge() to merge cells, and use AutoFit() to automatically adjust the cell width.

            _sheet.Columns.AutoFit();
    
            string value = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
            var currentCell = (Excel.Range)_sheet.Cells[30, 10];
            currentCell.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
    
            currentCell.Value = value;
    

    Note: After testing, it was found that if the length of value is too long, it will need to occupy [30,9], [30,8] cells after AutoFit, then [30,9], [30,8] cannot be occupied by other elements. Whether AutoFit will fail, the string will still be truncated. You could calculate [30,7] as the previous cell according to the previous method.


    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.


1 additional answer

Sort by: Most helpful
  1. Viorel 120.4K Reputation points
    2025-03-02T17:12:34.1266667+00:00

    Maybe you should execute this:

    _sheet.Range("A1").EntireColumn.AutoFit()

    or this:

    _sheet.Range("A1").EntireColumn.WrapText = true

    Use the corresponding cell instead of "A1".


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.