Migrating from Apache POI to Aspose.Cells for Java: Step-by-Step
1. Prepare the project
- Add Aspose.Cells for Java dependency (Maven):
xml
com.aspose aspose-cells latest - Remove or keep Apache POI dependencies as needed; avoid classpath conflicts.
2. Map core concepts
- Workbook (POI: XSSFWorkbook/HSSFWorkbook) → com.aspose.cells.Workbook
- Sheet (POI: Sheet) → com.aspose.cells.Worksheet
- Row/Cell (POI: Row/Cell) → com.aspose.cells.Cells and com.aspose.cells.Cell
- Styles: POI CellStyle → com.aspose.cells.Style / StyleFlag
- Formulas: POI evaluate → com.aspose.cells.Workbook.calculateFormula()
3. Open and save files
- POI:
java
Workbook wb = new XSSFWorkbook(new FileInputStream(“in.xlsx”)); - Aspose.Cells:
java
com.aspose.cells.Workbook wb = new com.aspose.cells.Workbook(“in.xlsx”);wb.save(“out.xlsx”);
4. Read and write cell values
- POI reading:
java
Cell cell = row.getCell(0);String v = cell.getStringCellValue(); - Aspose.Cells:
java
com.aspose.cells.Worksheet sheet = wb.getWorksheets().get(0);com.aspose.cells.Cells cells = sheet.getCells();String v = cells.get(“A1”).getStringValue(); - Writing:
java
cells.get(“A1”).putValue(“Hello”);
5. Styles and formatting
- Create/modify style:
java
Style style = cells.get(“A1”).getStyle();style.getFont().setName(“Arial”);style.getFont().setSize(12);cells.get(“A1”).setStyle(style); - Use StyleFlag to control which style attributes are applied.
6. Formulas and recalculation
- Set formula:
java
cells.get(“B1”).setFormula(“SUM(A1:A5)”);wb.calculateFormula(); - Read cached formula result with getDoubleValue()/getStringValue().
7. Named ranges, charts, pivot tables
- Named ranges:
java
wb.getWorksheets().getNames().add(“MyRange”, “Sheet1!A1:A10”); - Charts and pivot tables: Aspose provides builder APIs—create using Worksheet.getCharts() and PivotTableCollection.
8. Images, comments, hyperlinks
- Insert image:
java
int idx = sheet.getPictures().add(1, 1, “logo.png”);sheet.getPictures().get(idx).setPlacement(1); - Comments:
java
Comment comment = sheet.getComments().get(0);comment.setNote(“Note text”); - Hyperlinks:
9. Performance considerations
- Use WorkbookDesigner for template-based large report generation.
- For large files, avoid cell-by-cell operations when possible; use importArray or importDataTable.
- Turn off automatic formula calculation during bulk updates and call calculateFormula() once.
10. Error handling and compatibility
- Expect API differences in method names and behavior; test key flows (formats, formulas, macros).
- Validate saved files in target Excel versions.
- Handle unsupported features (very obscure POI-specific extensions) by reimplementing or exporting to compatible formats.
11. Licensing and deployment
- Aspose.Cells requires a commercial license for full functionality; test with evaluation mode and apply license in code:
java
com.aspose.cells.License lic = new com.aspose.cells.License();lic.setLicense(“Aspose.Cells.lic”);
12. Verification checklist
- Read/write basic sheets and cells — OK
- Styles and formats preserved — OK
- Formulas evaluate correctly — OK
- Charts/pivots render as expected — OK
- Large file performance acceptable — OK
- Licensing applied in production — OK
Quick migration example
java
// Loadcom.aspose.cells.Workbook wb = new com.aspose.cells.Workbook(“input.xlsx”);Worksheet ws = wb.getWorksheets().get(0);Cells cells = ws.getCells(); // ReadString name = cells.get(“A1”).getStringValue(); // Writecells.get(“B1”).putValue(123);wb.calculateFormula();wb.save(“output.xlsx”);
If you want, I can convert a specific POI code snippet you have into Aspose.Cells code.
Leave a Reply