手册表格列宽修正

This commit is contained in:
zhal 2023-09-06 10:31:34 +08:00
parent 2a9f325f74
commit 1a8848a390
2 changed files with 31 additions and 25 deletions

View File

@ -437,16 +437,14 @@ public class OutputWordUtil {
//添加表格 //添加表格
Table table = section.addTable(true); Table table = section.addTable(true);
table.resetCells(data.length + 1, header.length); table.resetCells(data.length + 1, header.length);
table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);
TableRow row = table.getRows().get(0); TableRow row = table.getRows().get(0);
row.isHeader(true); row.isHeader(true);
row.setHeightType(TableRowHeightType.Auto); row.setHeightType(TableRowHeightType.Exactly);
for (int i = 0; i < header.length; i++) { for (int i = 0; i < header.length; i++) {
row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle); row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
//设置固定列宽 //设置固定列宽
if(i==0){ /*if(i==0){
row.getCells().get(0).setWidth(40); row.getCells().get(0).setWidth(40);
}else if(i==1){ }else if(i==1){
row.getCells().get(1).setWidth(60); row.getCells().get(1).setWidth(60);
@ -462,7 +460,7 @@ public class OutputWordUtil {
row.getCells().get(6).setWidth(50); row.getCells().get(6).setWidth(50);
}else if(i==7){ }else if(i==7){
row.getCells().get(7).setWidth(80); row.getCells().get(7).setWidth(80);
} }*/
Paragraph p = row.getCells().get(i).addParagraph(); Paragraph p = row.getCells().get(i).addParagraph();
p.getFormat().setHorizontalAlignment(Center); p.getFormat().setHorizontalAlignment(Center);
@ -482,13 +480,13 @@ public class OutputWordUtil {
TableRow dataRow = table.getRows().get(r + 1); TableRow dataRow = table.getRows().get(r + 1);
dataRow.setHeight(25); dataRow.setHeight(25);
dataRow.setHeightType(TableRowHeightType.Auto); dataRow.setHeightType(TableRowHeightType.Exactly);
dataRow.getRowFormat().setBackColor(Color.white); dataRow.getRowFormat().setBackColor(Color.white);
for (int c = 0; c < data[r].length; c++) { for (int c = 0; c < data[r].length; c++) {
dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle); dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
//设置固定列宽 //设置固定列宽
if(c==0){ /* if(c==0){
dataRow.getCells().get(0).setWidth(40); dataRow.getCells().get(0).setWidth(40);
}else if(c==1){ }else if(c==1){
dataRow.getCells().get(1).setWidth(60); dataRow.getCells().get(1).setWidth(60);
@ -504,7 +502,7 @@ public class OutputWordUtil {
dataRow.getCells().get(6).setWidth(50); dataRow.getCells().get(6).setWidth(50);
}else if(c==7){ }else if(c==7){
dataRow.getCells().get(7).setWidth(80); dataRow.getCells().get(7).setWidth(80);
} }*/
//dataRow.getCells().get(c).addParagraph().appendText(data[r][c]); //dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
@ -736,34 +734,39 @@ public class OutputWordUtil {
//添加表格 //添加表格
Table table = section.addTable(true); Table table = section.addTable(true);
table.resetCells(data.length + 1, header.length); table.resetCells(data.length + 1, header.length);
table.getTableFormat().isAutoResized(true);
table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);
TableRow row = table.getRows().get(0); TableRow row = table.getRows().get(0);
row.isHeader(true); row.isHeader(true);
row.setHeight(20); row.setHeight(20);
row.setHeightType(TableRowHeightType.Auto); row.setHeightType(TableRowHeightType.Exactly);
for (int i = 0; i < header.length; i++) { for (int i = 0; i < header.length; i++) {
row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle); row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
//设置固定列宽 //设置固定列宽
row.getCells().get(0).setWidth(150); //row.getCells().get(0).setWidth(50);
row.getCells().get(1).setWidth(500); //row.getCells().get(1).setWidth(150);
Paragraph p = row.getCells().get(i).addParagraph(); Paragraph p = row.getCells().get(i).addParagraph();
p.getFormat().setHorizontalAlignment(Center); p.getFormat().setHorizontalAlignment(Center);
TextRange txtRange = p.appendText(header[i]); TextRange txtRange = p.appendText(header[i]);
txtRange.getCharacterFormat().setBold(true); txtRange.getCharacterFormat().setBold(true);
} }
//将数据添加到其余行 //将数据添加到其余行
for (int r = 0; r < data.length; r++) { for (int r = 0; r < data.length; r++) {
TableRow dataRow = table.getRows().get(r + 1); TableRow dataRow = table.getRows().get(r + 1);
dataRow.setHeight(25); dataRow.setHeight(25);
dataRow.setHeightType(TableRowHeightType.Auto); dataRow.setHeightType(TableRowHeightType.Exactly);
dataRow.getRowFormat().setBackColor(Color.white); dataRow.getRowFormat().setBackColor(Color.white);
for (int c = 0; c < data[r].length; c++) { for (int c = 0; c < data[r].length; c++) {
dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle); dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
//设置固定列宽 //设置固定列宽
dataRow.getCells().get(0).setWidth(150); //dataRow.getCells().get(0).setWidth(50);
dataRow.getCells().get(1).setWidth(500); //dataRow.getCells().get(1).setWidth(150);
//dataRow.getCells().get(c).addParagraph().appendText(data[r][c]); //dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
TextRange text =dataRow.getCells().get(c).addParagraph().appendText(data[r][c]); TextRange text =dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
@ -772,6 +775,8 @@ public class OutputWordUtil {
} }
} }
Paragraph paragraph_blank = section.addParagraph(); Paragraph paragraph_blank = section.addParagraph();
} }
@ -852,12 +857,12 @@ public class OutputWordUtil {
TableRow row = table.getRows().get(0); TableRow row = table.getRows().get(0);
row.isHeader(true); row.isHeader(true);
row.setHeight(20); row.setHeight(20);
row.setHeightType(TableRowHeightType.Auto); row.setHeightType(TableRowHeightType.Exactly);
for (int i = 0; i < header.length; i++) { for (int i = 0; i < header.length; i++) {
row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle); row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
//设置固定列宽 //设置固定列宽
row.getCells().get(0).setWidth(150); //row.getCells().get(0).setWidth(150);
row.getCells().get(1).setWidth(500); //row.getCells().get(1).setWidth(500);
Paragraph p = row.getCells().get(i).addParagraph(); Paragraph p = row.getCells().get(i).addParagraph();
p.getFormat().setHorizontalAlignment(Center); p.getFormat().setHorizontalAlignment(Center);
TextRange txtRange = p.appendText(header[i]); TextRange txtRange = p.appendText(header[i]);
@ -869,13 +874,13 @@ public class OutputWordUtil {
for (int r = 0; r < data.length; r++) { for (int r = 0; r < data.length; r++) {
TableRow dataRow = table.getRows().get(r + 1); TableRow dataRow = table.getRows().get(r + 1);
dataRow.setHeight(25); dataRow.setHeight(25);
dataRow.setHeightType(TableRowHeightType.Auto); dataRow.setHeightType(TableRowHeightType.Exactly);
dataRow.getRowFormat().setBackColor(Color.white); dataRow.getRowFormat().setBackColor(Color.white);
for (int c = 0; c < data[r].length; c++) { for (int c = 0; c < data[r].length; c++) {
dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle); dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
//设置固定列宽 //设置固定列宽
dataRow.getCells().get(0).setWidth(150); //dataRow.getCells().get(0).setWidth(150);
dataRow.getCells().get(1).setWidth(500); //dataRow.getCells().get(1).setWidth(500);
//dataRow.getCells().get(c).addParagraph().appendText(data[r][c]); //dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
TextRange text =dataRow.getCells().get(c).addParagraph().appendText(data[r][c]); TextRange text =dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
text.getCharacterFormat().setFontName("宋体"); text.getCharacterFormat().setFontName("宋体");
@ -938,6 +943,7 @@ public class OutputWordUtil {
Table table = section.addTable(true); Table table = section.addTable(true);
table.resetCells(data.length + 1, header.length); table.resetCells(data.length + 1, header.length);
table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);
TableRow row = table.getRows().get(0); TableRow row = table.getRows().get(0);
row.isHeader(true); row.isHeader(true);
@ -946,8 +952,8 @@ public class OutputWordUtil {
for (int i = 0; i < header.length; i++) { for (int i = 0; i < header.length; i++) {
row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle); row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
//设置固定列宽 //设置固定列宽
row.getCells().get(0).setWidth(150); //row.getCells().get(0).setWidth(150);
row.getCells().get(1).setWidth(500); //row.getCells().get(1).setWidth(500);
Paragraph p = row.getCells().get(i).addParagraph(); Paragraph p = row.getCells().get(i).addParagraph();
p.getFormat().setHorizontalAlignment(Center); p.getFormat().setHorizontalAlignment(Center);
TextRange txtRange = p.appendText(header[i]); TextRange txtRange = p.appendText(header[i]);
@ -963,8 +969,8 @@ public class OutputWordUtil {
for (int c = 0; c < data[r].length; c++) { for (int c = 0; c < data[r].length; c++) {
dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle); dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
//设置固定列宽 //设置固定列宽
dataRow.getCells().get(0).setWidth(150); //dataRow.getCells().get(0).setWidth(150);
dataRow.getCells().get(1).setWidth(500); //dataRow.getCells().get(1).setWidth(500);
//dataRow.getCells().get(c).addParagraph().appendText(data[r][c]); //dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
TextRange text =dataRow.getCells().get(c).addParagraph().appendText(data[r][c]); TextRange text =dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
text.getCharacterFormat().setFontName("宋体"); text.getCharacterFormat().setFontName("宋体");