Как я могу установить высоту строки таблицы, различную для каждого столбца в слове, используя apache poi - PullRequest
0 голосов
/ 29 апреля 2020

Я хочу создать таблицу в моем текстовом документе. В этой таблице мне нужно что-то вроде этого

sample

На этом изображении третий столбец третьей строки имеет высоту, отличную от первого и второго столбцов. Я хочу создать что-то подобное с apache poi, но я не могу найти никакого решения для этого.

Мои коды здесь

        table.getRow(6).setHeight(630);
        table.getRow(6).getCtRow().getTrPr().getTrHeightArray(0).setHRule(STHeightRule.EXACT);
        table.getRow(6).getCell(0).removeParagraph(0);  //Cell0
        XWPFParagraph row6Paragraph=table.getRow(6).getCell(0).addParagraph();
        row6Paragraph.setAlignment(ParagraphAlignment.LEFT);
        row6Paragraph.setVerticalAlignment(TextAlignment.CENTER);
        XWPFRun row6Run=row6Paragraph.createRun();
        row6Run.setFontFamily("Arial");
        row6Run.setFontSize(10);
        row6Run.setText("5) Engine Induction Date");
        row6Run.addBreak();
        String indDate="12-09-2018";
        row6Run.setText("  "+indDate);
        table.getRow(6).getCell(0).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(4985));
        table.getRow(6).getCell(1).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(365));//cell1
        table.getRow(6).getCell(2).removeParagraph(0);  //Cell2
        row6Paragraph=table.getRow(6).getCell(2).addParagraph();
        row6Paragraph.setAlignment(ParagraphAlignment.LEFT);
        row6Paragraph.setVerticalAlignment(TextAlignment.CENTER);
        XWPFRun row6Run1=row6Paragraph.createRun();
        row6Run1.setFontFamily("Arial");
        row6Run1.setFontSize(10);
        row6Run1.setBold(true);
        row6Run1.setText("PART INFORMATION");
        table.getRow(6).getCell(2).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(4985)); 

1 Ответ

1 голос
/ 29 апреля 2020

Вы должны включить отображение непечатаемых символов в Word (¶). Без этих персонажей это не очень ясно из вашего снимка экрана. Но создать это, используя только одну таблицу путем слияния ячеек, было бы действительно сложно, не только используя apache poi, но и используя Word 'GUI. Поэтому я подозреваю, что это три таблицы.

Таблица 1 находится в секции с одним столбцом и содержит только одну строку с одной ячейкой, содержащей заголовок.

Затем таблица 2 помещается в секцию с двумя столбцами слова документа. Затем таблица разбивается на второй столбец, если это необходимо. В нем только один столбец, и каждая строка имеет такую ​​же высоту, как и содержимое.

Таблица три снова находится в одном разделе столбца и содержит только одну строку с одной ячейкой, содержащей нижний колонтитул.

Пример использования текущий apache poi 4.1.2:

import java.io.FileOutputStream;
import java.math.BigInteger;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

public class Word2ColumnPageWithTable {
 public static void main(String[] args) throws Exception {

  XWPFDocument document= new XWPFDocument();

  //one column section
  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run = paragraph.createRun();  
  run.setText("One column on top.");

  //table over full width of one column section
  XWPFTable table = document.createTable();
  table.setWidth("100%");
  XWPFTableRow row = table.getRow(0);
  row.getCell(0).setColor("808080");
  row.getCell(0).getParagraphArray(0).setAlignment(ParagraphAlignment.CENTER);
  row.getCell(0).getParagraphArray(0).setSpacingAfter(0);
  row.getCell(0).getParagraphArray(0).createRun().setText("Table Header");
  row.getCell(0).getParagraphArray(0).getRuns().get(0).setBold(true);
  row.getCell(0).getParagraphArray(0).getRuns().get(0).setColor("FFFFFF");

  //paragraph with section setting for one column section above
  paragraph = document.createParagraph();
  CTSectPr ctSectPr = paragraph.getCTP().addNewPPr().addNewSectPr();
  CTColumns ctColumns = ctSectPr.addNewCols();
  ctColumns.setNum(BigInteger.valueOf(1));

  //two column section
  //table over full width of one column in two column section
  table = document.createTable();
  table.setWidth("100%");
  row = table.getRow(0);
  row.getCell(0).getParagraphArray(0).setSpacingAfter(0);
  row.getCell(0).getParagraphArray(0).createRun().setText("SHORT CONTENT");
  row.getCell(0).getParagraphArray(0).getRuns().get(0).setBold(true);
  table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
  table.createRow().getCell(0).setText("Long content, which increases the row height.");
  table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
  table.createRow().getCell(0).setText("Long content, which increases the row height.");
  row = table.createRow();
  row.getCell(0).getParagraphArray(0).setSpacingAfter(0);
  row.getCell(0).getParagraphArray(0).createRun().setText("SHORT CONTENT");
  row.getCell(0).getParagraphArray(0).getRuns().get(0).setBold(true);
  table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
  table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
  row = table.createRow();
  row.getCell(0).getParagraphArray(0).setSpacingAfter(0);
  row.getCell(0).getParagraphArray(0).createRun().setText("SHORT CONTENT");
  row.getCell(0).getParagraphArray(0).getRuns().get(0).setBold(true);
  table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
  row = table.createRow();
  row.getCell(0).getParagraphArray(0).setSpacingAfter(0);
  row.getCell(0).getParagraphArray(0).createRun().setText("SHORT CONTENT");
  row.getCell(0).getParagraphArray(0).getRuns().get(0).setBold(true);
  table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
  table.createRow().getCell(0).setText("Long content, which increases the row height.");
  table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
  table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
  table.createRow().getCell(0).setText("Long content, which increases the row height.");

  //paragraph with section break continuous for two column section above
  paragraph = document.createParagraph();
  ctSectPr = paragraph.getCTP().addNewPPr().addNewSectPr();
  ctSectPr.addNewType().setVal(STSectionMark.CONTINUOUS);
  ctColumns = ctSectPr.addNewCols();
  ctColumns.setNum(BigInteger.valueOf(2));
  ctColumns.setEqualWidth(STOnOff.ON);

  //one column section again
  //table over full width of one column section
  table = document.createTable();
  table.setWidth("100%");
  row = table.getRow(0);
  row.getCell(0).getParagraphArray(0).setSpacingAfter(0);
  row.getCell(0).getParagraphArray(0).createRun().setText("TABLE FOOTER");

  paragraph = document.createParagraph();
  paragraph = document.createParagraph();
  run=paragraph.createRun();  
  run.setText("One column on bottom.");

  //section setting continuous for one column section above and whole document from here on
  CTDocument1 ctDocument = document.getDocument();
  CTBody ctBody = ctDocument.getBody();
  ctSectPr = ctBody.addNewSectPr();
  ctSectPr.addNewType().setVal(STSectionMark.CONTINUOUS);
  ctColumns = ctSectPr.addNewCols();
  ctColumns.setNum(BigInteger.valueOf(1));

  FileOutputStream out = new FileOutputStream("Word2ColumnPageWithTable.docx");  
  document.write(out);
  out.close();
  document.close();

 }
}

Этот код выдает Word2ColumnPageWithTable.docx, который выглядит следующим образом:

enter image description here

...