API JExcel - Lire et écrire un fichier Excel en Java
Dans cet article, nous discuterons de la lecture et de l'écriture d'un fichier Excel à l'aide de l'API JExcel, une bibliothèque simple et largement utilisée pour des opérations simples qui n'impliquent pas un niveau élevé de mise en forme et des opérations complexes basées sur des formules.
P.S Tested with JExcel API – 2.6.12
1. Télécharger JExcel
Utilisateur Maven.
pom.xml
net.sourceforge.jexcelapi jxl 2.6.12
Ou téléchargez directement àthis location
2. Écrire un fichier Excel
Exemple d'API JExcel pour vous montrer comment créer un fichier Excel et y écrire des données.
ExcelWrite.java
package com.techfou; import jxl.Workbook; import jxl.write.*; import jxl.write.Number; import java.io.File; import java.io.IOException; public class ExcelWrite { private static final String EXCEL_FILE_LOCATION = "C:\\temp\\MyFirstExcel.xls"; public static void main(String[] args) { //1. Create an Excel file WritableWorkbook myFirstWbook = null; try { myFirstWbook = Workbook.createWorkbook(new File(EXCEL_FILE_LOCATION)); // create an Excel sheet WritableSheet excelSheet = myFirstWbook.createSheet("Sheet 1", 0); // add something into the Excel sheet Label label = new Label(0, 0, "Test Count"); excelSheet.addCell(label); Number number = new Number(0, 1, 1); excelSheet.addCell(number); label = new Label(1, 0, "Result"); excelSheet.addCell(label); label = new Label(1, 1, "Passed"); excelSheet.addCell(label); number = new Number(0, 2, 2); excelSheet.addCell(number); label = new Label(1, 2, "Passed 2"); excelSheet.addCell(label); myFirstWbook.write(); } catch (IOException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } finally { if (myFirstWbook != null) { try { myFirstWbook.close(); } catch (IOException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } } } }
En sortie, un fichier Excel est créé avec le contenu suivant:
3. Lire le fichier Excel
Exemple à lire ci-dessus fichier Excel.
ExcelRead.java
package com.techfou; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import java.io.File; import java.io.IOException; public class ExcelRead { private static final String EXCEL_FILE_LOCATION = "C:\\temp\\MyFirstExcel.xls"; public static void main(String[] args) { Workbook workbook = null; try { workbook = Workbook.getWorkbook(new File(EXCEL_FILE_LOCATION)); Sheet sheet = workbook.getSheet(0); Cell cell1 = sheet.getCell(0, 0); System.out.print(cell1.getContents() + ":"); // Test Count + : Cell cell2 = sheet.getCell(0, 1); System.out.println(cell2.getContents()); // 1 Cell cell3 = sheet.getCell(1, 0); System.out.print(cell3.getContents() + ":"); // Result + : Cell cell4 = sheet.getCell(1, 1); System.out.println(cell4.getContents()); // Passed System.out.print(cell1.getContents() + ":"); // Test Count + : cell2 = sheet.getCell(0, 2); System.out.println(cell2.getContents()); // 2 System.out.print(cell3.getContents() + ":"); // Result + : cell4 = sheet.getCell(1, 2); System.out.println(cell4.getContents()); // Passed 2 } catch (IOException e) { e.printStackTrace(); } catch (BiffException e) { e.printStackTrace(); } finally { if (workbook != null) { workbook.close(); } } } }
Le code ci-dessus est presque compréhensible par lui-même. Chaque cellule ou feuille est mappée en tant qu'objet en Java. Dans le code ci-dessus, nous avons utilisé le jar JExcel pour obtenir la feuille de calcul écrite. En exécutant le code, la sortie est obtenue comme ci-dessous:
Test Count:1 Result:Passed Test Count:2 Result:Passed 2
4. Ajouter une mise en forme au fichier Excel
Un exemple peut être encore amélioré en ajoutant une mise en forme. Un code court pour ajouter une mise en forme a été présenté ci-dessous:
ExcelFormat .java
package com.example; import jxl.Workbook; import jxl.write.*; import jxl.write.Number; import java.io.File; import java.io.IOException; public class ExcelFormat { private static final String EXCEL_FILE_LOCATION = "C:\\temp\\MyFormattedExcel.xls"; public static void main(String[] args) { //1. Create an Excel file WritableWorkbook mySecondWbook = null; try { mySecondWbook = Workbook.createWorkbook(new File(EXCEL_FILE_LOCATION)); WritableSheet myFirstSheet = mySecondWbook.createSheet("Sheet 1", 0); WritableCellFormat cFormat = new WritableCellFormat(); WritableFont font = new WritableFont(WritableFont.ARIAL, 16, WritableFont.BOLD); cFormat.setFont(font); Label label = new Label(0, 0, "Test Count", cFormat); myFirstSheet.addCell(label); Number number = new Number(0, 1, 1); myFirstSheet.addCell(number); label = new Label(1, 0, "Result", cFormat); myFirstSheet.addCell(label); label = new Label(1, 1, "Passed"); myFirstSheet.addCell(label); number = new Number(0, 2, 2); myFirstSheet.addCell(number); label = new Label(1, 2, "Passed 2"); myFirstSheet.addCell(label); mySecondWbook.write(); } catch (WriteException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (mySecondWbook != null) { try { mySecondWbook.close(); } catch (IOException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } } } }
Sortie
Le code formate l'en-tête en police Arial, 16px, Gras. Il existe plusieurs autres polices et tailles prises en charge disponibles à explorer. Il existe de nombreuses fonctionnalités supplémentaires de JExcel qui peuvent être utilisées pour créer un Excel encore plus raffiné et bien formaté. Cet article vous donne une longueur d'avance. Reportez-vous aux liens suivants dans les références pour aller plus vite.