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
|
package org.apache.poi.ss.examples;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.util.Map;
import java.util.HashMap;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.ss.util.CellRangeAddressList;
public class Excel {
public static void main(String[] args) throws Exception {
Workbook wb;
if (args.length > 0 && args[0].equals("-xls")) {
wb = new HSSFWorkbook();
} else {
wb = new XSSFWorkbook();
}
Map<String, CellStyle> styles = createStyles(wb);
Sheet sheet = wb.createSheet("Configurateur E2 | Fichier de traçabilité");
sheet.setPrintGridlines(false);
sheet.setDisplayGridlines(false);
PrintSetup printSetup = sheet.getPrintSetup();
printSetup.setLandscape(true);
sheet.setFitToPage(true);
sheet.setHorizontallyCenter(true);
//Taille des colonnes
sheet.setColumnWidth(0, 15 * 256);
sheet.setColumnWidth(1, 15 * 256);
sheet.setColumnWidth(2, 15 * 256);
sheet.setColumnWidth(3, 15 * 256);
sheet.setColumnWidth(4, 15 * 256);
sheet.setColumnWidth(5, 15 * 256);
sheet.setColumnWidth(6, 15 * 256);
//Création des lignes à utiliser
Row titleRow = sheet.createRow(0);
Row row1 = sheet.createRow((short) 1);
Row row2 = sheet.createRow((short) 2);
Row row3 = sheet.createRow((short) 3);
Row row4 = sheet.createRow((short) 4);
//Création des cellulles et assigner les lignes aux cellules
titleRow.createCell(0).setCellStyle(styles.get("title")); //Créer une cellule et appliquer un style à cette cellule
row1.createCell(0).setCellStyle(styles.get("rowS"));
row2.createCell(0).setCellStyle(styles.get("rowS"));
row2.createCell(2).setCellStyle(styles.get("rowS"));
row3.createCell(0).setCellStyle(styles.get("rowS"));
row3.createCell(2).setCellStyle(styles.get("rowS"));
row4.createCell(0).setCellStyle(styles.get("rowS"));
row4.createCell(2).setCellStyle(styles.get("rowS"));
CellRangeAddressList addressList = new CellRangeAddressList(2, 2, 2, 2);
DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(new String[]{"10", "20", "30"});
DataValidation dataValidation = new HSSFDataValidation(addressList, dvConstraint);
dataValidation.setSuppressDropDownArrow(false);
sheet.addValidationData(dataValidation);
//Renseigner sur quelle cellule nous voulons écire
Cell titleCell = titleRow.getCell(0);
Cell row1Cell = row1.getCell(0);
Cell row2Cell = row2.getCell(0);
Cell row2Cell1 = row2.getCell(2);
Cell row3Cell = row3.getCell(0);
Cell row3Cell1 = row3.getCell(2);
Cell row4Cell = row4.getCell(0);
Cell row4Cell1 = row4.getCell(2);
//Texte qui est insérer dans telles cellules
titleCell.setCellValue("Fichier de traçabilité");
row1Cell.setCellValue("NOM DU PRODUIT");
row2Cell.setCellValue("N° référence");
row2Cell1.setCellValue("aa");
row3Cell.setCellValue("N° CI");
row3Cell1.setCellValue("bb");
row4Cell.setCellValue("N° MF");
row4Cell1.setCellValue("cc");
//Fusion des différentes cellulles
sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$F$1"));
sheet.addMergedRegion(CellRangeAddress.valueOf("$A$2:$F$2"));
sheet.addMergedRegion(CellRangeAddress.valueOf("$A$3:$B$3"));
sheet.addMergedRegion(CellRangeAddress.valueOf("$C$3:$D$3"));
sheet.addMergedRegion(CellRangeAddress.valueOf("$A$4:$B$4"));
sheet.addMergedRegion(CellRangeAddress.valueOf("$C$4:$D$4"));
sheet.addMergedRegion(CellRangeAddress.valueOf("$A$5:$B$5"));
sheet.addMergedRegion(CellRangeAddress.valueOf("$C$5:$D$5"));
//Création du fichier EXCEL
String file = "tracaE2.xls";
if (wb instanceof XSSFWorkbook) {
file += "x";
}
try (FileOutputStream out = new FileOutputStream(file)) {
wb.write(out);
}
}
/**
* cell styles used for formatting calendar sheets
*/
private static Map<String, CellStyle> createStyles(Workbook wb) {
Map<String, CellStyle> styles = new HashMap<>();
CellStyle style;
Font titleFont = wb.createFont();
titleFont.setFontHeightInPoints((short) 14);
titleFont.setFontName("Trebuchet MS");
style = wb.createCellStyle();
style.setFont(titleFont);
style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
style.setAlignment(HorizontalAlignment.CENTER_SELECTION);
styles.put("title", style);
style = wb.createCellStyle();
Font rowFont = wb.createFont();
rowFont.setFontHeightInPoints((short) 9);
rowFont.setFontName("Trebuchet MS");
style.setFont(rowFont);
style.setAlignment(HorizontalAlignment.CENTER);
styles.put("rowS", style);
return styles;
}
} |
Partager