Ecrire dans un dans fichier word/excel

Oneagaindoguys Messages postés 8 Date d'inscription vendredi 24 avril 2009 Statut Membre Dernière intervention 17 mars 2010 - 18 nov. 2009 à 20:59
Oneagaindoguys Messages postés 8 Date d'inscription vendredi 24 avril 2009 Statut Membre Dernière intervention 17 mars 2010 - 19 nov. 2009 à 08:26
Bonjour, après de nombreuses recherche sur le net, j'ai réussi à créer et enregistrer un fichier word et un fichier excel.

C'est bien joli, mais j'aimerais y ajouter du contenu. Pour l'instant, voici mon code (juste pour doc, xls, c'est pareil) :

public class DOCGenerator {

private JFileChooser jfc;
private int save;
private String path;
private POIFSFileSystem fs;
private HWPFDocument document;
private OutputStream out;
private String string;
private String [][] stringCollection;

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.

Merci d'avance

2 réponses

kaloway Messages postés 358 Date d'inscription jeudi 24 octobre 2002 Statut Membre Dernière intervention 13 avril 2020
19 nov. 2009 à 06:28
dans le cours de jm doudoux, il y a un chapitre consacré à POI.
0
Oneagaindoguys Messages postés 8 Date d'inscription vendredi 24 avril 2009 Statut Membre Dernière intervention 17 mars 2010 1
19 nov. 2009 à 08:26
Je l'ai en plus ! Je n'ai pas pensé à regarder. Merci
0
Rejoignez-nous