easypoi导出Word中,表格分页时上边框丢失

问题具体描述: 使用easypoi导出的Word中包含一个表格,表格行数较多,需要分页显示,在分页后第一行的上边框部分丢失,显示不美观,具体如下:
在这里插入图片描述

解决:
对表格中每行都添加一个上边框,具体如下:

        XWPFDocument document = WordExportUtil.exportWord07(templateFile.getAbsolutePath(),map);
		//以下为添加单元格上线框操作,应该可以再优化,因为此处是对每个单元格添加
        List<XWPFTable> tables = document.getTables();
        for (XWPFTable table : tables) {
   
            int numberOfRows = table.getNumberOfRows();
            for (int i = 0; i < numberOfRows; i++) {
   
                XWPFTableRow row = table.getRow(i);
                List<XWPFTableCell> tableCells = row.getTableCells();
                CTTcPr tcPr ;
                CTTcBorders ctTcBorders;
                CTBorder ctBorder;
                for (XWPFTableCell tableCell : tableCells) {
   
                    tcPr = tableCell.getCTTc().getTcPr();
                    if(tcPr==null)tcPr=tableCell.getCTTc().addNewTcPr();
                    ctTcBorders = tcPr.addNewTcBorders();
                    ctBorder = ctTcBorders.addNewTop();
                    ctBorder.setVal(STBorder.SINGLE);
                }
            }
        }

知识点:

  1. 创建表格时,为每个单元格设置上边框。这样可以确保每个单元格都有上边框线。
XWPFTable table = document.createTable(rows, cols);
for (int row = 0; row < rows; row++) {
          
	for (int col = 0; col < cols; col++) {
           
		XWPFTableCell cell = table.getRow(row).getCell(col);        
		CTTcPr cellProperties = cell.getCTTc().getTcPr();        
		if (cellProperties == null) {
               
			cellProperties =cell.getCTTc().addNewTcPr(); 
		}        
		CTTcBorders borders = cellProperties.addNewTcBorders();        
		CTBorder topBorder = borders.addNewTop();   
		topBorder.setVal(STBorder.SINGLE);    
	}
}

上述代码使用XWPFTable创建表格,并为每个单元格设置上边框线。
2. 当表格分页时,检查当前页的最后一行是否位于表格的最后一行。如果是,则在表格的下方添加一个空行,并为该行的每个单元格设置上边框

XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.addBreak(BreakType.PAGE);
if (isLastRowInTable(pageLastRow, table)) {
       
	XWPFTableRow emptyRow = table.createRow();    
	for (int col = 0; col < cols; col++) {
           
		XWPFTableCell cell = emptyRow.getCell(col);        
		CTTcPr cellProperties = cell.getCTTc().getTcPr();        
		if (cellProperties == null) {
               
			cellProperties = cell.getCTTc().addNewTcPr();        
		}        
		CTTcBorders borders = cellProperties.addNewTcBorders();        
		CTBorder topBorder = borders.addNewTop();        
		topBorder.setVal(STBorder.SINGLE);    
	}
}

上述代码使用XWPFParagraphXWPFRun创建一个新段落,并在段落中添加分页符(addBreak(BreakType.PAGE))。然后,检查当前页的最后一行是否是表格的最后一行,如果是,则在表格下方添加一个空行,并为该行的每个单元格设置上边框线。通过这两个步骤,您可以确保在分页时不丢失上边框。请根据您的具体需求和代码结构进行相应的调整和集成。

相关推荐

  1. EasyPOI导出动态表头

    2024-01-27 07:46:03       37 阅读
  2. Easypoi word 模板导出问题

    2024-01-27 07:46:03       57 阅读
  3. element_Plus表格的使用

    2024-01-27 07:46:03       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-27 07:46:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-27 07:46:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-27 07:46:03       87 阅读
  4. Python语言-面向对象

    2024-01-27 07:46:03       96 阅读

热门阅读

  1. 鸿蒙架构&Android架构分析

    2024-01-27 07:46:03       54 阅读
  2. 第二百八十九回

    2024-01-27 07:46:03       65 阅读
  3. mysql小知识

    2024-01-27 07:46:03       49 阅读
  4. web前端项目重构的理解

    2024-01-27 07:46:03       52 阅读
  5. ADB的配置和使用 ADB常用命-2

    2024-01-27 07:46:03       48 阅读
  6. 数据库(二)

    2024-01-27 07:46:03       49 阅读
  7. Ceph部署

    2024-01-27 07:46:03       41 阅读