IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Documents Java Discussion :

Génération d'un document WORD => Entête avec POI


Sujet :

Documents Java

  1. #1
    Membre à l'essai
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2017
    Messages
    32
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Septembre 2017
    Messages : 32
    Points : 23
    Points
    23
    Par défaut Génération d'un document WORD => Entête avec POI
    Bonjour,

    n'ayant pas réussi à trouver de quoi exporter des données vers un modèle (ou plutôt un calque) .odt; je me suis résigné à créer mon document en .docx grâce à POI.

    C'est plutôt satisfaisant mais niveau maîtrise et compréhension suis encore bien light.

    Est-ce que quelqu'un pourrait prendre le temps de m'expliquer quelques lignes dans mon code (j'ai mis en commentaire ce que je n'avais pas compris) et surtout me dire comment est-ce que je peux afficher mon entête avec mon logo à droite de mon texte et uniquement sur la première page (une seule entete au début du document).

    Voici mon petit code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.math.BigInteger;
    import org.apache.poi.util.Units;
    import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
    import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    import org.apache.poi.xwpf.usermodel.XWPFHeader;
    import org.apache.poi.xwpf.usermodel.XWPFParagraph;
    import org.apache.poi.xwpf.usermodel.XWPFRun;
    import org.apache.poi.xwpf.usermodel.XWPFTable;
    import org.apache.poi.xwpf.usermodel.XWPFTableRow;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
     
     
     
     
    public class CreerDocument
    {
       public static void main(String[] args)throws Exception 
       {
          //////////////////////////////////////////////////////////////////////////////////////////////////      créer un nouveau document
          XWPFDocument document= new XWPFDocument(); 
     
          //créer un flux d'écriture pour enregistrer contenu dans nouveaudoc.docx
          FileOutputStream out = new FileOutputStream(
          new File("nouveaudoc.docx"));
     
          /////////////////////////////////////////////////////////////////////////////////////////////    on s'occupe des marges d'impression      
     
          CTSectPr sectPr = document.getDocument().getBody().addNewSectPr(); // visiblement je créée une section dans mon document????
          CTPageMar pageMar = sectPr.addNewPgMar(); // je créé des marges pour ma section
          pageMar.setLeft(BigInteger.valueOf(360));
          pageMar.setTop(BigInteger.valueOf(720));
          pageMar.setRight(BigInteger.valueOf(720));
          pageMar.setBottom(BigInteger.valueOf(720));
     
          //////////////////////////////////////////////////////////////////////////////////////////////////     création de l'entête
     
          // la classe HeaderFooterPolicy est celle qui me permet de créer une entête et un pied de page
          XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr); 
     
          // on créé notre entete
          XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT); //il y a FIRST et EVEN mais qu'est ce que ça veut dire?
     
          //je créé un paragraphe dans mon entête
     
          XWPFParagraph paragraphe = header.createParagraph();
          paragraphe.setAlignment(ParagraphAlignment.LEFT); // je l'aligne à gauche
          // un XWPFRun c'est un flux de texte qui va etre écrit à l'exécution. Sur ce run (cette exécution) propre au paragraphe on va appliquer des règles
          // éditer du texte, le formater etc.
          XWPFRun run = paragraphe.createRun(); 
          run.setText("Monsieur X \n \r Aspirant developeur \r 1, Rue du Code 00404 Error Cedex \r");
          run.setBold(true);
          run.setFontFamily("Arial");
          run.setFontSize(9);
     
          // sur ce run j'aimerai qu'il y ait une image d'insérée
          String monLogo ="logo.png";  
          // la méthode addPicture comprends 4 paramètres => un flux d'entrée, le type d'image, l'adresse de mon image, la largeur, la hauteur)
          run.addPicture (new FileInputStream((monLogo)),XWPFDocument.PICTURE_TYPE_PNG, monLogo,Units.toEMU (101),Units.toEMU (60)); 
     
     
          XWPFTable tableau = document.createTable(70, 5);
     
        //première ligne
          XWPFTableRow tableRowOne = tableau.getRow(0);
          tableRowOne.getCell(0).setText("un");
          tableRowOne.getCell(1).setText("deux");
          tableRowOne.getCell(2).setText("trois");
          tableRowOne.getCell(3).setText("quatre");
          tableRowOne.getCell(4).setText("cinq");
     
          XWPFTableRow tableRowTwo = tableau.getRow(1);
          tableRowTwo.getCell(0).setText("1");
          tableRowTwo.getCell(1).setText("2");
          tableRowTwo.getCell(2).setText("3");
          tableRowTwo.getCell(3).setText("4");
          tableRowTwo.getCell(4).setText("5");
     
        //mettre à jour le fichier nouveaudoc.docx
          document.write(out);
     
          //fermer le flux d'écriture
          out.close();
          System.out.println("les modifications ont été faites avec succès");
       }
    }

  2. #2
    Membre à l'essai
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2017
    Messages
    32
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Septembre 2017
    Messages : 32
    Points : 23
    Points
    23
    Par défaut
    J'ai un peu avancé je me suis dit pourquoi pas mettre ça dans un tableau.
    Bilan => ça fonctionne par contre j'ai une ligne vide au début de chaque cellule => comment la supprimer?

    J'ai toujours mon soucis de l'entête qui est encore présente sur la deuxième page.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.math.BigInteger;
    import org.apache.poi.util.Units;
    import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
    import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    import org.apache.poi.xwpf.usermodel.XWPFHeader;
    import org.apache.poi.xwpf.usermodel.XWPFParagraph;
    import org.apache.poi.xwpf.usermodel.XWPFRun;
    import org.apache.poi.xwpf.usermodel.XWPFTable;
    import org.apache.poi.xwpf.usermodel.XWPFTableRow;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
     
     
     
     
    public class CreerDocument
    {
       public static void main(String[] args)throws Exception 
       {
          //////////////////////////////////////////////////////////////////////////////////////////////////      créer un nouveau document
          XWPFDocument document= new XWPFDocument(); 
     
          //créer un flux d'écriture pour enregistrer contenu dans nouveaudoc.docx
          FileOutputStream out = new FileOutputStream(
          new File("nouveaudoc.docx"));
     
          /////////////////////////////////////////////////////////////////////////////////////////////    on s'occupe des marges d'impression      
     
          CTSectPr sectPr = document.getDocument().getBody().addNewSectPr(); // visiblement je créée une section dans mon document????
          CTPageMar pageMar = sectPr.addNewPgMar(); // je créé des marges pour ma section
          pageMar.setLeft(BigInteger.valueOf(360));
          pageMar.setTop(BigInteger.valueOf(720));
          pageMar.setRight(BigInteger.valueOf(720));
          pageMar.setBottom(BigInteger.valueOf(720));
     
          //////////////////////////////////////////////////////////////////////////////////////////////////     création de l'entête
     
          // la classe HeaderFooterPolicy est celle qui me permet de créer une entête et un pied de page
          XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr); 
     
          // on créé notre entete
          XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT); //il y a FIRST et EVEN mais qu'est ce que ça veut dire?
     
          //je créé un paragraphe dans mon entête
     
          XWPFTable tabEntete = header.createTable(1,2);
     
     
          XWPFTableRow ligneOne = tabEntete.getRow(0);
          //première colonne
          XWPFParagraph paragraphe = ligneOne.getCell(0).addParagraph();
          paragraphe.setAlignment(ParagraphAlignment.LEFT);
          XWPFRun run = paragraphe.createRun(); 
          run.setText("Monsieur X \n \r Aspirant developeur \r 1, Rue du Code 00404 Error Cedex \r");
          run.setBold(true);
          run.setFontFamily("Arial");
          run.setFontSize(9);
          run.addBreak();
     
          //deuxième colonne
          XWPFParagraph paragraphe2 = ligneOne.getCell(1).addParagraph();
          paragraphe2.setAlignment(ParagraphAlignment.RIGHT);
          run = paragraphe2.createRun(); 
          String monLogo ="logo.png";  
          run.addPicture (new FileInputStream((monLogo)),XWPFDocument.PICTURE_TYPE_PNG, monLogo,Units.toEMU (101),Units.toEMU (60));
     
     
     
          XWPFTable tableau = document.createTable(70, 5);
     
        //première ligne
          XWPFTableRow tableRowOne = tableau.getRow(0);
          tableRowOne.getCell(0).setText("un");
          tableRowOne.getCell(1).setText("deux");
          tableRowOne.getCell(2).setText("trois");
          tableRowOne.getCell(3).setText("quatre");
          tableRowOne.getCell(4).setText("cinq");
     
          XWPFTableRow tableRowTwo = tableau.getRow(1);
          tableRowTwo.getCell(0).setText("1");
          tableRowTwo.getCell(1).setText("2");
          tableRowTwo.getCell(2).setText("3");
          tableRowTwo.getCell(3).setText("4");
          tableRowTwo.getCell(4).setText("5");
     
        //mettre à jour le fichier nouveaudoc.docx
          document.write(out);
     
          //fermer le flux d'écriture
          out.close();
          System.out.println("les modifications ont été faites avec succès");
       }
    }

  3. #3
    Membre à l'essai
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2017
    Messages
    32
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Septembre 2017
    Messages : 32
    Points : 23
    Points
    23
    Par défaut
    La ligne vide au début de mon tableau est résolue.
    Quand j'appelle la méthode addParagraph une ligne vide est créée en rang 0 => je la supprime à chaque fois.

    Les \r de ma chaîne de caractère faisaient planter cette manipulation j'ai donc ajouter un break() à mes run pour changer de ligne sans un retour charriot.

    Pour l'entête sur la première page seulement et les lignes de codes que j'ai pas compris je sui toujours preneur d'une explication

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.math.BigInteger;
    import org.apache.poi.util.Units;
    import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
    import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    import org.apache.poi.xwpf.usermodel.XWPFHeader;
    import org.apache.poi.xwpf.usermodel.XWPFParagraph;
    import org.apache.poi.xwpf.usermodel.XWPFRun;
    import org.apache.poi.xwpf.usermodel.XWPFTable;
    import org.apache.poi.xwpf.usermodel.XWPFTableRow;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
     
     
     
     
    public class CreerDocument
    {
       public static void main(String[] args)throws Exception 
       {
          //////////////////////////////////////////////////////////////////////////////////////////////////      créer un nouveau document
          XWPFDocument document= new XWPFDocument(); 
     
          //créer un flux d'écriture pour enregistrer contenu dans nouveaudoc.docx
          FileOutputStream out = new FileOutputStream(
          new File("nouveaudoc.docx"));
     
          /////////////////////////////////////////////////////////////////////////////////////////////    on s'occupe des marges d'impression      
     
          CTSectPr sectPr = document.getDocument().getBody().addNewSectPr(); // visiblement je créée une section dans mon document????
          CTPageMar pageMar = sectPr.addNewPgMar(); // je créé des marges pour ma section
          pageMar.setLeft(BigInteger.valueOf(360));
          pageMar.setTop(BigInteger.valueOf(720));
          pageMar.setRight(BigInteger.valueOf(720));
          pageMar.setBottom(BigInteger.valueOf(720));
     
          //////////////////////////////////////////////////////////////////////////////////////////////////     création de l'entête
     
          // la classe HeaderFooterPolicy est celle qui me permet de créer une entête et un pied de page
          XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr); 
     
          // on créé notre entete
          XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT); //il y a FIRST et EVEN mais qu'est ce que ça veut dire?
     
          //je créé un paragraphe dans mon entête
     
          XWPFTable tabEntete = header.createTable(1,2);
     
     
          //première ligne
          XWPFTableRow ligneOne = tabEntete.getRow(0);
          XWPFParagraph paragraphe = ligneOne.getCell(0).addParagraph();      
          paragraphe.setAlignment(ParagraphAlignment.LEFT);
          XWPFRun run = paragraphe.createRun();
     
          run.setText("Monsieur X");
          run.addBreak();
          run.setText("Aspirant developeur");
          run.addBreak();
          run.setText("1, Rue du Code 00404 Error Cedex");
          run.addBreak();
          run.setBold(true);
          run.setFontFamily("Arial");
          run.setFontSize(9);
     
          ligneOne.getCell(0).removeParagraph(0);
     
     
          XWPFParagraph paragraphe2 = ligneOne.getCell(1).addParagraph();
          ligneOne.getCell(1).removeParagraph(0);
          paragraphe2.setAlignment(ParagraphAlignment.RIGHT);
          run = paragraphe2.createRun(); 
          String monLogo ="logo.png";  
          run.addPicture (new FileInputStream((monLogo)),XWPFDocument.PICTURE_TYPE_PNG, monLogo,Units.toEMU (101),Units.toEMU (60));
         // ligneOne.getCell(1).removeParagraph(0); // cette ligne me permet de supprimer la ligne vide avant l'image (  ligneOne.getCell(0).removeParagraph(0); par contre donne un résultat étrange si vous le rajoutez)
     
          XWPFTable tableau = document.createTable(70, 5);
     
        //première ligne
          XWPFTableRow tableRowOne = tableau.getRow(0);
          tableRowOne.getCell(0).setText("un");
          tableRowOne.getCell(1).setText("deux");
          tableRowOne.getCell(2).setText("trois");
          tableRowOne.getCell(3).setText("quatre");
          tableRowOne.getCell(4).setText("cinq");
     
          XWPFTableRow tableRowTwo = tableau.getRow(1);
          tableRowTwo.getCell(0).setText("1");
          tableRowTwo.getCell(1).setText("2");
          tableRowTwo.getCell(2).setText("3");
          tableRowTwo.getCell(3).setText("4");
          tableRowTwo.getCell(4).setText("5");
     
        //mettre à jour le fichier nouveaudoc.docx
          document.write(out);
     
          //fermer le flux d'écriture
          out.close();
          System.out.println("les modifications ont été faites avec succès");
       }
    }

  4. #4
    Membre à l'essai
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2017
    Messages
    32
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Septembre 2017
    Messages : 32
    Points : 23
    Points
    23
    Par défaut
    J'ai trouvé tout seul finalement pour l'entête.
    Pour ceux que ça intéresse voici un exemple du code: (j'ouvre un autre fil car j'ai un soucis pour les cellules des tableaux dans Apache POI ...de taille d'ailleurs ^^)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
     
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.math.BigInteger;
    import org.apache.poi.util.Units;
    import org.apache.poi.wp.usermodel.HeaderFooterType;
    import org.apache.poi.xwpf.usermodel.BreakType;
    import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    import org.apache.poi.xwpf.usermodel.XWPFHeader;
    import org.apache.poi.xwpf.usermodel.XWPFParagraph;
    import org.apache.poi.xwpf.usermodel.XWPFRun;
    import org.apache.poi.xwpf.usermodel.XWPFTable;
    import org.apache.poi.xwpf.usermodel.XWPFTableRow;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
     
     
     
    public class test
    {
       public static void main(String[] args)throws Exception 
       {
          //////////////////////////////////////////////////////////////////////////////////////////////////      créer un nouveau document
          XWPFDocument document= new XWPFDocument(); 
     
          //créer un flux d'écriture pour enregistrer contenu dans nouveaudoc.docx
          FileOutputStream out = new FileOutputStream(
          new File("nouveaudoc.docx"));
     
          /////////////////////////////////////////////////////////////////////////////////////////////    on s'occupe des marges d'impression      
     
          CTSectPr sectPr = document.getDocument().getBody().addNewSectPr(); // visiblement je créée une section dans mon document????
          CTPageMar pageMar = sectPr.addNewPgMar(); // je créé des marges pour ma section
          pageMar.setLeft(BigInteger.valueOf(850L));
          pageMar.setRight(BigInteger.valueOf(710L));
          pageMar.setTop(BigInteger.valueOf(0L)); // on laisse de la marge pour l'entete
          pageMar.setBottom(BigInteger.valueOf(565L));
          pageMar.setHeader(BigInteger.valueOf(130)); //on ajuste au besoin
          pageMar.setFooter(BigInteger.valueOf(565));   
     
          /////////////////////////////////////////////////////////////////////     création de l'entête de la première page 
     
          // la classe HeaderFooterPolicy est celle qui me permet de créer une entête et un pied de page
          XWPFHeader header = document.createHeader(HeaderFooterType.FIRST);
     
          // on créé notre entete      
          //je créé un paragraphe dans mon entête
     
          XWPFTable tabEntete = header.createTable(2,2);
          tabEntete.removeBorders();  
     
          //première ligne
          XWPFTableRow ligneOne = tabEntete.getRow(0);
          XWPFParagraph paragraphe = ligneOne.getCell(0).addParagraph();      
          paragraphe.setAlignment(ParagraphAlignment.LEFT);
          XWPFRun run = paragraphe.createRun();
     
          run.setText("");
          run.addBreak();
          run.setText("Ecole de programmation");
          run.addBreak();
          run.setText("JAVA POI");
          run.addBreak();
          run.setText("1, Rue du code 00404 Matrice Cedex");
          run.addBreak();
          run.setBold(true);
          run.setFontFamily("Arial");
          run.setFontSize(9);
     
          ligneOne.getCell(0).removeParagraph(0);
     
     
          XWPFParagraph paragraphe2 = ligneOne.getCell(1).addParagraph();
          ligneOne.getCell(1).removeParagraph(0);
          paragraphe2.setAlignment(ParagraphAlignment.RIGHT);
          run = paragraphe2.createRun(); 
          String monLogo ="logo.png";  
          run.addPicture (new FileInputStream((monLogo)),XWPFDocument.PICTURE_TYPE_PNG, monLogo,Units.toEMU (101),Units.toEMU (60));
     
     
     
        //deuxième ligne
          XWPFTableRow ligneTwo = tabEntete.getRow(1);
          XWPFParagraph paragraphe3 = ligneTwo.getCell(0).addParagraph();      
          paragraphe3.setAlignment(ParagraphAlignment.LEFT);
          run = paragraphe3.createRun();
     
     
          run.setText("Tel : 01.23.45.67.89");
          run.addBreak();
     
          run.setBold(true);
          run.setFontFamily("Arial");
          run.setFontSize(9);
     
          ligneTwo.getCell(0).removeParagraph(0);
     
          XWPFParagraph paragraphe4 = ligneTwo.getCell(1).addParagraph();      
          paragraphe4.setAlignment(ParagraphAlignment.CENTER);
          run = paragraphe4.createRun();
     
     
          run.setText("Version 0.1 de 06/2019");
          run.addBreak();          
          run.setBold(true);
          run.setFontFamily("Arial");
          run.setFontSize(9);
     
          ligneTwo.getCell(1).removeParagraph(0);
          run.addBreak(BreakType.PAGE);
     
     
       // create default page header qui est vide
          header = document.createHeader(HeaderFooterType.DEFAULT);
          paragraphe = header.createParagraph();
          paragraphe.setAlignment(ParagraphAlignment.LEFT);
          run = paragraphe.createRun();
          run.setText("");
          run.addBreak();
          run.setText("");
          run.addBreak();
          run.setText("");
          run.addBreak();
     
          XWPFTable tableau = document.createTable(1, 1);
     
        //premier tableau
          XWPFTableRow tableRowOne = tableau.getRow(0);
          tableRowOne.getCell(0).setText("Petit modèle créé à partir d'Apache POI");
     
          // première ligne sous le tableau
     
          paragraphe = document.createParagraph();
          run=paragraphe.createRun();  
          run.setText("Objectif : réussir à maîtriser l'extraction en docx et surtout manipuler les tableaux d'un writer...");
     
          XWPFTable tableau2 = document.createTable(1, 6);
          XWPFParagraph p = tableau2.getRow(0).getCell(0).addParagraph();  
          tableau2.getRow(0).getCell(0).setColor("a9a9a9");
     
     
          p.setAlignment(ParagraphAlignment.LEFT);
          run = p.createRun();
          run.setText("Ma première case que je voudrais plus large que celles d'à côté :");      
          run.addBreak();   
          run.setFontFamily("Arial");
          run.setFontSize(9);
          run.setColor("000000");
     
          tableau2.getRow(0).getCell(0).removeParagraph(0);      
     
          p = tableau2.getRow(0).getCell(1).addParagraph(); 
          tableau2.getRow(0).getCell(1).setColor("a9a9a9");
          p.setAlignment(ParagraphAlignment.LEFT);
          run = p.createRun();
          run.setText("case 1");      
          run.addBreak();    
          run.setFontFamily("Arial");
          run.setFontSize(9);
          run.setColor("ffffff");
          run.setStrikeThrough(true);
          tableau2.getRow(0).getCell(1).removeParagraph(0);
     
          p = tableau2.getRow(0).getCell(2).addParagraph(); 
          tableau2.getRow(0).getCell(2).setColor("a9a9a9");
          p.setAlignment(ParagraphAlignment.LEFT);
          run = p.createRun();
          run.setText("Case 2");      
          run.addBreak();   
          run.setFontFamily("Arial");
          run.setFontSize(9);
          run.setColor("ffffff");
          tableau2.getRow(0).getCell(2).removeParagraph(0);
     
          p = tableau2.getRow(0).getCell(3).addParagraph(); 
          tableau2.getRow(0).getCell(3).setColor("a9a9a9");
          p.setAlignment(ParagraphAlignment.LEFT);
          run = p.createRun();
          run.setText("Case 3");      
          run.addBreak();   
          run.setFontFamily("Arial");
          run.setFontSize(9);
          run.setColor("ffffff");
          run.setStrikeThrough(true);
          tableau2.getRow(0).getCell(3).removeParagraph(0);
     
          p = tableau2.getRow(0).getCell(4).addParagraph();
          tableau2.getRow(0).getCell(4).setColor("a9a9a9");
          p.setAlignment(ParagraphAlignment.LEFT);
          run = p.createRun();
          run.setText("Case 4");      
          run.addBreak();  
          run.setFontFamily("Arial");
          run.setFontSize(9);
          run.setColor("ffffff");
          run.setStrikeThrough(true);
          tableau2.getRow(0).getCell(4).removeParagraph(0);
     
          p = tableau2.getRow(0).getCell(5).addParagraph();  
          tableau2.getRow(0).getCell(5).setColor("a9a9a9");
          p.setAlignment(ParagraphAlignment.LEFT);
          run = p.createRun();
          run.setText("Case 5");      
          run.addBreak();  
          run.setFontFamily("Arial");
          run.setFontSize(9);
          run.setColor("ffffff");
          run.setStrikeThrough(true);
          tableau2.getRow(0).getCell(5).removeParagraph(0);
     
          tableau2.setBottomBorder(XWPFTable.XWPFBorderType.SINGLE, 20, 1, "000001");
          tableau2.setTopBorder(XWPFTable.XWPFBorderType.SINGLE, 20, 1, "000001");
          tableau2.setLeftBorder(XWPFTable.XWPFBorderType.SINGLE, 20, 1, "000001");
          tableau2.setRightBorder(XWPFTable.XWPFBorderType.SINGLE, 20, 1, "000001");
     
     
          XWPFTable tableau3 = document.createTable(1, 1);
          p = tableau3.getRow(0).getCell(0).addParagraph();  
          tableau3.getRow(0).getCell(0).setColor("a9a9a9");
          p.setAlignment(ParagraphAlignment.CENTER);
          run = p.createRun();
          run.setText("un nouveau tableau sous le premier que je veux garder aussi large que ça");      
          run.addBreak();   
          run.setFontFamily("Arial");
          run.setFontSize(9);
          run.setColor("ffffff");
          tableau3.getRow(0).getCell(0).removeParagraph(0);  
          tableau3.setBottomBorder(XWPFTable.XWPFBorderType.SINGLE, 20, 1, "000001");
          tableau3.setTopBorder(XWPFTable.XWPFBorderType.SINGLE, 20, 1, "000001");
          tableau3.setLeftBorder(XWPFTable.XWPFBorderType.SINGLE, 20, 1, "000001");
          tableau3.setRightBorder(XWPFTable.XWPFBorderType.SINGLE, 20, 1, "000001");
     
     
     
          //mettre à jour le fichier nouveaudoc.docx
     
          document.write(out);           
          //fermer le flux d'écriture
          out.close();
          document.close();
          System.out.println("les modifications ont été faites avec succès");
       }   
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 4
    Dernier message: 09/01/2017, 15h11
  2. Transformer un document word en pdf avec un activeX
    Par vikking dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 21/03/2012, 19h48
  3. [XL-2003] Ouvrir document word depuis excel avec boite de dialogue
    Par gtkill dans le forum Macros et VBA Excel
    Réponses: 6
    Dernier message: 22/08/2011, 18h50
  4. Upload Document word dans SharePoint avec le web service copy et JAVA
    Par -MielPops- dans le forum Développement Sharepoint
    Réponses: 0
    Dernier message: 26/04/2011, 10h40
  5. Génération d'un document word dynamiquement
    Par omo_albaraa dans le forum Documents
    Réponses: 3
    Dernier message: 15/01/2009, 20h21

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo