demand : It needs to realize the function of exporting course seat table according to customer needs

Several problems in the concrete realization :

* The first is the way of function realization , Which component is used to implement this module . This module uses Apache POI Component to implement . Available through maven To import related dependencies .
<dependency> <groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId> </dependency> <dependency>
<groupId>org.apache.poi</groupId> <artifactId>ooxml-schemas</artifactId>
<version>1.3</version> </dependency>
*
  The second is the problem of table drawing in development . Center table , Table cell property settings , Cell border removal , Adaptive display of cells according to the incoming content ( There is a problem that the horizontal center of cells fails after vertical display ), Cell merging .
* Third, data traversal display , adopt for Cyclic simplification of code and different effect processing of corresponding different data .
 

The implementation is as follows :
public class WordUtil { /** * Get download path * * @param filename File name */ public String
getAbsoluteFile(String filename) { String downloadPath =
Global.getDownloadPath() + filename; File desc = new File(downloadPath); if
(!desc.getParentFile().exists()) { desc.getParentFile().mkdirs(); } return
downloadPath; } /** * * @param table table * @param row Lines ( from 0 start ) * @param cell
Total number of columns * @return */ public static List<XWPFTableCell> getCellList(XWPFTable table,
int row, int cell,int classroomNo,int height,int width) { List<XWPFTableCell>
cellList = new ArrayList<XWPFTableCell>(); for (int i = 0; i < cell; i++) {
XWPFTableRow tableRow = table.getRow(row); XWPFTableCell xcell =
tableRow.getCell(i); CTTc cttc = xcell.getCTTc(); CTTcPr ctPr =
cttc.addNewTcPr(); ctPr.addNewVAlign().setVal(STVerticalJc.CENTER); // Center text
cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
// Remove border from first and second lines if (row==0||row==1|| (row==7&&classroomNo==1)||// Remove the borders from row 7 of classroom 1
(row==5&&classroomNo==1&&i==12)||(row==5&&classroomNo==1&&i==13)||// Yes, classroom one, No. 5, No 12 Row seat frame removal
(row==6&&classroomNo==1&&i==13)||(row==6&&classroomNo==1&&i==12)||(row==6&&classroomNo==1&&i==11)||(row==6&&classroomNo==1&&i==10)||
(row==8&&classroomNo==1&&i==13)) { CTTcBorders tblBorders =
ctPr.addNewTcBorders(); tblBorders.addNewLeft().setVal(STBorder.NIL);
tblBorders.addNewRight().setVal(STBorder.NIL);
tblBorders.addNewBottom().setVal(STBorder.NIL);
tblBorders.addNewTop().setVal(STBorder.NIL); } // Set row height
tableRow.setHeight(height); xcell.getVerticalAlignment(); // Set the width of each cell
CTTblWidth cellw = xcell.getCTTc().addNewTcPr().addNewTcW();
cellw.setType(STTblWidth.DXA); cellw.setW(BigInteger.valueOf(width));
cellList.add(xcell); } return cellList; } public static AjaxResult
createWord(int hang,int lie,List<List<String>> datas,int classroomNo,int
height,int width) throws IOException { XWPFDocument word = new XWPFDocument();
String path=Global.getDownloadPath() + " Student seat selection information .docx"; FileOutputStream out =
new FileOutputStream(new File(path));// Set table storage location XWPFTable table =
word.createTable(hang, lie);// Create a table , The form is :9 That's ok 10 column
table.getCTTbl().addNewTblPr().addNewJc().setVal(STJc.CENTER);// Set table center for
(int i = 0; i < datas.size(); i++) { List<XWPFTableCell> cellList =
getCellList(table, i, datas.get(i).size(),classroomNo,height,width); for (int j
= 0; j < cellList.size(); j++) { XWPFTableCell cell = cellList.get(j); String
text = datas.get(i).get(j); if(text.length()>0&&i>1) { XWPFRun run =
cell.addParagraph().createRun(); for (int k = 0; k < text.length(); k++) {
run.setText(datas.get(i).get(j).substring(k, k+1)); run.addBreak(); } }else {
cellList.get(j).setText(text); } } } // Civilized practice on the third floor if (classroomNo==1) {
mergeCellsVertically(table, 5, 2, 6); mergeCellsVertically(table, 9, 2, 6);
}else if (classroomNo==2) { mergeCellsVertically(table, 6, 2, 11);
mergeCellsVertically(table, 13, 2, 11); }else if (classroomNo==3) {
mergeCellsVertically(table, 3, 0, 9);// Merge the fourth column ( Subscripts are from 0 start )
mergeCellsVertically(table, 7, 0, 9);// Merge column 8 } word.write(out); out.close();
return AjaxResult.success(path); } /** * word Unit grid merge * * @param table form *
@param row Merge columns in rows * @param startCell Start column * @param endCell End column * @date 2020 year 4 month 8 day
afternoon 4:43:54 */ public static void mergeCellsHorizontal(XWPFTable table, int row,
int startCell, int endCell) { for (int i = startCell; i <= endCell; i++) {
XWPFTableCell cell = table.getRow(row).getCell(i); if (i == startCell) { // 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); } } } /**
* word Cell row merge * * @param table form * @param col Column of consolidated row * @param fromRow Start line *
@param toRow End line * @date 2020 year 4 month 8 day afternoon 4:46:18 */ public static void
mergeCellsVertically(XWPFTable table, int col, int startRow, int endRow) { for
(int i = startRow; i <= endRow; i++) { XWPFTableCell cell =
table.getRow(i).getCell(col); if (i == startRow) { // 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); } } } }
 
WordUtil util = new WordUtil(); try { List<List<String>> datas=new
ArrayList<List<String>>(); // datas.add(Arrays.asList("", "", "", "", " main ", " seat ",
" platform ", "", "", "")); // datas.add(Arrays.asList("", "6", "4", "", "2", "1", "5",
"", "6", "7")); // datas.add(Arrays.asList(" first row ", "", "", " passageway ", "", "", "",
" passageway ", "", "")); // datas.add(Arrays.asList(" Second row ", " Yang Wei ", " Wanhanlong ", "", "", " Wanhanlong ",
"", "", " Wanhanlong ", " Wanhanlong ")); // datas.add(Arrays.asList(" Third row ", " Wanhanlong ", " Wanhanlong ", "", "",
" Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ")); // datas.add(Arrays.asList(" Fourth row ", " Wanhanlong ",
" Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ")); //
datas.add(Arrays.asList(" Row five ", " Wanhanlong ", "", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ",
" Wanhanlong ")); // datas.add(Arrays.asList(" Row six ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ",
" Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ")); // datas.add(Arrays.asList(" Row seven ", " Wanhanlong ", " Wanhanlong ", "",
" Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ")); // datas.add(Arrays.asList(" Eighth row ",
" Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ")); //
util.createWord(10,10,datas,3,1220,980); // datas.add(Arrays.asList("", "", "",
"", "", "", " main ", " seat ", " platform ", "","", "", "", "")); // datas.add(Arrays.asList("",
"10", "8", "6", "4", "", "2", "1", "3", "", "5", "7", "9", "11")); //
datas.add(Arrays.asList(" first row ", "", "", "", "", " passageway ", "", "", "", " passageway ", " Wanhanlong ",
" Wanhanlong ", "", " Wanhanlong ")); // datas.add(Arrays.asList(" Second row ", " Yang Wei ", " Wanhanlong ", " Wanhanlong ",
" Wanhanlong ", " Wanhanlong ", "", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ")); //
datas.add(Arrays.asList(" Third row ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "",
" Wanhanlong ", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ")); // datas.add(Arrays.asList(" Fourth row ",
" Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "",
"")); // datas.add(Arrays.asList(" Row five ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ",
"", " Wanhanlong ", " Wanhanlong ", "", "", "", "")); // datas.add(Arrays.asList("", "", "", "",
"", "", "", "", "", "", "", "", "", "")); // datas.add(Arrays.asList(" Row six ",
" Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "",
"")); // util.createWord(9,14,datas,1,1220,980); datas.add(Arrays.asList("",
"", "", "", "", "", "", " main ", "", " seat ","", " platform ", "", "", "","", "", "", ""));
datas.add(Arrays.asList("", "14", "12", "10", "8", "6", "", "4", "2", "1", "3",
"5", "7", "", "9","11", "13", "15", "17")); datas.add(Arrays.asList(" first row ", "",
"", "", "", "", " passageway ", "", "", "", " Wanhanlong ", " Wanhanlong ", "", " passageway ", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ",
"", " Wanhanlong ")); datas.add(Arrays.asList(" Second row ", " Yang Wei ", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ",
"", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong "));
datas.add(Arrays.asList(" Third row ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "",
" Wanhanlong ", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong "));
datas.add(Arrays.asList(" Fourth row ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "",
" Wanhanlong ", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong "));
datas.add(Arrays.asList(" Row five ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "",
" Wanhanlong ", " Wanhanlong ", "", "", "", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong "));
datas.add(Arrays.asList(" Row six ", "", "", "", "", "", "", "", "", "", "", "", "",
"", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ")); datas.add(Arrays.asList(" Row seven ", " Wanhanlong ",
" Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", "", " Wanhanlong ",
" Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ")); datas.add(Arrays.asList(" Eighth row ", " Wanhanlong ", " Wanhanlong ", "",
" Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", "", " Wanhanlong ", " Wanhanlong ",
" Wanhanlong ", "", " Wanhanlong ")); datas.add(Arrays.asList(" Row 9 ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ",
" Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "",
" Wanhanlong ")); datas.add(Arrays.asList(" Row 10 ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ",
"", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", "", " Wanhanlong ", " Wanhanlong ", " Wanhanlong ", "", " Wanhanlong "));
util.createWord(12,19,datas,2,220,980); } catch (IOException e) {
e.printStackTrace(); }
  The actual export rendering is as follows :

 

 

 

 

Technology
©2019-2020 Toolsou All rights reserved,
Mybatis Error resolution :There is no getter for property named '*' in 'class Java.lang.String Big data tells you , How tired are Chinese women Message quality platform series | Full link troubleshooting Gude Haowen serial - You deserve to be an engineer ( Preface ) Image explanation of over fitting and under fitting Springboot of JPA Common query methods JAVA Detailed explanation of anomalies vue Of v-if And v-show The difference between python To solve the problem of dictionary writing list in Codeup——601 | problem A: task scheduling