[POI] Problème d'insertion d'image
Bonjour,
Je travaille sur un projet et celui-ci m'amène à insérer une image qui est une copie d'écran d'un JComponent dans un fichier Excel. Après quelques recherches, j'ai finis par trouver un bon de code qui devrait fonctionner. Après tests et debug, celui-ci ne créer pas d'erreur et avec le pas à pas, à l'air de fonctionner correctement. Mais au final, mon image n'est pas afficher dans mon fichier excel, il n'y à même rien qui laisserait paraître un traitement quelconque sur le fichier Excel.
Voici le code :
Code:
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
public class SaveFile {
String savePath;
File file;
boolean isError = false;
public SaveFile(File file, WindowDebit frame, JTable table, JTable table2) throws IOException{
String val;
savePath = file.getAbsolutePath() + "\\" + frame.cbClient.getSelectedItem().toString() + "_Débit.xls";
File file2 = new File(savePath);
if(file2.exists()){
throw new IOException("Le nom de fichier : "+ frame.cbClient.getSelectedItem().toString() + "_Débit.xls" +" existe déjà dans ce répertoire\nLe sauvegarde du fichier à donc échoué");
}
InputStream inp = new FileInputStream("C:\\Users\\Utilisateur\\Documents\\Debit.xls");
HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
Sheet sheet = wb.getSheetAt(0);
// wb.removeSheetAt(1);
wb.setSheetName(0, "Débit");
/*Row row;
TableModel model = table.getModel();
TableModel model2 = table2.getModel();
sheet.getRow(0).createCell(1).setCellValue(frame.cbClient.getSelectedItem().toString());
sheet.getRow(0).getCell(1).setCellStyle(sheet.getRow(0).getCell(0).getCellStyle());
row = sheet.createRow(5);
row.createCell(0).setCellValue(1);
row.createCell(1).setCellValue(frame.cbTypeFenetre.getSelectedItem().toString());
row.createCell(2).setCellValue(Integer.parseInt(frame.cbNbVentail.getSelectedItem().toString()));
if(frame.cbOuverture.isEnabled())
row.createCell(3).setCellValue(frame.cbOuverture.getSelectedItem().toString());
else
row.createCell(3).setCellValue("Normal");
row.createCell(4).setCellValue(frame.cbTypeBois.getSelectedItem().toString());
row.createCell(5).setCellValue(frame.cbTypeVitrage.getSelectedItem().toString());
row.createCell(6).setCellValue(Integer.parseInt(frame.jtfNbFenetre.getText()));
row.createCell(7).setCellValue(frame.cbEndroit.getSelectedItem().toString());
row = sheet.getRow(sheet.getLastRowNum());
for (int i = 0; i < model.getColumnCount(); i++) {
row.createCell(i).setCellValue(model.getColumnName(i));
}
for (int i = 0; i < model.getRowCount(); i++) {
row = sheet.createRow(sheet.getLastRowNum() + 1);
for (int j = 0; j < model.getColumnCount(); j++) {
if(model.getValueAt(i, j) != null)
val = model.getValueAt(i, j).toString();
else
val = "0";
try{
row.createCell(j).setCellValue(Integer.parseInt(val));
}
catch (NumberFormatException e){
row.createCell(j).setCellValue(val);
}
}
}
row = sheet.createRow(sheet.getLastRowNum() + 2);
row.createCell(0).setCellValue("Vitre");
row = sheet.createRow(sheet.getLastRowNum() + 2);
for (int i = 0; i < model2.getColumnCount(); i++) {
row.createCell(i).setCellValue(model.getColumnName(i));
}
for (int i = 0; i < model2.getRowCount(); i++) {
row = sheet.createRow(sheet.getLastRowNum() + 1);
for (int j = 0; j < model2.getColumnCount(); j++) {
if(model.getValueAt(i, j) != null)
val = model2.getValueAt(i, j).toString();
else
val = "0";
try{
row.createCell(j).setCellValue(Integer.parseInt(val));
}
catch (NumberFormatException e){
row.createCell(j).setCellValue(val);
}
}
}*/
//Créer une BuffereRImage à partir du Graphic de panFenetre
BufferedImage img = new BufferedImage(frame.panFenetre.getWidth(), frame.panFenetre.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = img.createGraphics();
frame.paintAll(g);
g.dispose();
//Convertir la BufferedImage en byte[]
ByteArrayOutputStream bos = null;
try {
bos = new ByteArrayOutputStream();
ImageIO.write(img, "png", bos);
} finally {
try {
bos.close();
} catch (Exception e) {
}
}
byte[] b = bos.toByteArray();
//Ajouter l'image au Workbook et récupérer l'index de l'image
int pictureIdx = wb.addPicture(b, Workbook.PICTURE_TYPE_PNG);
//Créer un objet qui gère l'instanciation des classes concrète
CreationHelper helper = wb.getCreationHelper();
//Créer drawing patriarch de haut level
Drawing drawing = sheet.createDrawingPatriarch();
//Créer une anchor attacher à la worksheet
ClientAnchor anchor = helper.createClientAnchor();
//Placer le coin supérieur gauche de l'image
anchor.setCol1(UtilitaireXLS.NombreMaxColonne((HSSFSheet) sheet) + 2);
anchor.setRow1(2);
//Créer l'image
Picture pict = drawing.createPicture(anchor, pictureIdx);
// //Remettre l'image à sa taille originel
// pict.resize();
FileOutputStream fileOut;
try {
fileOut = new FileOutputStream(savePath);
wb.write(fileOut);
fileOut.close();
wb.close();
} catch (IOException e) {
System.out.println(e);
e.printStackTrace();
}
}
} |
Merci à ceux qui se pencheront sur le problème !
Je tiens juste à rajouter que ce qu'il y a en commentaire, fonctionne et insert bien ce que je veut. Mais il n'y a pas l'image demandée.