Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionimport java.awt.Color; import java.awt.Desktop; import java.io.File; import java.io.IOException; import java.util.ArrayList; import javax.swing.JFileChooser; import javax.swing.table.AbstractTableModel; import jxl.CellView; import jxl.Sheet; import jxl.Workbook; import jxl.format.Border; import jxl.format.BorderLineStyle; import jxl.format.Colour; import jxl.write.Label; import jxl.write.WritableCell; import jxl.write.WritableCellFormat; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; //......... déclaration de la classe public static void enregistrerDevis(JTable modelTab, String nomFichier){ AbstractTableModel modelTab String chemin; JFileChooser chooser = new JFileChooser("JFileChooser"); chooser.setApproveButtonText("Enregistrer ici"); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { chemin = chooser.getSelectedFile().getAbsolutePath(); try { // Création du fichier WritableWorkbook workBook = Workbook.createWorkbook(new File(chemin + "\" + nomFichier + ".xls")); WritableSheet sheet = workBook.createSheet("Feuille 1", 0); Label label; WritableCellFormat cellFormat; cellFormat = new WritableCellFormat(); cellFormat.setBackground(Colour.GREY_25_PERCENT); cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN); for(int col = 0 ; col < modelTab.getColumnCount(); col++) { label = new Label(col, 0, (String) modelTab.getColumnName(col), cellFormat); sheet.addCell(label); } WritableCellFormat cellFormat2 = new WritableCellFormat(); cellFormat2.setBackground(Colour.WHITE); cellFormat2.setBorder(Border.ALL, BorderLineStyle.THIN); int dernLig = 0; for(int lig = 0 ; lig < modelTab.getRowCount(); lig++) { for(int col = 0 ; col < modelTab.getColumnCount() ; col++) { label = new Label(col, lig+1, (String) modelTab.getValueAt(lig, col), cellFormat2); sheet.addCell(label); } dernLig = lig+1; } CellView cell = new CellView(); for(int col = 0 ; col < sheet.getColumns() ; col++) { cell = sheet.getColumnView(col); cell.setAutosize(true); sheet.setColumnView(col, cell); } workBook.write(); // On ferme le fichier Excel workBook.close(); if ( Desktop.isDesktopSupported() ) { Desktop desktop = Desktop.getDesktop(); if (desktop.isSupported(Desktop.Action.OPEN)) { try { desktop.open(new File(chemin + "\" + nomFichier + ".xls")); } catch (IOException e) { // TODO Auto-generated catch block } } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }