poi设置行距固定磅数

设置固定行距固定磅数


设置word文档行距为固定值

  public void setSingleLineSpacing(XWPFParagraph paragraph) {
        CTP ctp = paragraph.getCTP();
        CTPPr ppr = ctp.isSetPPr() ? ctp.getPPr() : ctp.addNewPPr();
        CTSpacing spacing = ppr.isSetSpacing()? ppr.getSpacing() : ppr.addNewSpacing();
        spacing.setAfter(BigInteger.valueOf(0));
        spacing.setBefore(BigInteger.valueOf(0));
        //注意设置行距类型为 EXACT
        spacing.setLineRule(STLineSpacingRule.EXACT);
        //1磅数是20
        spacing.setLine(BigInteger.valueOf(480));


    }

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在使用POI操作Excel时,可以通过设置CellStyle的属性来设置行间距。 具体操作步骤如下: 1. 获取Workbook对象。 2. 创建CellStyle对象。 3. 设置CellStyle的行高和字体属性,其中行高的单位是1/20个点。 ```java // 创建CellStyle对象 CellStyle cellStyle = workbook.createCellStyle(); // 设置行高 cellStyle.setRowHeight((short) (20 * 20)); // 创建字体对象 Font font = workbook.createFont(); // 设置字体大小 font.setFontHeightInPoints((short) 12); // 设置字体 font.setFontName("宋体"); // 设置字体样式 font.setBoldweight(Font.BOLDWEIGHT_NORMAL); // 设置字体颜色 font.setColor(IndexedColors.BLACK.getIndex()); // 设置行间距 font.setLineSpacing((short) 300); // 将字体应用到CellStyle cellStyle.setFont(font); ``` 4. 将CellStyle应用到需要设置行间距的行。 ```java // 获取Sheet对象 Sheet sheet = workbook.getSheetAt(0); // 获取第一行 Row row = sheet.getRow(0); // 将CellStyle应用到第一行 row.setRowStyle(cellStyle); ``` 完整示例代码如下: ```java import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class PoiExcelSetRowSpacingDemo { public static void main(String[] args) throws IOException { // 创建Workbook对象 Workbook workbook = new HSSFWorkbook(); // 创建Sheet对象 Sheet sheet = workbook.createSheet("Sheet1"); // 创建Row对象 Row row = sheet.createRow(0); // 创建Cell对象 row.createCell(0).setCellValue("Hello World!"); // 创建CellStyle对象 CellStyle cellStyle = workbook.createCellStyle(); // 设置行高 cellStyle.setRowHeight((short) (20 * 20)); // 创建字体对象 Font font = workbook.createFont(); // 设置字体大小 font.setFontHeightInPoints((short) 12); // 设置字体 font.setFontName("宋体"); // 设置字体样式 font.setBoldweight(Font.BOLDWEIGHT_NORMAL); // 设置字体颜色 font.setColor(IndexedColors.BLACK.getIndex()); // 设置行间距 font.setLineSpacing((short) 300); // 将字体应用到CellStyle cellStyle.setFont(font); // 将CellStyle应用到第一行 row.setRowStyle(cellStyle); // 保存Excel文件 FileOutputStream fos = new FileOutputStream("test.xls"); workbook.write(fos); fos.close(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值