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
|
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();
}
} |
Partager