Created
December 20, 2013 12:12
-
-
Save epochcoder/8053932 to your computer and use it in GitHub Desktop.
this method is meant as an aid to help convert complex html tables to POI (Excel). it accepts the current sheet and row, but keeps count of where your next column will be, colspans and rowspans are included, this method takes advantage of addMergedRegion(...)
This file contains hidden or 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
/** | |
* adds a new cell to the current excel sheet with the HTML translated col- and row spans. | |
* @param sheet the current HSSFSheet | |
* @param row the current HSSFRow. | |
* @param style the style for this cell | |
* @param value the cell value. | |
* @param rowNum the current row number. | |
* @param colNum the current column number. | |
* @param rowSpan should there be a row-span here. | |
* @param colSpan should there be a col-span here. | |
* @return the next column index. | |
*/ | |
private static int addCell(HSSFSheet sheet, HSSFRow row, HSSFCellStyle style, String value, | |
int rowNum, int colNum, int rowSpan, int colSpan) { | |
if (row == null) { | |
return -1; | |
} | |
final boolean shouldRowSpan = rowSpan > 1; | |
final boolean shouldColSpan = colSpan > 1; | |
// new column number | |
int updatedColumn = colNum; | |
HSSFCell cell = row.createCell(updatedColumn++); | |
if (style != null) { | |
// set the cell style | |
cell.setCellStyle(style); | |
} | |
// set the value to show | |
cell.setCellValue(value); | |
// calculate the spans | |
if (shouldRowSpan || shouldColSpan) { | |
// add the row- and colspans | |
sheet.addMergedRegion(new CellRangeAddress( | |
(rowNum - 1), (rowNum - 1) + (shouldRowSpan ? rowSpan - 1 : 0), | |
(updatedColumn - 1), (updatedColumn - 1) + (shouldColSpan ? colSpan - 1 : 0) | |
)); | |
if (shouldColSpan) { | |
// increment updated column according to colspan | |
updatedColumn += (colSpan - 1); | |
} | |
} | |
return updatedColumn; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment