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
| public static void main(String[] args) {
String path = "D:\\test\\test_alphabet";
final String jpg = ".jpg";
File dirPicture = new File(path);
File[] tabFichier = dirPicture.listFiles(new FilenameFilter() {
public boolean accept(@SuppressWarnings("unused") File arg0, String arg1) {
return arg1.indexOf(jpg) > 0;
}
});
Arrays.sort(tabFichier, new Comparator<File>() {
public int compare(File o1, File o2) {
return StringUtils.compareTo(o1.getName(), o2.getName());
}
});
try {
FileOutputStream fileOut = new FileOutputStream("D:\\test\\poi-test.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet worksheet = workbook.createSheet("POI Worksheet");
int i = 0;
HSSFPatriarch patriarch=worksheet.createDrawingPatriarch();
for (File fileImg : tabFichier) {
InputStream is = new FileInputStream(fileImg);
byte[] bytes = IOUtils.toByteArray(is);
int pictureIdx = workbook.addPicture(bytes, HSSFWorkbook.PICTURE_TYPE_JPEG);
is.close();
HSSFClientAnchor anchor = new HSSFClientAnchor();
anchor.setAnchorType(0);
anchor.setCol1(0);
anchor.setCol2(1);
anchor.setRow1(i);
anchor.setRow2(i+1);
patriarch.createPicture(anchor, pictureIdx);
HSSFCellStyle cellStyle = workbook.createCellStyle();
HSSFRow row = worksheet.createRow(i);
HSSFCell cellB = row.createCell(1);
cellB.setCellType(Cell.CELL_TYPE_STRING);
cellB.setCellValue(i);
cellB.setCellStyle(cellStyle);
HSSFCell cellC = row.createCell(2);
cellC.setCellType(Cell.CELL_TYPE_NUMERIC);
cellC.setCellValue(fileImg.getName().substring(0, fileImg.getName().indexOf(jpg)));
cellC.setCellStyle(cellStyle);
i++;
}
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
} |
Partager