2013-03-01 13 views
6

Sposób automatycznego dopasowania tabeli do szerokości strony dokumentu, gdy wzrasta rozmiar kolumny, użycie apache-poi i wyrównanie tej tabeli do środka.Jak automatycznie dopasować tabelę i wyrównanie stołu do środka, zgodnie z dokumentem Rozmiar dokumentu Apache-POI?

Ten kod generuje słowo Dokument wyodrębniający dane z Java do pliku word znajdującego się na dysku c. Ustawiłem ręcznie szerokość, ale teraz działa dobrze. Jeśli byłoby cenne dla mnie, jeśli właściwe wytyczne podane

public Word2Doc() throws Exception { 

    System.out.println("This is Word To Document Class"); 

    File file = null; 
      FileOutputStream fos = null; 
      XWPFDocument document = null; 
      XWPFParagraph para = null; 
      XWPFRun run = null; 
      try { 
       // Create the first paragraph and set it's text. 
       document = new XWPFDocument(); 
       para = document.createParagraph(); 

       para.setAlignment(ParagraphAlignment.CENTER); 

       para.setSpacingAfter(100); 

       para.setSpacingAfterLines(10); 
       run = para.createRun(); 
       for(int i=1; i<=5; i++) 
       run.setText("Test Name \009\009\009 Value \t\t\t\t Normal Ranges\013\013"); 
       run.addBreak(); // similar to new line 
       run.addBreak(); 

       XWPFTable table = document.createTable(4, 3); 

       table.setRowBandSize(1); 
       table.setWidth(1); 
       table.setColBandSize(1); 
       table.setCellMargins(1, 1, 100, 30); 

       table.setStyleID("finest"); 


       table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE"); 
       table.getRow(2).getCell(1).setText("fine"); 
       XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0); 
       p1.setAlignment(ParagraphAlignment.CENTER); 
         XWPFRun r1 = p1.createRun(); 
         r1.setBold(true); 
         r1.setText("Test Name"); 
         r1.setItalic(true); 
         r1.setFontFamily("Courier"); 
         r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH); 
         r1.setTextPosition(100); 

       //Locating the cell values 
         table.getRow(0).getCell(1).setText("Value"); 
         table.getRow(0).getCell(2).setText("Normal Ranges"); 

         table.getRow(2).getCell(2).setText("numeric values"); 

         table.setWidth(120); 

       file = new File("c:\\nwhpe.docx"); 
       if(file.exists()) 
        file.delete(); 


       FileOutputStream out = new FileOutputStream(file); 
       document.write(out); 
       out.close(); 
      } 

     } 
public static void main(String ar[]) throws Exception{ 
    new Word2Doc(); 
} 

}

Odpowiedz

4

Aby ustawić szerokość tabeli:

XWPFTable table = doc.createTable(nRows, nCols); 
    table.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(10000)); 

Aby ustawić szerokość kolumny:

cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000)); 

Jeśli dane wzrosną w komórce, wpłynie to na szerokość kolumny, tj. Szerokość kolumny również wzrośnie, aby uniknąć tego rodzaju proble ms musimy sprawdzić długość ciągu znaków w komórce i musimy dodać \ n (nowa linia) w łańcuchu, dzięki czemu możemy ustawić szerokość kolumny.

W moim projekcie miałem dużo problemów z ustawieniem szerokości kolumny i rozwiązałem ten problem, kontrolując dane w komórce.

+0

Jakie są jednostki? (tj. co oznacza 2000 i 10000?) – golimar

11

, aby ustawić automatyczne dopasowanie stołu, w zasadzie chodzi tylko o rozszerzenie szerokości do określonego rozmiaru. przykład

CTTbl table  = poiTable.getCTTbl(); 
CTTblPr pr   = table.getTblPr(); 
CTTblWidth tblW = pr.getTblW(); 
tblW.setW(BigInteger.valueOf(5000)); 
tblW.setType(STTblWidth.PCT); 
pr.setTblW(tblW); 
table.setTblPr(pr); 

jak do ustawiania stołu do centrum

CTJc jc = pr.addNewJc();   
jc.setVal(STJc.RIGHT); 
pr.setJc(jc); 
Powiązane problemy