引入jar
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
业务代码
//创建表格
public void createTable(XWPFDocument doc,List<JczcJianchabaogao> jianchabaogaoList,int xiangmuId) throws IOException, InvalidFormatException {
ArrayList<MobanxiangqingBean> mobanxiangqingBeans = getMobanbianhao(xiangmuId);
for (int i = 0; i < mobanxiangqingBeans.size(); i++) {
for (int j = 0; j < jianchabaogaoList.size(); j++) {
if (jianchabaogaoList.get(j).getJcxmName().equals(mobanxiangqingBeans.get(i).getJcxmName())){
jianchabaogaoList.get(j).setXmBianhao(mobanxiangqingBeans.get(i).getJcxmBianhao());
}
if (jianchabaogaoList.get(j).getJcnrfjName().equals(mobanxiangqingBeans.get(i).getJcnrfjName())){
jianchabaogaoList.get(j).setNrfjBianhao(mobanxiangqingBeans.get(i).getJcnrfjBianhao());
}
if (jianchabaogaoList.get(j).getJcnrName().equals(mobanxiangqingBeans.get(i).getJcnrName())){
jianchabaogaoList.get(j).setNrBiaohao(mobanxiangqingBeans.get(i).getJcnrBianhao());
}
}
}
int pos=0;
int pos2=0;
int pos3=0;
ArrayList<Integer> integers = new ArrayList<>();
ArrayList<Integer> integers2 = new ArrayList<>();
ArrayList<Integer> integers3 = new ArrayList<>();
XWPFTable xwpfTable = doc.createTable(jianchabaogaoList.size()+1,9);
XWPFTableRow row0 = xwpfTable.getRow(0);
CTTcPr ctTcPr0 = row0.getCell(0).getCTTc().addNewTcPr();
ctTcPr0.addNewTcW().setW(BigInteger.valueOf(600));
row0.getCell(1).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(1200));
row0.getCell(2).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(1050+3000));
row0.getCell(4).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(3000+420));
row0.getCell(6).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2400));
row0.getCell(7).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2400));
row0.getCell(8).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(1800));
XWPFTableCell cell0 = row0.getCell(0);
XWPFRun xwpfRun = cell0.getParagraphs().get(0).createRun();
xwpfRun.setText("序号");
xwpfRun.setBold(true);
xwpfRun.setFontFamily("宋体");
xwpfRun.setFontSize(10);
XWPFTableCell cell1 = row0.getCell(1);
XWPFRun xwpfRun1 = cell1.getParagraphs().get(0).createRun();
xwpfRun1.setText("检查项目");
xwpfRun1.setBold(true);
xwpfRun1.setFontFamily("宋体");
xwpfRun1.setFontSize(10);
XWPFTableCell cell2 = row0.getCell(2);
XWPFRun xwpfRun2 = cell2.getParagraphs().get(0).createRun();
xwpfRun2.setText("检查内容");
xwpfRun2.setBold(true);
xwpfRun2.setFontFamily("宋体");
xwpfRun2.setFontSize(10);
row0.getCell(3).setText("");
XWPFTableCell cell4 = row0.getCell(4);
XWPFRun xwpfRun4 = cell4.getParagraphs().get(0).createRun();
xwpfRun4.setText("检查情况");
xwpfRun4.setBold(true);
xwpfRun4.setFontFamily("宋体");
xwpfRun4.setFontSize(10);
row0.getCell(5).setText("");
XWPFTableCell cell6 = row0.getCell(6);
XWPFRun xwpfRun6 = cell6.getParagraphs().get(0).createRun();
xwpfRun6.setText("检查依据");
xwpfRun6.setBold(true);
xwpfRun6.setFontFamily("宋体");
xwpfRun6.setFontSize(10);
XWPFTableCell cell7 = row0.getCell(7);
XWPFRun xwpfRun7 = cell7.getParagraphs().get(0).createRun();
xwpfRun7.setText("违法依据");
xwpfRun7.setBold(true);
xwpfRun7.setFontFamily("宋体");
xwpfRun7.setFontSize(10);
XWPFTableCell cell8 = row0.getCell(8);
XWPFRun xwpfRun8 = cell8.getParagraphs().get(0).createRun();
xwpfRun8.setText("整改要求");
xwpfRun8.setBold(true);
xwpfRun8.setFontFamily("宋体");
xwpfRun8.setFontSize(10);
for (int i = 0; i < jianchabaogaoList.size(); i++) {
if (i<jianchabaogaoList.size()-1){
if (jianchabaogaoList.get(i).getJcxmName().equals(jianchabaogaoList.get(i+1).getJcxmName())&&pos==0) {
integers.add(i);
pos++;
} if (!jianchabaogaoList.get(i).getJcxmName().equals(jianchabaogaoList.get(i+1).getJcxmName())&&pos!=0)
{
integers.add(i);
pos=0;
}
}else{
if (i-1>=0){
if (jianchabaogaoList.get(i).getJcxmName().equals(jianchabaogaoList.get(i-1).getJcxmName())) {
integers.add(i);
}
}
}
if (i<jianchabaogaoList.size()-1){
if (jianchabaogaoList.get(i).getJcnrName().equals(jianchabaogaoList.get(i+1).getJcnrName())&&pos2==0)
integers2.add(i);
pos2++;
} if (!jianchabaogaoList.get(i).getJcnrName().equals(jianchabaogaoList.get(i+1).getJcnrName())&&pos2!=0)
{
integers2.add(i);
pos2=0;
}
}else{
if (i-1>=0){
if (jianchabaogaoList.get(i).getJcnrName().equals(jianchabaogaoList.get(i-1).getJcnrName())) {
integers2.add(i);
}
}
}
if (i<jianchabaogaoList.size()-1){
if (jianchabaogaoList.get(i).getJcnrfjName().equals(jianchabaogaoList.get(i+1).getJcnrfjName())&&pos3==0) {
integers3.add(i);
pos3++;
} if (!jianchabaogaoList.get(i).getJcnrfjName().equals(jianchabaogaoList.get(i+1).getJcnrfjName())&&pos3!=0)
{
integers3.add(i);
pos3=0;
}
}else{
if (i-1>=0){
if (jianchabaogaoList.get(i).getJcnrfjName().equals(jianchabaogaoList.get(i-1).getJcnrfjName())) {
integers3.add(i);
}
}
}
XWPFTableRow row = xwpfTable.getRow(i+1);
row.getCell(0).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(600));
row.getCell(1).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(1200));
row.getCell(2).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(1050));
row.getCell(3).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(3000));
row.getCell(4).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(3000));
row.getCell(5).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(420));
row.getCell(6).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2400));
row.getCell(7).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2400));
row.getCell(8).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(1800));
XWPFTableCell ncell0 = row.getCell(0);
XWPFTableCell ncell1 = row.getCell(1);
XWPFTableCell ncell2 = row.getCell(2);
XWPFTableCell ncell3 = row.getCell(3);
XWPFTableCell ncell4 = row.getCell(4);
XWPFTableCell ncell5 = row.getCell(5);
XWPFTableCell ncell6 = row.getCell(6);
XWPFTableCell ncell7 = row.getCell(7);
XWPFTableCell ncell8 = row.getCell(8);
XWPFRun nxwpfRun0 = ncell0.getParagraphs().get(0).createRun();
nxwpfRun0.setText(i+1+"");
nxwpfRun0.setBold(true);
nxwpfRun0.setFontFamily("宋体");
nxwpfRun0.setFontSize(10);
XWPFRun nxwpfRun1 = ncell1.getParagraphs().get(0).createRun();
nxwpfRun1.setText(jianchabaogaoList.get(i).getXmBianhao()+"."+jianchabaogaoList.get(i).getJcxmName());
nxwpfRun1.setBold(true);
nxwpfRun1.setFontFamily("宋体");
nxwpfRun1.setFontSize(10);
XWPFRun nxwpfRun2 = ncell2.getParagraphs().get(0).createRun();
nxwpfRun2.setText(jianchabaogaoList.get(i).getNrBiaohao()+"."+jianchabaogaoList.get(i).getJcnrName());
nxwpfRun2.setFontFamily("宋体");
nxwpfRun2.setFontSize(10);
XWPFRun nxwpfRun3 = ncell3.getParagraphs().get(0).createRun();
nxwpfRun3.setText(jianchabaogaoList.get(i).getNrfjBianhao()+"."+jianchabaogaoList.get(i).getJcnrfjName());
nxwpfRun3.setFontFamily("宋体");
nxwpfRun3.setFontSize(10);
// XWPFRun nxwpfRun4 = ncell4.getParagraphs().get(0).createRun();
// nxwpfRun4.setText(jianchabaogaoList.get(i).getJcxmName());
// nxwpfRun4.setFontFamily("宋体");
// nxwpfRun4.setFontSize(10);
// XWPFRun nxwpfRun5 = ncell5.getParagraphs().get(0).createRun();
// nxwpfRun5.setText(jianchabaogaoList.get(i).getJcxmName());
// nxwpfRun5.setFontFamily("宋体");
// nxwpfRun5.setFontSize(10);
XWPFParagraph p = ncell4.addParagraph();
p.setAlignment(ParagraphAlignment.LEFT);
List<JianchaJieguoPic> picList = jianchabaogaoList.get(i).getPicList();
XWPFRun r = p.createRun();
r.setText(jianchabaogaoList.get(i).getJianchaqingkuang());
r.setFontFamily("宋体");
r.setFontSize(10);
r.addBreak();
XWPFParagraph xwpfParagraph2 = ncell4.addParagraph();
XWPFRun run1 = xwpfParagraph2.createRun();
xwpfParagraph2.setAlignment(ParagraphAlignment.CENTER);
for (int j = 0; j< picList.size(); j++) {
int format;
String imgFile="D:\\jianchazhicheng\\jianchapic\\"+picList.get(j).getSavepath();
File picture=new File(imgFile);
BufferedImage sourceImg=ImageIO.read(new FileInputStream(picture));
int width = sourceImg.getWidth();
int height = sourceImg.getHeight();
float num= (float)height/width;
DecimalFormat df = new DecimalFormat("0.00");//格式化小数
String s = df.format(num);
Double aDouble = Double.valueOf(s);
int a=150;
int b=(int) (aDouble * a);
if(imgFile.endsWith(".emf")) {
format = XWPFDocument.PICTURE_TYPE_EMF;
}
else if(imgFile.endsWith(".wmf")) {
format = XWPFDocument.PICTURE_TYPE_WMF;
}
else if(imgFile.endsWith(".pict")) {
format = XWPFDocument.PICTURE_TYPE_PICT;
}
else if(imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg")) {
format = XWPFDocument.PICTURE_TYPE_JPEG;
}
else if(imgFile.endsWith(".png")){
format = XWPFDocument.PICTURE_TYPE_PNG;
}
else if(imgFile.endsWith(".dib")){
format = XWPFDocument.PICTURE_TYPE_DIB;
}
else if(imgFile.endsWith(".gif")){
format = XWPFDocument.PICTURE_TYPE_GIF;
}
else if(imgFile.endsWith(".tiff")){
format = XWPFDocument.PICTURE_TYPE_TIFF;
}
else if(imgFile.endsWith(".eps")){
format = XWPFDocument.PICTURE_TYPE_EPS;
}
else if(imgFile.endsWith(".bmp")) {
format = XWPFDocument.PICTURE_TYPE_BMP;
}
else if(imgFile.endsWith(".wpg")){
format = XWPFDocument.PICTURE_TYPE_WPG;
}
else {
System.err.println("Unsupported picture: " + imgFile +
". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
continue;
}
run1.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(a), Units.toEMU(b)); // 200x200 pixels
}
run1.addBreak(BreakType.PAGE);
XWPFParagraph paragraphs = ncell6.getParagraphs().get(0);
XWPFParagraph xwpfParagraph = ncell5.getParagraphs().get(0);
XWPFRun run = xwpfParagraph.createRun();
// run.setText("不");
// run.addBreak();
// run.setText("符");
// run.addBreak();
// run.setText("合");
// run.addBreak();
run.setFontFamily("宋体");
run.setFontSize(10);
List<Jianchayiju> jianchayijuList = jianchabaogaoList.get(i).getJianchayijuList();
for (int j = 0; j < jianchayijuList.size(); j++) {
XWPFRun runJianhcayiju = paragraphs.createRun();
runJianhcayiju.setFontFamily("宋体");
runJianhcayiju.setFontSize(10);
runJianhcayiju.setText((j+1)+".《"+jianchayijuList.get(j).getTitle()+"》第"+jianchayijuList.get(j).getItemId()+"条 "+jianchayijuList.get(j).getContent());
runJianhcayiju.addBreak();
runJianhcayiju.addBreak();
}
XWPFParagraph paragraphs7 = ncell7.getParagraphs().get(0);
List<Chufayiju> chufayijus = jianchabaogaoList.get(i).getChufayijus();
for (int j = 0; j < chufayijus.size(); j++) {
XWPFRun runJianhcayiju = paragraphs7.createRun();
runJianhcayiju.setFontFamily("宋体");
runJianhcayiju.setFontSize(10);
runJianhcayiju.setText((j+1)+".《"+chufayijus.get(j).getTitle()+"》第"+chufayijus.get(j).getItemId()+"条 "+chufayijus.get(j).getContent());
runJianhcayiju.addBreak();
runJianhcayiju.addBreak();
}
XWPFParagraph xwpfParagraph1 = ncell8.getParagraphs().get(0);
XWPFRun run2 = xwpfParagraph1.createRun();
run2.setText(jianchabaogaoList.get(i).getZhenggaicuoshi());
run2.setFontSize(10);
run2.setFontFamily("宋体");
}
shezhiSHuxing(xwpfTable);
mergeCellsHorizontal(xwpfTable,0,4,5);
mergeCellsHorizontal(xwpfTable,0,2,3);
for (int i = 0; i < integers.size(); i+=2) {
mergeCellsVertically(xwpfTable,1,integers.get(i)+1,integers.get(i+1)+1);
}
for (int i = 0; i < integers2.size(); i+=2) {
mergeCellsVertically(xwpfTable,2,integers2.get(i)+1,integers2.get(i+1)+1);
}
for (int i = 0; i < integers3.size(); i+=2) {
mergeCellsVertically(xwpfTable,3,integers3.get(i)+1,integers3.get(i+1)+1);
}
}
public void shezhiSHuxing (XWPFTable xwpfTable){
// 居中
List<XWPFTableRow> rowList = xwpfTable.getRows();
for(int i = 0; i < rowList.size(); i++) {
if (i==0){
XWPFTableRow infoTableRow = rowList.get(i);
List<XWPFTableCell> cellList = infoTableRow.getTableCells();
for(int j = 0; j < cellList.size(); j++) {
XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
cellParagraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun cellParagraphRun = cellParagraph.createRun();
}
}else{
XWPFTableRow infoTableRow = rowList.get(i);
List<XWPFTableCell> cellList = infoTableRow.getTableCells();
for(int j = 0; j < cellList.size(); j++) {
if (j==0||j==5){
XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
cellParagraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun cellParagraphRun = cellParagraph.createRun();
cellParagraphRun.setFontSize(12);
}
if (j!=0&&j!=4&j!=5){
XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
cellParagraph.setAlignment(ParagraphAlignment.LEFT);
XWPFRun cellParagraphRun = cellParagraph.createRun();
cellParagraphRun.setFontSize(12);
}
// if(j==4){
// XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
//
// cellParagraph.setAlignment(ParagraphAlignment.CENTER);
// }
}
}
}
List<XWPFTableRow> rows = xwpfTable.getRows();
for (int i = 0; i <rows.size() ; i++) {
CTTrPr ctTrPr = rows.get(i).getCtRow().addNewTrPr();
CTHeight ctHeight = ctTrPr.addNewTrHeight();
List<XWPFTableCell> tableCells = rows.get(i).getTableCells();
for (int j = 0; j < tableCells.size(); j++) {
CTTcPr ctTcPr = tableCells.get(j).getCTTc().addNewTcPr();
ctTcPr.addNewVAlign().setVal(STVerticalJc.CENTER);
}
}
}
//合并一列之中相同的
public static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
if (rowIndex == fromRow) {
// The first merged cell is set with RESTART merge value
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one, are set with CONTINUE
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
}
}
}
//合并一行之中相同的
public static void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
if ( cellIndex == fromCell ) {
// The first merged cell is set with RESTART merge value
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one, are set with CONTINUE
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
}
}