Free Support Forum - aspose.com

Aspose.Cells for java: migration from v 2.* to 7.*

Hi,

Is there some document describing the API changes between Aspose.Cells 2.* and 7.* for Java ?
Kind of a migration guide would be appreciated.
At the time being, I need to know the equivalent for:

  1. AsposeLicenseException
  2. cell.getDoubleValue().isNaN()
  3. HorizontalAlignmentType
  4. VerticalAlignmentType
  5. FindOptions.SEARCH_BY_ROWS
  6. FindOptions.SEARCH_BY_COLUMNS
  7. FindOptions.MATCH_ENTIRE
  8. new Font()
  9. Style.setFont(Font)
  10. Cells.setRangeStyle(int, int, int, int, Style)
  11. Plus there might be some other changes that I did not stumble upon yet and that are not easy to find.
Also, I regret that the Workbook class is now throwing a lot of untyped java.lang.Exception. This is not very good practice and it is forcing us to catch and guess. Any plan to move back to specialized exceptions?

Thanks and regards,
Vincent

Hi,


We have changed API structure to equally match with .NET version of the product for APIs, features set, performance.

See the release notes of v7.0.0 to know about all the details and check especially, “Notable Changes for the Existing Users” table to know more about the changed APIs set:
http://www.aspose.com/community/files/72/java-components/aspose.cells-for-java/entry325010.aspx

1) You may try-catch and handle the main com.aspose.cells.CellsException class and you may use ExceptionType.Exception member now.

2) You may use com.aspose.cells.Cell.getDoubleValue() method that returns double type. Also, you may use Cell.getType() method that returns different CellValueType members for your need.

3), 4) Please use TextAlignmentType instead. The HorizontalAlignmentType and VerticalAlignmentType API is changed/renamed to become TextAlignmentType now, see the document for your reference:
http://www.aspose.com/documentation/java-components/aspose.cells-for-java/configuring-alignment-settings.html

5), 6), 7) See the sample code with comments:
//Instantiate the workbook object
Workbook workbook = new Workbook(“C:\book1.xls”);

//Get Cells collection
Cells cells = workbook.getWorksheets().get(0).getCells();

//Instantiate FindOptions Object
FindOptions findOptions = new FindOptions();

//Create a Cells Area
CellArea ca = new CellArea();
ca.StartRow = 8;
ca.StartColumn = 2;
ca.EndRow = 17;
ca.EndColumn = 13;

//Set cells area for find options
findOptions.setRange(ca);

//Set searching properties
findOptions.setSearchNext(true);

findOptions.setSeachOrderByRows(true); //If false then search order would be by columns.

findOptions.setLookAtType(LookAtType.ENTIRE_CONTENT);

findOptions.setLookInType(LookInType.VALUES); //MATCH_ENTIRE

//Find the cell with 0 value
Cell cell = cells.find(0, null, findOptions);

8) Check the following code to see how to set font using new versions.
//Instantiating a Workbook object
Workbook workbook = new Workbook();

//Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.getWorksheets().get(0);

//Accessing the “A1” cell from the worksheet
Cell cell = worksheet.getCells().get(“A1”);

//Adding some value to the “A1” cell
cell.putValue(“Hello Aspose!”);

Font font = cell.getStyle().getFont();

//Setting the font name to "Times New Roman"
font.setName(“Times New Roman”);

//Setting font size to 14
font.setSize(14);

//setting font color as Red
font.setColor(com.aspose.cells.Color.getRed());

//Saving the Excel file
workbook.save(“d:\dest.xls”);

9) No need to use (there is no setFont() method anymore) Style.setFont() method as you only need to get font of the cell’s style and change it according to your need, it will be applied directly. See the document for your complete reference:
http://www.aspose.com/documentation/java-components/aspose.cells-for-java/dealing-with-font-settings.html

10) Please create a Range object as per your desired area. Now, create Style object with your desired style formatting. Lastly, use Range.setStyle() method for your need.

We recommend you to kindly see the online Aspose.Cells for Java documentation for your complete reference (it is up-to-date):
and


"Also, I regret that the Workbook class is now throwing a lot of untyped java.lang.Exception. This is not very good practice and it is forcing us to catch and guess. Any plan to move back to specialized exceptions?"

Please try our latest version (v7.0.2) in which we have enhanced the Exceptional handling further.
http://www.aspose.com/community/files/72/java-components/aspose.cells-for-java/entry335888.aspx

Hope, this helps.

Thank you.

2), 3), 4), 5), 6), 7), 8), 9)
OK

10)
Range.setStyle() is deprecated. I can use applyStyle() but I don't know what StyleFlag to use.

1)
So I understand that AsposeLicensexception has been replaced with a generic RuntimeException with an error code that can be interpreted.
So now I need to "guess" there is a RuntimeException that can catch, and then use getCode()... Or just remove the try/catch block.

General exceptions management)
In v7.0.2 the Workbook class is still throwing java.lang.Exception on constructor and on save(). In previous versions we used to have IOException. Now I need to catch all the Exceptions, including RuntimeException... As my code exists and is based o throwing IOException, I cannot change all my methods signatures and I need to catch your Exception and use it to build a new IOExcpetion to be thrown.
I insist that this new API does not comply with general Java best practices.
http://today.java.net/article/2006/04/04/exception-handling-antipatterns#throwingException

Regards

VR

PS: Cell.getDoubleValue() will now throw a RuntimeException if the cell is not a numeric, when the same was returning null in the older API. This will definitely introduce regressions in our existing code...

Hi,


10) Please see the following sample code on how to use Range.applyStyle(style, styleFlag) method.

Sample code:
Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);

Cells cells = sheet.getCells();
cells.get(“A1”).putValue(“Column1”);
cells.get(“B1”).putValue(“Column2”);
Style style = workbook.getStyles().get(workbook.getStyles().add());
style.setForegroundColor(Color.getYellow());
style.setPattern(BackgroundType.SOLID);
StyleFlag flag = new StyleFlag();
flag.setCellShading(true);

Range range = sheet.getCells().createRange(“A1”, “B1”);
range.applyStyle(style, flag);

workbook.save(“range_style_book.xlsx”);


Thank you.