Last active
June 9, 2020 10:28
-
-
Save faizakram/35ea191da593e88a26bf7f623d843bde to your computer and use it in GitHub Desktop.
Apache POI Row Copy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RowCopy { | |
public static void main(String[] args) throws Exception { | |
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream("E:/budget-1.xls")); | |
HSSFSheet sheet = workbook.getSheet("Budget"); | |
copyRow(workbook, sheet, 5, 8); | |
copyRow(workbook, sheet, 6, 9); | |
FileOutputStream out = new FileOutputStream("E:/output.xls"); | |
workbook.write(out); | |
out.close(); | |
} | |
private static void copyRow(HSSFWorkbook workbook, HSSFSheet worksheet, int sourceRowNum, int destinationRowNum) { | |
// Get the source / new row | |
HSSFRow newRow = worksheet.getRow(destinationRowNum); | |
HSSFRow sourceRow = worksheet.getRow(sourceRowNum); | |
// If the row exist in destination, push down all rows by 1 else create a new | |
// row | |
if (newRow != null) { | |
worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1); | |
} else { | |
newRow = worksheet.createRow(destinationRowNum); | |
} | |
// Loop through source columns to add to new row | |
for (int i = 0; i < sourceRow.getLastCellNum(); i++) { | |
// Grab a copy of the old/new cell | |
HSSFCell oldCell = sourceRow.getCell(i); | |
HSSFCell newCell = newRow.createCell(i); | |
// If the old cell is null jump to next cell | |
if (oldCell == null) { | |
continue; | |
} | |
// Copy style from old cell and apply to new cell | |
HSSFCellStyle newCellStyle = workbook.createCellStyle(); | |
newCellStyle.cloneStyleFrom(oldCell.getCellStyle()); | |
newCell.setCellStyle(newCellStyle); | |
// If there is a cell comment, copy | |
if (oldCell.getCellComment() != null) { | |
newCell.setCellComment(oldCell.getCellComment()); | |
} | |
// If there is a cell hyperlink, copy | |
if (oldCell.getHyperlink() != null) { | |
newCell.setHyperlink(oldCell.getHyperlink()); | |
} | |
// Set the cell data type | |
newCell.setCellType(oldCell.getCellType()); | |
// Set the cell data value | |
switch (oldCell.getCellType()) { | |
case BLANK:// Cell.CELL_TYPE_BLANK: | |
newCell.setCellValue(oldCell.getStringCellValue()); | |
break; | |
case BOOLEAN: | |
newCell.setCellValue(oldCell.getBooleanCellValue()); | |
break; | |
case FORMULA: | |
newCell.setCellFormula(oldCell.getCellFormula()); | |
break; | |
case NUMERIC: | |
newCell.setCellValue(oldCell.getNumericCellValue()); | |
break; | |
case STRING: | |
newCell.setCellValue(oldCell.getRichStringCellValue()); | |
break; | |
default: | |
break; | |
} | |
} | |
// If there are are any merged regions in the source row, copy to new row | |
for (int i = 0; i < worksheet.getNumMergedRegions(); i++) { | |
CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i); | |
if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) { | |
CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.getRowNum(), | |
(newRow.getRowNum() + (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow())), | |
cellRangeAddress.getFirstColumn(), cellRangeAddress.getLastColumn()); | |
worksheet.addMergedRegion(newCellRangeAddress); | |
} | |
} | |
} | |
} |
Author
faizakram
commented
Jun 9, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment