【问题标题】:Apache POI: Why Is a Row Missing When I use shiftRows?Apache POI:为什么我使用 shiftRows 时缺少一行?
【发布时间】:2019-04-18 18:22:03
【问题描述】:

我正在移动 Excel 工作表中的行并在工作表的开头插入新行。但是,无论我移动和插入多少行,我最终似乎都比我应该的少了一行。

import org.apache.poi.ss.usermodel.Row
import   Row.MissingCellPolicy._
import org.apache.poi.ss.usermodel.Sheet
import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.ss.util.CellRangeAddress
import org.apache.poi.ss.util.WorkbookUtil.createSafeSheetName
import org.apache.poi.xssf.usermodel.XSSFWorkbook

def shiftAndInsertRow(sheet: Sheet) = {

  val rowInsertionPoint = 0

  // shift all the rows down
  val lastRowNum = sheet.getLastRowNum
  println(s"Last row is $lastRowNum")

  val debugRow1 = sheet.getRow(rowInsertionPoint)
  val debugCell1 = debugRow1.getCell(0)
  // let's get a play-by-play of what's being attempted
  println(s"Current value in row $rowInsertionPoint is " +
      s"${debugCell1.getNumericCellValue}")
  println(s"Shifting rows $rowInsertionPoint and below down one row")
  sheet.shiftRows(rowInsertionPoint, lastRowNum, 1, true, true)
  val debugRow2 = sheet.getRow(rowInsertionPoint + 1)
  val debugCell2 = debugRow2.getCell(0)
  println(s"Current value in row ${rowInsertionPoint + 1} is now " +
      s"${debugCell2.getNumericCellValue}")

  println(s"Creating new row at $rowInsertionPoint in sheet")
  // create the new row
  val newRow = sheet.createRow(rowInsertionPoint)
  // set the field ID of the row
  val newCell = newRow.getCell(0, CREATE_NULL_AS_BLANK)
  println(s"Inserting value $lastRowNum at $rowInsertionPoint in sheet")
  newCell.setCellValue(lastRowNum)
  println()

}

val workbook = new XSSFWorkbook()
val sheet = workbook.createSheet(createSafeSheetName("Test 1"))
val rowNum = 0
val cellValue = -1

println(s"Creating new row at $rowNum in sheet")
// create the new row
val row = sheet.createRow(rowNum)
// set the field ID of the row
val cell = row.getCell(0, CREATE_NULL_AS_BLANK)
println(s"Inserting value $cellValue at $rowNum in sheet")
cell.setCellValue(cellValue)
println()
// insert a second row
shiftAndInsertRow(sheet)
// and a third
shiftAndInsertRow(sheet)
workbook.write(new java.io.FileOutputStream("out/test.xlsx"))

上面的代码创建了一个只有两行而不是三行的电子表格。我错过了什么?

【问题讨论】:

    标签: scala apache-poi


    【解决方案1】:

    我认为您的代码很好,在我看来这是 apache-poi 中的错误。它适用于我的 3.17 版本,但如果我升级到 4.0.0,它就会中断。

    据我所知,行号已正确更新,但参考 (cell.getReference) 未正确更新。

    我建议尝试查找该错误是否已在此处https://bz.apache.org/bugzilla/buglist.cgi?product=POI 报告,如果没有,请提交新的错误报告。

    与此同时,您也许可以尝试这种似乎对我有用的解决方法。它在电子表格的每个单元格上调用updateCellReferencesForShifting

    import scala.collection.JavaConverters._
    for {
      row <- sheet.rowIterator().asScala.toList
      cell <- row.cellIterator().asScala.toList
    } yield cell.asInstanceOf[XSSFCell].updateCellReferencesForShifting("")
    

    在您致电shiftRows 之后立即放置此代码块。不能保证它不会破坏其他东西,所以请谨慎使用!

    【讨论】:

    • 似乎 4.x 不稳定。我发现了另一个问题。
    猜你喜欢
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 2014-04-29
    相关资源
    最近更新 更多