public DOCGenerator () throws FileNotFoundException, IOException {
this.jfc = new JFileChooser();//choisir où le fichier sera enregistré
this.save = jfc.showSaveDialog(null);
boolean wanted = this.getModel(save);//ouvre un fichier word vide
if (wanted) {
createDocFile();
closeDocFile();
}
}
//si le contenu à écrire est un texte
public DOCGenerator (String str) throws FileNotFoundException, IOException {
new DOCGenerator();
this.string = str;
}
//si le contenu est un tableau
public DOCGenerator (String[][] str) throws FileNotFoundException, IOException {
new DOCGenerator();
this.stringCollection = str;
}
//ouvre le modèle word (fichier créé par word) (vide)
public boolean getModel(int choice) throws IOException {
boolean ret;
if(choice==JFileChooser.APPROVE_OPTION){
path = jfc.getSelectedFile().getPath();
fs = new POIFSFileSystem(new FileInputStream("./ressources/wordModel.doc"));
ret = true;
}
else ret = false;
return ret;
}
//permet de conserver le format word
public void createDocFile() throws IOException {
document = new HWPFDocument(fs);
}
public void writeIntoDocFile () {
Range range = document.getRange();
}
//enregistre le modifications
public void closeDocFile () throws IOException {
out = new FileOutputStream(new File(path+".doc"));
document.write(out);
out.flush();
out.close();
}
}
J'ai vu ce code sur le net :
Range range = doc.getRange();
for (int i = 0; i < range.numSections(); i++)
{
Section section = range.getSection(i);
for (int x = 0; x < section.numParagraphs(); x++)
{
Paragraph p = section.getParagraph(x);
for (int z = 0; z < p.numCharacterRuns(); z++)
{
//character run
CharacterRun run = p.getCharacterRun(z);
//character run text
String text = run.text();
if( text.contains("DEMANDE"))
{
run.insertAfter("Eulu");
}
}
}
}
mais je ne comprend pas ce que ça fait.
En plus, c'est un tableau que j'ai besoin d'écrire, donc si jamais vous connaissez un tuto ou un bout de code fait par quelqu'un ce serait cool.