We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Formatting a cell to multi lines


I have a cell that has contents in a single line as following:

Pulse: 30, B.P: 10/180, Temp: 96, R/R: 100, Shifted To: ABC Hospital

What I want is to format it in the same cell with comma separated line breaks i.e. it should be arranged like following:

Pulse: 30,
B.P: 10/180,
Temp: 96,
R/R: 100,
Shifted To: ABC Hospital

Also, the same needs to be applied the whole column. Please provide runnable sample code.


Thanks for your query.

See the following sample code with comments to accomplish your task for your requirements:
Sample code:

    Workbook workbook = new Workbook();
    //Get the first worksheet (default worksheet).
    Worksheet worksheet = workbook.Worksheets[0];

    //Create a Style object
    Style style = workbook.CreateStyle();
    //Set the wrapped text attribute to true.
    style.IsTextWrapped = true;
    //Create a StyleFlag object.
    Aspose.Cells.StyleFlag flag = new StyleFlag();
    //Making the wrapped text on.
    flag.WrapText = true;

    //Apply the style to the whole column (A).
    worksheet.Cells.Columns[0].ApplyStyle(style, flag);

    //Put your desired text into the A1.
    //Here make sure to use "\n" in the text where you want to place break.
    worksheet.Cells["A1"].PutValue("Pulse: 30, \nB.P: 10/180, \nTemp: 96, \nR/R: 100, \nShifted To: ABC Hospital");

     //You need to extend the column width (A) a bit.
    worksheet.Cells.SetColumnWidth(0, 22);
    //Auto-fit respective row to extend the row height accordingly.


Hope, this helps a bit.

Thank you.


Thank you for sample code. We are loading from DB and then reflecting the DataSource directly to the Worksheet. Will this also work in that case?


Well the text would be wrapped in the column cells (for sure) but as I told you to insert line break at your desired position in the text, you got to use/insert ā€œ\nā€ (new line - carriage return) accordingly in the source data/text, so Aspose.Cells would understand and set line breaks at your desired positions in the text. If you do not put carriage return char, wrapping text would be done randomly and automatically.