如何在 Apache POI 中格式化 XWPFTable 中的单元格 [英] How to format cell in XWPFTable in Apache POI

查看:46
本文介绍了如何在 Apache POI 中格式化 XWPFTable 中的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够在 Apache POI 中创建一个表格,我的表格具有正确的值,但我想要的是减少表格中列大小的单元格大小如何做到这一点,请帮助..这是我所做的

I am able to create a table in Apache POI in word ,my table is coming with proper value but what i want i want to decrease the cell size of the column size in the table how to do that ,please help..Here is what i have done upto

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

        r3.setText("MSH Activity Score Card");
        r3.setBold(true);



        //creating first row 
        table.getRow(0).getCell(1).setText("Job ID#"); 
        table.getRow(0).getCell(2).setText("1362"); 

        //creating second row

        table.getRow(1).getCell(1).setText("Job Title#"); 
        table.getRow(1).getCell(2).setText("Global Network Architect Consultant");

        //creating third row
        table.getRow(2).getCell(1).setText("Client");
        table.getRow(2).getCell(2).setText("Carnival Corporation"); 

        //creating fourth row
        table.getRow(3).getCell(1).setText("Start Date");
        table.getRow(3).getCell(2).setText("11/13/2014");

       //creating fifth row
        table.getRow(4).getCell(1).setText("Days Old");
        table.getRow(4).getCell(2).setText("33");

        CTTblWidth width = table.getCTTbl().addNewTblPr().addNewTblW();

        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(1500));

我得到一个包含数据的表格,但我想最小化尺寸

I am getting a table with data but i want to minimize the size

但我想要这个尺寸

推荐答案

首先你需要确保你已经声明了库的导入:

First you need to make sure that you've declared the library's imports :

import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

然后就像 ,假设您要在 row-0 上格式化 cell-0 :

Then it's just like , assuming you want to format cell-0 on row-0:

table.getRow(0).getCell(0).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000));

或者在创建最后一行后放置此代码段以设置所有单元格的格式:

Or put this snippet after creating your last row to format all cells:

for(int x = 0;x < table.getNumberOfRows(); x++){
          XWPFTableRow row = table.getRow(x);
          int numberOfCell = row.getTableCells().size();
          for(int y = 0; y < numberOfCell ; y++){
              XWPFTableCell cell = row.getCell(y);

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

*但请记住,单元格的宽度总是会随着字符串的长度而增加.

*But remember that the width of the cell will always increase to follow the length of the string.

这篇关于如何在 Apache POI 中格式化 XWPFTable 中的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆