[英]Error in Deleting last rows Excel - Apache POI
I am trying to delete last 10 rows using Apache-POI. 我正在尝试使用Apache-POI删除最后10行。 I tried to find solutions online, no luck.
我试图在网上找到解决方案,但是没有运气。 My code:
我的代码:
import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.sl.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.*;
public class Sample1 {
public static void main(String[] args)throws Exception
{
File file = new File("File.xlsx");
FileInputStream fIP = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(fIP);
XSSFSheet sheet = wb.getSheetAt(1);
int lastRowNum = sheet.getLastRowNum()+1;
//XSSFRow removingRow = sheet.getRow(lastRowNum);
if(file.isFile() && file.exists())
{
for (int i = lastRowNum; i>= lastRowNum-10 ; i-- )
{
XSSFRow removingRow = sheet.getRow(i);
sheet.removeRow(removingRow);
}
}
else
{
System.out.println("Error in opening the file");
}
}
}
Error: 错误:
Exception in thread "main" java.lang.NullPointerException
线程“主”中的异常java.lang.NullPointerException
at org.apache.poi.xssf.usermodel.XSSFSheet.removeRow(XSSFSheet.java:1914) at Sample1.main(Sample1.java:27)在org.apache.poi.xssf.usermodel.XSSFSheet.removeRow(XSSFSheet.java:1914)在Sample1.main(Sample1.java:27)
Jar Files: Jar文件:
dom4J-1.6.1
poi-3.16
poi-3.17-beta
poi-ooxml-3.15.0
poi-ooxml-schemas-3.15.0
xmlbeans-2.6.0
commons-collections4-4.0
commons-collections4-4.1
commons-codec-1.10
commons-logging-1.2
maven-ant-tasks-2.1.3
Any help is appreciated. 任何帮助表示赞赏。
Thanks in advance. 提前致谢。
Off-by-one error: 一对一错误:
for (int i = lastRowNum; i>= lastRowNum-10 ; i-- )
should be: 应该:
for (int i = lastRowNum - 1; i>= Math.max(lastRowNum-10, 0) ; i-- )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.