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

API standards et tierces Java Discussion :

Apache POI. Tableau Duplication du texte


Sujet :

API standards et tierces Java

  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Décembre 2009
    Messages
    265
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2009
    Messages : 265
    Points : 149
    Points
    149
    Par défaut Apache POI. Tableau Duplication du texte
    Bonjour,

    Avant toute chose, je dois dire que c'est mon premier essai de Apache POI et je me suis inspiré de quelques exemples trouvés sur le net.


    J'essaie de créer un tableau de deux colonnes. Cela ne marche pas trop mal, mais j'ai un problème:
    Lorsque je remplis les cellules en utilisant un paragraphe, le texte va bien dans la cellule désignée, mais est également dupliqué sous le tableau.



    Merci d'avance

    Gégé

    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
     
    package test;
     
    import gegeutil.Gegetools;
     
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintStream;
    import java.math.BigInteger;
    import java.nio.charset.StandardCharsets;
    import java.util.List;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblLayoutType;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblLayoutType;
     import org.apache.poi.xwpf.usermodel.XWPFDocument;
    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.XWPFTableCell;
    import org.apache.poi.xwpf.usermodel.XWPFTableRow;
     
    public class EcritureWORDTable 
    {
     
    	protected static String répertoire = "D:\\DONNEES\\DEF19\\000-Saisie-DEF19\\";
    	 protected String fichier = "";
     
    	public EcritureWORDTable ()
    	{
    		super();
    		créerTable();
    	}
     
     
    		private static  XWPFParagraph getTableParagraph(XWPFDocument document, XWPFTableCell  cell,  String text, boolean bold)
    		{
    		    XWPFParagraph paragraph = document.createParagraph(); 
    		     XWPFRun prun = paragraph.createRun();
    		     prun.setText(text);
    		      prun.setFontFamily("Times New Roman");
    		     prun.setFontSize(9);
    		      prun.setBold(bold);
     
    	           return paragraph;
    	   }
     
    	  private static void créerTable()
    	  {
    		     XWPFParagraph para =  null;
    		  try
    		 {
    			    XWPFDocument document = new XWPFDocument();
     
    			    FileOutputStream out = new FileOutputStream(new File(répertoire, "fichier Test.docx"));
     
    			    XWPFTable table = document.createTable();
                                CTTblLayoutType type = table.getCTTbl().getTblPr().addNewTblLayout();
    			    type.setType(STTblLayoutType.FIXED);
    			    table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(2000));
    			   table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(7000));
     
     
    			      WPFTableRow tableRow = table.getRow(0);
    			     para = getTableParagraph(document,  tableRow.getCell(0),  "Que j'aime a faire connaitre ce nombre utile aux sages, mon cher Archimède", false);
    			    tableRow .getCell(0).setParagraph(para);
    			    tableRow.addNewTableCell().setText( "Il est 14 heures" );
     
    			    tableRow  = table.createRow();
    			    tableRow.getCell(0).setText("col 1, row 2");
    			    tableRow.getCell(1).setText("col 2, row 2");
     
     
    			    document.write(out);
    			    document.close();
    			    out.close();
    			    System.out.println("Traitement terminé");
     
     
    	        } 
    		 catch (FileNotFoundException e) { e.printStackTrace(); } 
    		catch (IOException e) { e.printStackTrace(); } 
    		catch (Exception e)  { e.printStackTrace(); } 
              }
     
    }
     
    	 public static void main(String[] args) 
    	    {
    	    	  new EcritureWORDTable() ;
     
    	      } 
    }

  2. #2
    Membre habitué
    Profil pro
    Inscrit en
    Décembre 2009
    Messages
    265
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2009
    Messages : 265
    Points : 149
    Points
    149
    Par défaut
    re-bonjour,

    J'ai résolu mon problème. A la place de :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    XWPFParagraph paragraph = document.createParagraph();
    j'ai mis :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
       XWPFParagraph paragraph = cell.addParagraph();
        cell.removeParagraph(0);

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

Discussions similaires

  1. Réponses: 3
    Dernier message: 21/10/2016, 16h08
  2. [POI] Tableau dans l'entête word apache POI
    Par brucepr dans le forum Documents
    Réponses: 0
    Dernier message: 10/10/2016, 07h40
  3. Apache POI : Tableau dans word
    Par Palekor dans le forum API standards et tierces
    Réponses: 3
    Dernier message: 26/03/2014, 15h59
  4. tableau et fichier text
    Par tarekcom dans le forum Débuter
    Réponses: 25
    Dernier message: 05/05/2006, 21h30
  5. Réponses: 5
    Dernier message: 14/02/2006, 18h52

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