[英]XSSFSheet.shiftRows error
我想對xlsx文件使用shiftRows()
,但是即使導入了“ org.apache.poi.ss.formula.FormulaShifter;”,我也會收到以下錯誤消息。 適當地
線程“ 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;
}
}
}
正在對答案發表評論...
我可以混合不同版本的POI罐子嗎?
否。不支持此功能。
所有使用中的POI罐子必須來自同一版本。 不支持諸如
poi-3.11.jar
和poi-ooxml-3.9.jar
類的組合,它們將無法以不可預測的方式工作。
您需要確保所有POI jar都來自相同版本,然后一切正常
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.