Java使用poi实现excel的复制,下载,删除其中某一行
@Test
public void poiCaozuo(){
String filePath =System.getProperty("user.dir")+"\\resultFile\\HVAC分体空调与设备清单EDS2011-新风处理空调(最后"+UUID.randomUUID().toString()+").xlsx";
String templateFileName =System.getProperty("user.dir")+"\\templete\\HVAC分体空调与设备清单EDS2011-新风处理空调(最后99f1e8e8-0ceb-4eba-a141-752db75627b4).xlsx";
File fi=new File(filePath);
if (!fi.exists()){
try {
fi.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileInputStream fis = null;
XSSFWorkbook wb = null;
try {
fis = new FileInputStream(templateFileName);
wb = new XSSFWorkbook(fis);
XSSFSheet sheetAt = wb.getSheetAt(0);
sheetAt.shiftRows(4, sheetAt.getLastRowNum()-1, -1);
OutputStream out = new FileOutputStream(filePath);
wb.write(out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
总结:
这里的删除其实是向上覆盖几行或向下覆盖几行。
ShiftRows 是HSSFSheet工作薄的方法
ShiftRows(int startRow,int endRow,int n)
参数介绍:
startRow:开始行
endRow:末尾行
n:移动n行数startRow到endRow数据域(正数:向下移,负数:向上移)
使用:
1、工作薄中出现空行,想要删除该空行,假设空行的行号:rowIndex,
工作薄最后一行行号:LastRowIndex。
将rowIndex 到 LastRowIndex的数据移动向上移动一行,即将rowIndex行覆盖掉。
操作:ShiftRows(rowIndex-1,LastRowIndex,1)