我正在使用POI创建一个表格,表格已成功创建,但我想在单元格中的每个文本之间做一个新行,我无法做到这一点,这是我到目前为止所做的,< / p>
XWPFTable candidateTrackerTable = document.createTable();
XWPFTableRow canndidateTrackerTableRowOne = candidateTrackerTable.getRow(0);
canndidateTrackerTableRowOne.getCell(0).setText("Presented ");
canndidateTrackerTableRowOne.addNewTableCell().setText("Interview ");
canndidateTrackerTableRowOne.addNewTableCell().setText("Phone ");
canndidateTrackerTableRowOne.addNewTableCell().setText("Final Interview ");
canndidateTrackerTableRowOne.addNewTableCell().setText("Offer ");
XWPFTableRow canndidateTrackerTableRowTwo = null;
for(Map.Entry<Integer, ArrayList<String>> candidateName:aMap.entrySet()){
canndidateTrackerTableRowTwo = candidateTrackerTable.createRow();
for(String s:candidateName.getValue()){
//System.out.println("------>>"+s);
canndidateTrackerTableRowTwo.getCell(0).setText(String.valueOf(s)+"\n");
}
for(Map.Entry<Integer, ArrayList<String>> candidatePhone:bMap.entrySet()){
for(String s1:candidatePhone.getValue()){
//System.out.println("------>>"+s1);
canndidateTrackerTableRowTwo.getCell(1).setText(String.valueOf(s1));
}
for(Map.Entry<Integer, ArrayList<String>> candidateSkyped: cMap.entrySet())
for(String s2:candidateSkyped.getValue())
canndidateTrackerTableRowTwo.getCell(2).setText(String.valueOf(s2));
for(Map.Entry<Integer, ArrayList<String>> candidateOnsite : dMap.entrySet())
for(String s3:candidateOnsite.getValue())
canndidateTrackerTableRowTwo.getCell(3).setText(String.valueOf(s3));
for(Map.Entry<Integer, ArrayList<String>> candidateOffered : eMap.entrySet())
for(String s4:candidateOffered.getValue())
canndidateTrackerTableRowTwo.getCell(4).setText(String.valueOf(s4));
}
}
我想在每个canndidateTrackerTableRowTwo.getCell(0).setText(String.valueOf(s)+“\ n”)之间添加新行;
答案 0 :(得分:3)
这段代码对我有一些修改(循环有一些错误,找到最后一次运行并忘记在其他段落上插入新行)(编辑:*和dellasavia说:“while”比一个更好“为每个人”以避免例外)。
// Loop through runs in paragraph.
int i_paragraph = 0;
//for (XWPFRun xwpfRun : parrafo.getRuns()) { //EXCEPTION UPDATING RUNS IN THE PARAGRAPH
while (i_paragraph < parrafo.getRuns().size()) { //BETTER THE WHILE
XWPFRun xwpfRun = parrafo.getRuns().get(i_paragraph);
// Split runs by new line character.
String textOfRun = xwpfRun.getText(0);
if (textOfRun.contains("\n")) {
String[] stringsOnNewLines = textOfRun.split("\n");
// For each additional line, create a new run. Add carriage return on previous.
for (int i = 0; i < stringsOnNewLines.length; i++) {
// For every run except last one, add a carriage return.
String textForLine = stringsOnNewLines[i];
if (i == stringsOnNewLines.length - 1) {
xwpfRun.setText(textForLine, 0);
xwpfRun.addCarriageReturn();
} else {
parrafo.insertNewRun(i);
XWPFRun newRun = parrafo.getRuns().get(i);
CTRPr rPr = newRun.getCTR().isSetRPr() ? newRun.getCTR().getRPr() : newRun.getCTR().addNewRPr();
rPr.set(xwpfRun.getCTR().getRPr());
newRun.setText(textForLine);
newRun.addCarriageReturn(); //ADD THE NEW LINE
}
}
}
i_paragraph++;
}
答案 1 :(得分:0)
Apache POI / OpenXML格式无法识别传统的回车和换行符。相反,需要调用XWPFRun.addCarriageReturn()方法来创建新行。
您将需要遍历单元格段落并运行,然后将要在新行上显示的文本拆分为新运行,然后在之前的所有运行中添加回车符。
这里有一些代码可以帮助您入门。我没有测试过这个,但我应该让你去。
// Loop through paragraphs in cell.
for ( XWPFParagraph xwpfParagraph : cell.getParagraphs() ) {
// Loop through runs in paragraph.
for ( XWPFRun xwpfRun : xwpfParagraph.getRuns() ) {
// Split runs by new line character.
String textOfRun = xwpfRun.getText( 0 );
if ( textOfRun.contains( "\n" ) ) {
String[] stringsOnNewLines = textOfRun.split( "\n" );
// For each additional line, create a new run. Add carriage return on previous.
for ( int i = 0; i < stringsOnNewLines.length; i++ ) {
// For every run except last one, add a carriage return.
String textForLine = stringsOnNewLines[i];
if ( i < stringsOnNewLines.length - 1 ) {
xwpfRun.setText(textForLine, 0);
xwpfRun.addCarriageReturn();
}
else {
xwpfParagraph.insertNewRun( i );
XWPFRun newRun = paragraph.getRuns().get( i );
CTRPr rPr = newRun.getCTR().isSetRPr() ? newRun.getCTR().getRPr() : newRun.getCTR().addNewRPr();
rPr.set( xwpfRun.getCTR().getRPr() );
newRun.setText( textForLine );
}
}
}
}
}
答案 2 :(得分:0)
要向表格单元格添加格式化,请创建一个以段落和字符串作为参数的函数。此函数将从para创建一个运行,现在您可以将此格式添加到此运行中。
private static void tableText(List<XWPFParagraph> paragraph, String text) {
XWPFRun run = paragraph.get(0).createRun();
run.setFontSize(14);
run.setFontFamily("Times New Roman");
run.setText(text);
run.addBreak();
run.setText("Next line");
}
现在使用此功能将文本添加到表格中。
XWPFTableRow tableRow = table.createRow();
tableText(tableRow.getCell(0).getParagraphs(), " 6 ", true);
答案 3 :(得分:0)
POI网站列出了一项适合我的技术。
https://poi.apache.org/spreadsheet/quick-guide.html#Using+newlines+in+cells
在单元格中使用换行符
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(2);
Cell cell = row.createCell(2);
cell.setCellValue("Use \n with word wrap on to create a new line");
//to enable newlines you need set a cell styles with wrap=true
CellStyle cs = wb.createCellStyle();
cs.setWrapText(true);
cell.setCellStyle(cs);
//increase row height to accomodate two lines of text
row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));
//adjust column width to fit the content
sheet.autoSizeColumn((short)2);
FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx");
wb.write(fileOut);
fileOut.close();
答案 4 :(得分:0)
可能这有点晚了。我有一种方法,可以在其中创建表的标题(行= 0),并且每个标题都有标题编号,然后添加带有参数文本的另一段
public static final String[] headings= {"Title", "Name", "Surname", "Address", "DOB","Notes"};
'''
XWPFTableRow row = table.getRow(0);
for (int pos=0; pos < COLUMN_NUMBER; pos++) {
addCellHeading(row, pos, headings[pos]);
}
...
private void addCellHeading(XWPFTableRow row, Integer pos, String text){
XWPFTableCell cell = row.getCell(pos);
XWPFParagraph headingNo = cell.addParagraph();
headingNo.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run = headingNo.createRun();
run.setBold(true);
run.setText(String.valueOf(pos+1) + ". ");
XWPFParagraph heading = cell.addParagraph();
heading.setAlignment(ParagraphAlignment.CENTER);
run = heading.createRun();
run.setBold(true);
run.setText(text);
}