Home > Back-end >  About EasyExcel merged cell methods how to implement
About EasyExcel merged cell methods how to implement

Time:12-09

Doing a business of export, is encountering a demand, the diagram below:


The import com. Alibaba. Excel. Metadata. CellData;
The import com. Alibaba. Excel. Metadata. The Head;
The import com. Alibaba. Excel. Write. Handler. CellWriteHandler;
The import com. Alibaba. Excel. Write. Metadata. Holder. WriteSheetHolder;
The import com. Alibaba. Excel. Write. Metadata. Holder. WriteTableHolder;
The import lombok. Data;
The import org, apache poi. Ss. Usermodel. *;
The import org, apache poi. Ss. Util. CellRangeAddress;

import java.util.List;

/* *
* cell merge strategy class
*
*/
@ Data
Public class ExcelFillCellMergeStrategy implements CellWriteHandler {
/* *
Which a few columns * fields need to merge
*/
Private int [] mergeColumnIndex;
/* *
* line from which started to merge
*/
Private int mergeRowIndex;

Public ExcelFillCellMergeStrategy () {
}

Public ExcelFillCellMergeStrategy (int mergeRowIndex, int [] mergeColumnIndex) {
Enclosing mergeRowIndex=mergeRowIndex;
Enclosing mergeColumnIndex=mergeColumnIndex;
}

@ Override
Public void beforeCellCreate (WriteSheetHolder WriteSheetHolder, WriteTableHolder WriteTableHolder, Row, Row,
The Head Head, Integer Integer, Integer integer1, Boolean aBoolean) {

}

@ Override
Public void afterCellCreate (WriteSheetHolder WriteSheetHolder, WriteTableHolder WriteTableHolder, Cell Cell,
The Head, Head, Integer Integer, Boolean aBoolean) {

Workbook Workbook=writeSheetHolder. GetSheet (.) getWorkbook ();
CellStyle CellStyle=workbook. CreateCellStyle ();
The Font cellFont=workbook. CreateFont ();
//bold
CellFont. SetBold (true);
//font size
CellFont. SetFontHeightInPoints (12) (short);
//center
CellStyle. SetAlignment (HorizontalAlignment. CENTER);
CellStyle. SetVerticalAlignment (VerticalAlignment. CENTER);
CellStyle. SetFont (cellFont);
Cell. SetCellStyle (cellStyle);
//set up automatic newline
CellStyle. SetWrapText (true);
}

@ Override
Public void afterCellDataConverted (WriteSheetHolder WriteSheetHolder, WriteTableHolder WriteTableHolder,
CellData CellData Cell, the Cell, the Head Head, Integer Integer, Boolean aBoolean) {

}

@ Override
Public void afterCellDispose (WriteSheetHolder WriteSheetHolder, WriteTableHolder WriteTableHolder,
List The list, the Cell Cell, Head Head, Integer Integer, Boolean aBoolean) {

//the current line
Int curRowIndex=cell. GetRowIndex ();
//the current column
Int curColIndex=cell. GetColumnIndex ();
If (curRowIndex & gt; MergeRowIndex) {
for (int i=0; I & lt; MergeColumnIndex. Length; I++) {
If (curColIndex==mergeColumnIndex [I]) {
MergeWithPrevRow (writeSheetHolder, cell, curRowIndex curColIndex);
break;
}
}
}

}

Private void mergeWithPrevRow (WriteSheetHolder WriteSheetHolder, Cell Cell, int curRowIndex, int curColIndex) {
//get the current row of the current data column and a row when the front column data, whether through a line of data on the same merge
Object curData ()===https://bbs.csdn.net/topics/cell.getCellTypeEnum CellType. The STRING? Cell. GetStringCellValue () :
Cell. GetNumericCellValue ();
Cell preCell=Cell. GetSheet (.) getRow (curRowIndex - 1) getCell (curColIndex);
Object preData ()===https://bbs.csdn.net/topics/preCell.getCellTypeEnum CellType. The STRING? PreCell. GetStringCellValue () :
PreCell. GetNumericCellValue ();

//compare the current row in the first column of the cell with a line are the same, the same combination of current cell line with a
//
If (curData. Equals (preData)) {
Sheet Sheet=writeSheetHolder. GetSheet ();
List MergeRegions=sheet. GetMergedRegions ();
Boolean isMerged=false;
for (int i=0; I & lt; MergeRegions. The size () & amp; & ! IsMerged; I++) {
CellRangeAddress cellRangeAddr=mergeRegions. Get (I);
//if a cell has been merged, first remove the original merger unit, add again merge cells
If (cellRangeAddr. IsInRange (curRowIndex - 1, curColIndex)) {
Sheet. RemoveMergedRegion (I);
CellRangeAddr. SetLastRow (curRowIndex);
Sheet. AddMergedRegion (cellRangeAddr);
IsMerged=true;
}
}
//if a cell is not on the merger, the new combined unit
if (! IsMerged) {
CellRangeAddress CellRangeAddress=new CellRangeAddress (curRowIndex - 1, curRowIndex curColIndex,
CurColIndex);
Sheet. AddMergedRegion (cellRangeAddress);
}
}
}
}
After the method derived form data will be like in the second case, but the customer actual demand is according to the terms of the number of rows to merge (i.e., a third of the figure), the second and third columns corresponding examination department and push state is a dynamic number of rows is not fixed, have easyExcel using skilled bosses how can help me with my writing combined method? Currently in 2.2.3 version and 3.17 poi

CodePudding user response:

Add: real environment can form data forms, there is no such extreme (each terms corresponding audit department, after all, most is not the same as well as the score is not the same), but if use the combined methods will have this kind of situation

CodePudding user response:

Top of the top of the roof
  • Related