import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Excel {
public static void main(String[] args) {
//1. Créer un Document vide
XSSFWorkbook wb = new XSSFWorkbook();
//2. Créer une Feuille de calcul vide
Sheet feuille = wb.createSheet("new sheet");
//3. Créer une ligne et mettre qlq chose dedans
Row row = feuille.createRow((short)0);
//4. Créer une Nouvelle cellule
Cell cell = row.createCell(0);
//5. Donner la valeur
cell.setCellValue(1.2);
//Ajouter d'autre cellule avec différents type
/*int*/row.createCell(1).setCellValue(3);
/*char*/row.createCell(2).setCellValue('c');
/*String*/row.createCell(3).setCellValue("chaine");
/*boolean*/row.createCell(4).setCellValue(false);
FileOutputStream fileOut;
try {
fileOut = new FileOutputStream("nouveauFichier.xlsx");
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//insertion dans la cellule F1
cell = row.createCell((short) 6);
cell.setCellValue(new Date());
XSSFCellStyle cellStyle = wb.createCellStyle();
XSSFDataFormat xssfDataFormat = wb.createDataFormat();
//créer un Format date et heure
cellStyle.setDataFormat(xssfDataFormat.getFormat("dd/mm/yyyy h:mm"));
cell.setCellStyle(cellStyle);
/*créer un nouveau font*/
Font font = wb.createFont();
//taille: 12px
font.setFontHeightInPoints((short)12);
font.setFontName("Courier New");
font.setItalic(true);
font.setBold(true);
/*création d'un nouveau style*/
CellStyle cs = wb.createCellStyle();
cs.setFont(font);
//appliquer le style à la cellule 3(D1)
row.getCell(3).setCellStyle(cs);
/*changer la couleur de l'arrière plan*/
XSSFCellStyle csCouleur = wb.createCellStyle();
csCouleur.setFillForegroundColor(new XSSFColor(new Color(194, 154, 250)));
csCouleur.setFillPattern(csCouleur.SOLID_FOREGROUND);
//appliquer le style à la cellule 3
row.getCell(2).setCellStyle(csCouleur);
/*changer la couleur de la police*/
Font font = wb.createFont();
font.setColor((short)45);
CellStyle csCF = wb.createCellStyle();
csCF.setFont(font);
//appliquer le style à la cellule 0
row.getCell(0).setCellStyle(csCF);
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
public class Fusion{
public static void main(String[] args) throws FileNotFoundException {
Workbook wb = new HSSFWorkbook();
Sheet feuille = wb.createSheet("feuille1");
Row row = feuille.createRow((short) 1);
Cell cell = row.createCell((short) 1);
cell.setCellValue("test du fusion des cellules");
feuille.addMergedRegion(new CellRangeAddress(
1, //première ligne B2
2, //dernière ligne B3
1, //première colonne C2
2 //dernière colonne C3
));
/*Centrer*/
cell.getCellStyle().setAlignment((short)2);
cell.getCellStyle().setVerticalAlignment((short)1);
FileOutputStream fs = null;
try {
fs = new FileOutputStream("testFusion.xlsx");
wb.write(fs);
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Excel_formule {
public static void main(String[] args) {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet feuille = wb.createSheet("Moyenne");
Row row = feuille.createRow((short) 0);
row.createCell(0).setCellValue("Trimestre1");
row.createCell(1).setCellValue("Trimestre2");
row.createCell(2).setCellValue("Trimestre3");
row.createCell(3).setCellValue("Trimestre4");
row.createCell(4).setCellValue("Moyenne");
Row row1 = feuille.createRow((short) 1);
row1.createCell(0).setCellValue(2);
row1.createCell(1).setCellValue(5);
row1.createCell(2).setCellValue(1);
row1.createCell(3).setCellValue(7);
row1.createCell(4).setCellFormula("(A2+B2+C2+D2)/4");
try {
FileOutputStream out = new FileOutputStream(new File("formuletest.xlsx"));
wb.write(out);
out.close();
System.out.println("Le fichier excel a été créé avec succés");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.File;Sortie
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Excel_parcourir {
public static void main(String[] args) throws IOException {
FileInputStream fichier = new FileInputStream(new File("formuletest.xlsx"));
//créer une instance workbook qui fait référence au fichier xlsx
XSSFWorkbook wb = new XSSFWorkbook(fichier);
XSSFSheet sheet = wb.getSheetAt(0);
FormulaEvaluator formulaEvaluator =
wb.getCreationHelper().createFormulaEvaluator();
for (Row ligne : sheet) {//parcourir les lignes
for (Cell cell : ligne) {//parcourir les colonnes
//évaluer le type de la cellule
switch (formulaEvaluator.evaluateInCell(cell).getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
}
}
System.out.println();
}
}
}
Trimestre1 Trimestre2 Trimestre3 Trimestre4 Moyenne
2.0 5.0 1.0 7.0 3.75
Please disable your ad blocker and refresh the window to use this website.