How to do a text break in Excel programmatically with a check in C #

13

I'm using Microsoft.Office.Interop.Excel to create an Excel report from a txt file.

Everything works perfect, but sometimes some columns become extremely large and when we need to print, we need to resize and look horrible.

When I try to break it using Range.Style.WrapText = true , it looks strange, like this:

I'mthinkingofawaytobreakdownphrasesthathavemorethan20characters.andgetsomethinglikethis:

Theworstthingisthatinexcel,whenIclickonthetext(asIshouldwhenIeditit)itstaysthewayIwantitto.

Here'smycode.

//ignoremoloop,eleéparapegarotextoexatovindodotxtfor(intx=0;x<text.Length-2;x++){if(text[x].Length>20){//fazeraquebra...mascomo?app.Cells[i,j]=text[x+1].Trim();}else{app.Cells[i,j]=text[x+1].Trim();}}//Rangeusadoparaaformatação.RangeformatPaiP1=app.get_Range(letraColunaP1,BuscarAlfabeto(9)+(i).ToString());formatPaiP1.WrapText=true;formatPaiP1.EntireRow.AutoFit();formatPaiP1.VerticalAlignment=Constants.xlCenter;formatPaiP1.EntireColumn.AutoFit();formatPaiP1.Font.Bold=true;formatPaiP1.Font.Name="Arial";
    formatPaiP1.Font.Size = 12;
    
asked by anonymous 08.04.2016 / 20:11

1 answer

6

I solved the problem with the help of @rodorgas in an "alternative" way (read: technical fitness). I made the following change:

formatPaiP1.WrapText = true;
formatPaiP1.EntireRow.RowHeight = 33;

I noticed that when I used WrapText the line was 66 tall, breaking it in half, it excel fits correctly in all my cases.

This is a solution that worked exclusively for my project. I suppose you have the same problem you may not be able to adopt the same "logic".

Result:

Thank you all for the help.

    
23.05.2016 / 18:18