【发布时间】:2019-08-19 13:08:49
【问题描述】:
我正在创建一个应用程序,在该应用程序中我在用户输入一些输入后生成一个 Excel 工作表。 如果用户输入 5 条记录,我将处理初始/第一行(带有公式的单元格),然后在单元格中插入每个值。问题是在处理行之后,公式范围没有被更新,例如。如果初始行是 10th 并且单元格 A 有一个类似 SUM(G10:K10) 的公式,那么在处理完第 10th 行之后strong> 到 11th 公式保持不变,即 SUM(G10:K10) 而不是 SUM(G11:K11)。 谁能帮我解决这个问题。这是代码
HSSFSheet existingSheet = existingWorkBook.getSheetAt(0);
ExcelFile.copyRow(existingWorkBook, existingSheet, startRowColumn - 2, startRowColumn - 1);
public 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) {
newCell = 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 Cell.CELL_TYPE_BLANK:
newCell.setCellValue(oldCell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN:
newCell.setCellValue(oldCell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_ERROR:
newCell.setCellErrorValue(oldCell.getErrorCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
newCell.setCellFormula(oldCell.getCellFormula());
break;
case Cell.CELL_TYPE_NUMERIC:
newCell.setCellValue(oldCell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
newCell.setCellValue(oldCell.getRichStringCellValue());
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);
}
}
}
处理一行后,下面是插入单元格值的代码。
Iterator rowIter = existingSheet.rowIterator();
while (rowIter.hasNext()) {
HSSFRow oldRow = (HSSFRow) rowIter.next();
if (rowCounterOld >= startRowColumn - 2 && !isRecordInserted) {
if (configFieldsIndexes != null && configFieldsIndexes.size() > 0) {
for (Map.Entry entry : configFieldsIndexes.entrySet()) {
for (Map.Entry<String, String> entry1 : valueMap.entrySet()) {
if (((Map.Entry) entry1).getKey().toString().equalsIgnoreCase(entry.getKey().toString())) {
try {
float value = Float.valueOf(entry1.getValue());
oldRow.getCell(ExcelFile.getExcelColumnNumber(entry.getValue().toString())).setCellValue(value);
} catch (NumberFormatException e) {
e.printStackTrace();
oldRow.getCell(ExcelFile.getExcelColumnNumber(entry.getValue().toString())).setCellValue(entry1.getValue());
}
break;
}
}
}
isRecordInserted = true;
lastInsertedRow = rowCounterOld;
}
}
rowCounterOld++;
//rowCounterNew++;
}
HSSFFormulaEvaluator.evaluateAllFormulaCells(existingWorkBook);
【问题讨论】:
-
请添加所有相关代码
-
查看stackoverflow.com/questions/47594254/… 了解
copyFormula方法。
标签: android excel-formula apache-poi