Aspose Cells Java Auto Fit Problem

Hi I am trying to use autofit for a single cell but it is not working. But when I do the option of auto fit cell width it works. Can any body help ??


I have attached a sample excel file. The column contains the text “science fiction” for which I am trying autofit.


Hi,

Thanks for providing us template file and details.

Please try our latest version/fix: Aspose.Cells for Java v17.5.5 (attached)

I have tested your scenario/ case using the following sample code and it works fine. The output file is also attached which is fine tuned:
e.g.
Sample code:

Workbook book = new Workbook("Apple+(10).xlsx");
Worksheet sheet = book.getWorksheets().get(0);
sheet.autoFitColumns();
book.save("out1.xlsx");

If you still find any issue with v17.5.5, kindly paste your sample code with your output file, we will check it soon.

Thank you.

Hi Amzad. Thanks for replying and files. But I have one problem my project is in spring mvc. I am using maven and dependencies are present in pom.xml file. On maven repository version 17.5.5 is not available. The latest version available is 16.11.0. Can you help buddy ??

Hi,


We recommend you to kindly try our latest version/fix: Aspose.Cells for Java v17.5.x.

You may get the latest version (v17.5) from maven repo. here:
https://releases.aspose.com/java/repo/com/aspose/aspose-cells/

Thank you.

Hi Amjad.


Thanks for replying. The maven repostitory link given by you worked. I also wanted to know is there a way to apply autofit to a particular cell only. I used the update given by you. There is more data present in the file and I cannot tell you. But when I use the particular solution the column arrangement gets disturbed. So I wanted to ask whether there is a option of apply auto fit to a particular cell.

I tried using the option of auto fit column width in ms excel. It works fine. But when I do the same thing using code in Aspose it shrinks the cells below in that column.

Can you help ??

Hi,

Well, in MS Excel, auto-fit operation is based on complete row and column, you cannot implement auto-fit operation on one cell, you have to either auto-fit the complete column (cells) or complete row (cells). For your situation, I think you may autofit G column (index=6) only via Aspose.Cells APIs. It will not implement auto-fit operation to other columns in the worksheet. See the sample code for your reference:
e.g.
Sample code:

Workbook book = new Workbook("Apple+(10).xlsx");
Worksheet sheet = book.getWorksheets().get(0);

AutoFitterOptions options = new AutoFitterOptions();
sheet.autoFitColumn(6);//Autofit G column cells only.
book.save(“out1.xlsx”);

Let us know if you still have any issue or query.

Thank you.

Hi,

Thanks for using Aspose.Cells.

For your situation, if you only need to adjust column width or row height according to one cell’s value, you can try these methods:

  • Cell.getWidthOfValue ()
  • Cell.getHeightOfValue ()

Suppose, your cell contents are like this

  • B3: Sample Data.
  • B4: This is Sample Data.
  • B5: This is Sample Data With More Text.

If you will autofit the column, it will autofit according to cell B5, because B5 has maximum length.

But if you want to autofit the column according to cell B4, then you will autofit it like this. (Please see the sample Excel file and the output Excel file for your reference.)

Java
Workbook wb = new Workbook(dirPath + "sample.xlsx");

Worksheet ws = wb.getWorksheets().get(0);

Cell b4 = ws.getCells().get("B4");

ws.getCells().setColumnWidthPixel(b4.getColumn(), b4.getWidthOfValue());

wb.save(dirPath + "output.xlsx");

Hi Shakeel.


Thanks for replying early in the morning. This solution worked I gave width to the columns in which text width was more. It solved my issue. Thanks.