Last active
February 3, 2020 17:46
-
-
Save mwshubham/ff700c997a06c2a8a84966f7f0d6f4c3 to your computer and use it in GitHub Desktop.
Android Kotlin generate excel sheet file example sample
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
private fun generateExcel() { | |
try { | |
val workbook = HSSFWorkbook() | |
val spreadSheet = workbook.createSheet("Sell Report") | |
/*Header "Sampe..."*/ | |
val rowA = spreadSheet.createRow(0) | |
val cellAA = rowA.createCell(0) | |
cellAA.setCellValue(HSSFRichTextString("Sampe")) | |
cellAA.setCellStyle( | |
workbook.createCellStyle().apply { | |
alignment = CellStyle.ALIGN_CENTER | |
wrapText = true | |
setFont(workbook.createFont().apply { | |
boldweight = HSSFFont.BOLDWEIGHT_BOLD | |
}) | |
}) | |
spreadSheet.addMergedRegion(CellRangeAddress( | |
rowA.rowNum, | |
rowA.rowNum, | |
0, | |
/*index start from 0 and 1 additional as there will be total block for total qty per user... */ | |
products.size + 1 | |
)) | |
vehicles.forEachIndexed { vehicleIdx, vehicleBean -> | |
/*first vehicleDetailRow + index (0...N) * 2*/ | |
val vehicleDetailRow = spreadSheet.createRow(2 + vehicleIdx * (users.size + 4)) | |
val vehicleDetailCell = vehicleDetailRow.createCell(0) | |
vehicleDetailCell.setCellValue(HSSFRichTextString("${vehicleBean.vehicle_no} (${vehicleBean.driver_name})")) | |
ExcelStyleUtils.applyCenterAlignedBold(workbook, vehicleDetailCell) | |
spreadSheet.addMergedRegion(CellRangeAddress( | |
vehicleDetailRow.rowNum, | |
vehicleDetailRow.rowNum, | |
0, | |
products.size + 1 | |
)) | |
val productDetailsRow = spreadSheet.createRow(vehicleDetailRow.rowNum + 1) | |
products.forEachIndexed { productIdx, productBean -> | |
val productDetailsCell = productDetailsRow.createCell(1 + productIdx) | |
productDetailsCell.setCellValue(HSSFRichTextString(productBean.product_code)) | |
ExcelStyleUtils.applyCenterAlignedBold(workbook, productDetailsCell) | |
spreadSheet.setColumnWidth(productDetailsCell.columnIndex, 3000) | |
} | |
/*Total cell at the end of product list...*/ | |
val totalQtyPerUserCell = productDetailsRow.createCell(products.size + 1) | |
totalQtyPerUserCell.setCellValue(HSSFRichTextString("TOTAL")) | |
ExcelStyleUtils.applyCenterAlignedBold(workbook, totalQtyPerUserCell) | |
spreadSheet.setColumnWidth(totalQtyPerUserCell.columnIndex, 3000) | |
users.forEachIndexed { userIdx, userBean -> | |
val userDetailsRow = spreadSheet.createRow(productDetailsRow.rowNum + 1 + userIdx) | |
val userDetailsCell = userDetailsRow.createCell(0) | |
userDetailsCell.setCellValue(HSSFRichTextString("${userBean.firm_name.capitalize()} (${userBean.full_name})")) | |
ExcelStyleUtils.applyCenterAlignedBold(workbook, userDetailsCell) | |
spreadSheet.setColumnWidth(userDetailsCell.columnIndex, 8000) | |
val productQtyMap = HashMap<String, Int>() | |
orders.filter { | |
it.vehicle_id == vehicleBean.id && it.userId == userBean.id | |
} | |
.forEach { | |
it.items.forEach { | |
productQtyMap[it.product_id] = (productQtyMap[it.product_id] | |
?: 0).plus(it.qty) | |
} | |
} | |
products.forEachIndexed { productIdx, productBean -> | |
productQtyMap[productBean.id]?.let { | |
val productQtyPerUserCell = userDetailsRow.createCell(1 + productIdx) | |
productQtyPerUserCell.setCellValue(it.toDouble()) | |
ExcelStyleUtils.applyCenterAligned(workbook, productQtyPerUserCell) | |
spreadSheet.setColumnWidth(productQtyPerUserCell.columnIndex, 3000) | |
} | |
} | |
val totalProductQtyPerUserCell = userDetailsRow.createCell(products.size + 1) | |
totalProductQtyPerUserCell.cellType = HSSFCell.CELL_TYPE_FORMULA | |
totalProductQtyPerUserCell.cellFormula = "SUM(${CellReference.convertNumToColString(1)}${totalProductQtyPerUserCell.row.rowNum + 1}:${CellReference.convertNumToColString(products.size)}${totalProductQtyPerUserCell.row.rowNum + 1})" | |
ExcelStyleUtils.applyCenterAlignedBold(workbook, totalProductQtyPerUserCell) | |
spreadSheet.setColumnWidth(totalProductQtyPerUserCell.columnIndex, 3000) | |
} | |
/*Total cell at the end of user list...*/ | |
val totalQtyPerProductRow = spreadSheet.createRow(productDetailsRow.rowNum + users.size + 1) | |
val totalQtyPerProductCell = totalQtyPerProductRow.createCell(0) | |
totalQtyPerProductCell.setCellValue(HSSFRichTextString("TOTAL")) | |
ExcelStyleUtils.applyCenterAlignedBold(workbook, totalQtyPerProductCell) | |
spreadSheet.setColumnWidth(totalQtyPerProductCell.columnIndex, 8000) | |
products.forEachIndexed { productIdx, productBean -> | |
val totalProductQtyForAllUserCell = totalQtyPerProductRow.createCell(1 + productIdx) | |
totalProductQtyForAllUserCell.cellType = HSSFCell.CELL_TYPE_FORMULA | |
/*note rowNum is -1 the original excel sheet representaion as it is 0-Based index...*/ | |
totalProductQtyForAllUserCell.cellFormula = "SUM(${CellReference.convertNumToColString(totalProductQtyForAllUserCell.columnIndex)}${productDetailsRow.rowNum + 2}:${CellReference.convertNumToColString(totalProductQtyForAllUserCell.columnIndex)}${productDetailsRow.rowNum + users.size + 1})" | |
ExcelStyleUtils.applyCenterAlignedBold(workbook, totalProductQtyForAllUserCell) | |
spreadSheet.setColumnWidth(totalProductQtyForAllUserCell.columnIndex, 3000) | |
} | |
} | |
var fos: FileOutputStream? = null | |
fos.use { | |
try { | |
val str_path = Environment.getExternalStorageDirectory().toString() | |
val file: File | |
file = File(str_path, getString(R.string.app_name) + ".xls") | |
fos = FileOutputStream(file) | |
workbook.write(fos) | |
Toast.makeText(context, "Excel Sheet Generated: ${file.path}", Toast.LENGTH_LONG).show() | |
} catch (e: IOException) { | |
e.printStackTrace() | |
} | |
} | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
} |
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
Download Apache POI file | |
poi-3.9-20121203.jar [LATEST VERSION ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment