Bonjour,

je voudrais exporter une jtable verse excel, j'ai fait une recherche sur google et j'ai trouvé un code qui a résolu mon problème principale (export).

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 
try {
                        HSSFWorkbook fWorkbook = new HSSFWorkbook();
                        HSSFSheet fSheet;
                        fSheet = fWorkbook.createSheet("new Sheet");
                        HSSFFont sheetTitleFont = fWorkbook.createFont();
                        File file = new File("C:\\Users\\tosiba\\Desktop\\projet\\Nabila\\liste.xls");        
 
                        HSSFCellStyle cellStyle = fWorkbook.createCellStyle();
                        sheetTitleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
                        //sheetTitleFont.setColor();
                       TableModel model = table.getModel();
 
                       TableColumnModel model1 = table.getTableHeader().getColumnModel();
 
                        HSSFRow fRow1 = fSheet.createRow((short) 0);
                       for (int i = 0; i < model1.getColumnCount(); i++){
                            HSSFCell cell = fRow1.createCell((short) i);
                           cell.setCellValue(model1.getColumn(i).getHeaderValue().toString());           
 
                }
                       for (int i = 1; i < model.getRowCount(); i++) {
                            HSSFRow fRow = fSheet.createRow((short) i);
                            for (int j = 0; j < model.getColumnCount(); j++) {
                                HSSFCell cell = fRow.createCell((short) j);
                               cell.setCellValue(Objects.toString(model.getValueAt(i, j), ""));
                                cell.setCellStyle(cellStyle);
                            }
                        }
                        FileOutputStream fileOutputStream;
                        fileOutputStream = new FileOutputStream(file);
                        try (BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream)) {
                            fWorkbook.write(bos);
                        }
                        fileOutputStream.close();
                    } catch (Exception e) {
                        e.printStackTrace();
Dans le fichier excel, la premiére ligne de ma table n'est pas exporter c'est à dire,j'ai le header et les autres lignes sauf la premiére. j'ai essayé de changé l'indice de i=1 dans cette partie du code , mais en faisant ça le header n'apparait pas dans le fichier excel
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
for (int i = 1; i < model.getRowCount(); i++) {
                            HSSFRow fRow = fSheet.createRow((short) i);
                            for (int j = 0; j < model.getColumnCount(); j++) {
                                HSSFCell cell = fRow.createCell((short) j);
                               cell.setCellValue(Objects.toString(model.getValueAt(i, j), ""));
                                cell.setCellStyle(cellStyle);
                            }
Merci