XSSFSheet.shiftRows 错误
XSSFSheet.shiftRows error
我想将 shiftRows()
用于 xlsx 文件,但我收到以下错误消息,即使我已正确导入 "org.apache.poi.ss.formula.FormulaShifter;"
Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: org.apache.poi.ss.formula.FormulaShifter.createForRowShift(IIII)Lorg/apache/poi/ss/formula/FormulaShifter;
shiftRows()
确实移动了我的文件的行;但在那之后,程序不会 运行 其他命令。
代码如下:
import java.util.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import java.io.InputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.ss.formula.FormulaShifter;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.dom4j.DocumentException;
import java.nio.file.Files;
import java.nio.file.*;
XSSFWorkbook wb1;
InputStream inp1 = new FileInputStream("/home/directory/Desktop/arx.xlsx");
wb1 = new XSSFWorkbook(inp1);
XSSFSheet sheet = wb1.getSheetAt(0);
copyRow(wb1,sheet,5,2);
FileOutputStream fileOut = new FileOutputStream("/home/directory/Desktop/arx.xlsx");
wb1.write(fileOut);
fileOut.close();
private static void copyRow(XSSFWorkbook workbook, XSSFSheet worksheet, int sourceRowNum, int destinationRowNum) {
// Get the source / new row
XSSFRow newRow = worksheet.getRow(destinationRowNum);
XSSFRow 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
XSSFCell oldCell = sourceRow.getCell(i);
XSSFCell newCell = newRow.createCell(i);
// If the old cell is null jump to next cell
/*if (oldCell == null) {
newCell = null;
continue;
}*/
// 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;
}
}
}
正在提升对答案的评论...
Can I mix POI jars from different versions?
No. This is not supported.
All POI jars in use must come from the same version. A combination such as poi-3.11.jar
and poi-ooxml-3.9.jar
is not supported, and will fail to work in unpredictable ways.
您需要确保您所有的 POI jar 都来自同一版本,然后一切正常
我想将 shiftRows()
用于 xlsx 文件,但我收到以下错误消息,即使我已正确导入 "org.apache.poi.ss.formula.FormulaShifter;"
Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: org.apache.poi.ss.formula.FormulaShifter.createForRowShift(IIII)Lorg/apache/poi/ss/formula/FormulaShifter;
shiftRows()
确实移动了我的文件的行;但在那之后,程序不会 运行 其他命令。
代码如下:
import java.util.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import java.io.InputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.ss.formula.FormulaShifter;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.dom4j.DocumentException;
import java.nio.file.Files;
import java.nio.file.*;
XSSFWorkbook wb1;
InputStream inp1 = new FileInputStream("/home/directory/Desktop/arx.xlsx");
wb1 = new XSSFWorkbook(inp1);
XSSFSheet sheet = wb1.getSheetAt(0);
copyRow(wb1,sheet,5,2);
FileOutputStream fileOut = new FileOutputStream("/home/directory/Desktop/arx.xlsx");
wb1.write(fileOut);
fileOut.close();
private static void copyRow(XSSFWorkbook workbook, XSSFSheet worksheet, int sourceRowNum, int destinationRowNum) {
// Get the source / new row
XSSFRow newRow = worksheet.getRow(destinationRowNum);
XSSFRow 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
XSSFCell oldCell = sourceRow.getCell(i);
XSSFCell newCell = newRow.createCell(i);
// If the old cell is null jump to next cell
/*if (oldCell == null) {
newCell = null;
continue;
}*/
// 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;
}
}
}
正在提升对答案的评论...
Can I mix POI jars from different versions?
No. This is not supported.
All POI jars in use must come from the same version. A combination such as
poi-3.11.jar
andpoi-ooxml-3.9.jar
is not supported, and will fail to work in unpredictable ways.
您需要确保您所有的 POI jar 都来自同一版本,然后一切正常