How to apply the column width along with word wrap

Ruchita 40 Reputation points
2024-10-01T12:15:41.6266667+00:00

I have an excel in which data is large i.e.2000 characters for a cell.Since no wrap was there I have added the wrap handling and extra code so that I am able to scroll properly.

But column width is not getting set.

It is 273+ only.Tried autofitcolumn as well but no luck.

Below is the code :

w_Val = "2000 CHARACTERS VALUE"
StringBuilder wrappedText = new StringBuilder();
int w_currentIndex = 0;
while (w_currentIndex < w_Val.length()) {
    int endIndex = Math.min(w_currentIndex + 200, w_Val.length());
    // If there is no space, ensure we don't split words in the middle
    if (endIndex < w_Val.length() && w_Val.charAt(endIndex) != ' ') {
       int lastSpace = w_Val.lastIndexOf(' ', endIndex);
       if (lastSpace > w_currentIndex) {
          endIndex = lastSpace;
       }
    }
    // Append the current line
    wrappedText.append(w_Val, w_currentIndex, endIndex).append("\n");
    // Move to the next part of the text
    w_currentIndex = endIndex;
    // Skip any spaces at the start of the next line
    while (w_currentIndex < w_Val.length() && w_Val.charAt(w_currentIndex) == ' ') {
       w_currentIndex++;
    }
}
w_Val = wrappedText.toString().trim();
a_cell.setValue(w_Val);
Style w_style = a_cell.getStyle();
w_style.setTextWrapped(true);
a_cell.setStyle(w_style);
a_workSheet.setColumnWidth(3, 150);
a_workSheet.calculateFormula(true, true);
Excel
Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
1,955 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,978 questions
0 comments No comments
{count} votes

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.