POI operating form delete lines include two methods:
1. Sheet.removerow (ROW ROW); (POI3.17, tried POI3.8, there is a problem, POI4.0 does not support Java6 and Java7)
This method mainly uses and clear the content of the line, and also retains the location of the line
Common method:
/**
* Starting from a certain line, clear the fixed line (clear style)
* @param sheet
* @param Startrow
* @param endRow
*/
Public Static Void Removerow (XSSFSHEET Sheet, Int Startrow, Int Endrow) {
for (int j = endRow; j> = startrow; j--) {
sheet.removerow (sheet.getrow (j));
}
}
2. sheet.shiftRows(int startRow, int endRow, final int n, boolean copyRowHeight, boolean resetOriginalRowHeight);
Parameter introduction: Startrow: Starting EndRow: Ending N: Copyrowheight: Whether to copy the high when the class is changed
resetoriginalRowheight: Whether the original height is set to the default value
Main role: Together with the selected row (N is negative) or downward (n is positive) to cover the original line to achieve deletion operation
Public method:
/**
* Starting from a certain line, clear the fixed line (completely deleted), move up
* @param sheet
* @param Startrow
* @param endRow
*/
Public Static Void Deleterow (XSSFSHEET Sheet, Int Startrow, int Rowindex) {
int Lastropnum = Sheet.getlastropnum ();
if (StartRow <LastRownum) {
sheet.shiftRows (Startrow, LastRownum, -Rowindex, TRUE, FALSE);
}
}
/**
* Starting from a certain line, clear the fixed line (completely deleted), move down
* @param sheet
* @param Startrow
* @param endRow
*/
Public Static Void Deleterow1 (XSSFSHEET Sheet, Int Startrow, Int Rowindex) {
int Lastropnum = Sheet.getlastropnum ();
if (StartRow <LastRownum) {
Sheet.shiftRows (Startrow, LastRownum, Rowindex, TRUE, FALSE);
}
}
It needs to be noted that if the row of upward or downward and the lines that to be covered also contains the merger cell at the same position, you need to remove the merged cell before shiftRows, otherwise it will be reported to the consolidated conflict of the cell.
Remove the merger cells can be used with sheet.removeMergedRegion (int int index);
Public use method:
/**
* Clear the merged cells (delete columns)
* @param sheet
* @param StartRow Start Start Start from 0
* @param endRow ends
* @param StartColumn Start Column
* @param endcolumn end column
*/
Public Static Void RemoveMergedColregion (XSSFSHEET Sheet, Int Startrow, Int Endrow, Int StartColumn, Int Endcolumn)
{{
int sheetmergecount = sheet.getnummergedReds (); // Get all cells
int index = 0; // Used to save the cell grid number to be removed
for (int i = 0; I <SHEETMERGECOUNT; I ++) {
Cellrangeaddress CA = Sheet.getMergedRegion (i); // Get the i -i cell
int FIRSTCOLUMN = CA.GetfirstColumn ();
int LastColumn = CA.Getlastcolumn ();
int FIRSTROW = CA.GetfirstRow ();
int Lastrow = ca.getlastrow ();
if (fIRSTROW == Startrow && Lastrow == endrow) {
if (fIRSTCOLUMN == StartColumn && LastColumn == Endcolumn) {
Index = i;
sheet.removemerkygedRegion (index); // Remove the merger cell
Break;
}
}
}
}
/**
* Delete the merger cell (horizontal merger), delete all the mergers in the row
* @param sheet
* @param Row Starting
* @param column merger start column
*/
Public Static Void RemoveMerGEDROWROGION
{{
for (int j = startrow; j <= endrow; j ++) {
int sheetmergecount = sheet.getnummergedReds (); // Get all the merger cells
int index = 0; // Used to save the cell grid number to be removed
for (int i = 0; I <SHEETMERGECOUNT; I ++) {
Cellrangeaddress CA = Sheet.getMergedRegion (i); // Get the i -i cell
int FIRSTCOLUMN = CA.GetfirstColumn ();
int LastColumn = CA.Getlastcolumn ();
int FIRSTROW = CA.GetfirstRow ();
int Lastrow = ca.getlastrow ();
if (FIRSTROW> = Startrow && Lastrow <= endrow) {
if (column> = firstcolumn && colorn <= LastColumn) {
Index = i;
sheet.removemerkygedRegion (index); // Remove the merger cell
Break;
}
}
}
}
}
later, if you need to merge the unit grid, use the following method
// merging unit grid
Cellrangeaddress Region1 = New CellRageaddress (3, Removems-2, (SHORT) 7, (SHORT) 7);
// Parameter 1: Starting Parameter 2: Termination Parameter 3: Start Section 4: Termination column
Sheet.addmergedRegion (region1);