gpt4 book ai didi

android - 在excel中复制具有范围的行公式

转载 作者:行者123 更新时间:2023-11-29 00:53:11 24 4
gpt4 key购买 nike

我正在创建一个应用程序,在用户输入一些内容后,我会在其中生成一个 Excel 工作表。如果用户输入 5 条记录,我将处理初始/第一行(带公式的单元格),然后将每个值插入到单元格中。问题是在处理行之后,公式范围不会更新,例如。如果初始行是10th 并且单元格A 具有类似SUM(G10:K10) 的公式,那么在处理完10th 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);

最佳答案

如果您在A10中有一个初始公式,那么简单地使用VBA Copy 方法来填充A11:

Sub KopyFormula()
Dim A10 As Range, A11 As Range

Set A10 = Range("A10")
Set A11 = Range("A11")

A10.Copy A11
End Sub

enter image description here

关于android - 在excel中复制具有范围的行公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57557416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com