1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| package com.yhksxt.util; import java.io.IOException; import java.math.BigInteger; import javax.servlet.http.HttpServletResponse; import org.apache.poi.xwpf.usermodel.ParagraphAlignment; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth; public class ExprotSTUtil { public void exportSTToWord(String titleName,String[][] list ,HttpServletResponse response) throws IOException{
XWPFDocument document = new XWPFDocument();
XWPFParagraph titleParagraph = document.createParagraph();
titleParagraph.setAlignment(ParagraphAlignment.CENTER); XWPFRun titleParagraphRun = titleParagraph.createRun(); titleParagraphRun.setText(titleName); titleParagraphRun.setColor("000000"); titleParagraphRun.setFontSize(20);
int row = list.length +2; int column = list[0].length +3; XWPFTable table = document.createTable(row,column); setTableWidth(table, "10000"); mergeCellsVertically(table, 0, 0, 1); mergeCellsVertically(table, 2, 0, 1); mergeCellsVertically(table, column-1, 0, 1); for(int i=0;i<row-2;i++){ mergeCellsHorizontal(table, i, 0, 1); mergeCellsHorizontal(table, i, 2, 4); } mergeCellsHorizontal(table, 0, 5, column-2); mergeCellsHorizontal(table, row-2, 0, 4); mergeCellsHorizontal(table, row-1, 0, 4); mergeCellsHorizontal(table, row-1, 5, column-1); XWPFTableRow rowIndex = table.getRow(0); XWPFTableCell cell = rowIndex.getCell(0); cell.setText(list[0][0]); XWPFTableCell cell1 = rowIndex.getCell(2); cell1.setText(list[0][1]); XWPFTableCell cell2 = rowIndex.getCell(5); cell2.setText("试题类型及题量"); XWPFTableCell cell3 = rowIndex.getCell(column-1); cell3.setText(list[0][list[0].length-1]); XWPFTableRow rowIndex1 = table.getRow(1); for(int j=5,i=2;j<column-1;j++,i++){ XWPFTableCell cell4 = rowIndex1.getCell(j); cell4.setText(list[0][i]); } for(int i = 2,ii=1;i<row-1;i++,ii++){ XWPFTableRow rowIndex2 = table.getRow(i); for(int j = 0,jj=0;j<column-1 ;j++){ XWPFTableCell cell4 =null; if(j==0){ cell4 =rowIndex2.getCell(j); cell4.setText(list[ii][jj]); jj++; }if(j==2){ cell4 =rowIndex2.getCell(j); cell4.setText(list[ii][jj]); jj++; }else if(j>=5){ cell4 =rowIndex2.getCell(j); cell4.setText(list[ii][jj]); jj++; } } } XWPFTableRow rowIndex3 = table.getRow(row-1); XWPFTableCell cell5 =rowIndex3.getCell(0); cell5.setText("其他需要说明的问题"); document.write(response.getOutputStream()); }
private 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 ) { cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART); } else { cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE); } } }
private 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 ) { cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART); } else { cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE); } } }
private void setTableWidth(XWPFTable table,String width){ CTTbl ttbl = table.getCTTbl(); CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr(); CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW(); CTJc cTJc=tblPr.addNewJc(); cTJc.setVal(STJc.Enum.forString("center")); tblWidth.setW(new BigInteger(width)); tblWidth.setType(STTblWidth.DXA); } }
|