8

apache-poi を使用し、そのテーブルを中央に揃えて、列のサイズが大きくなったときに、このテーブルをドキュメントのページ幅に自動調整する方法。

このコードは、Java から C ドライブにある Word ファイルにデータを抽出する Word Document を生成します。手動で幅を設定しましたが、現在は正常に機能しています。適切なガイダンスが提供されれば、私にとって価値があると思います

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

}

4

4 に答える 4

4

表の幅を設定するには:

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

列幅を設定するには:

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

セルのデータが増加すると、列の幅に影響します。つまり、列の幅も増加するため、この種の問題を回避するには、セルの文字列値の長さを確認する必要があり、これを実行して文字列に \n (改行) を追加する必要があります。列の幅を設定できます。

私のプロジェクトでは、列幅を設定するのに苦労しており、セル内のデータを制御することでこの問題を解決しました。

于 2014-05-02T05:44:01.143 に答える