POI shiftRows方法

public void ShiftRows(int startRow, int endRow, int n);
public void ShiftRows(int startRow, int endRow, int n, bool copyRowHeight, bool resetOriginalRowHeight);
public void ShiftRows(int startRow, int endRow, int n, bool copyRowHeight, bool resetOriginalRowHeight, bool moveComments);
        //Parameters:
        //startRow - the row to start shifting(从下标为startRow的行开始移动)
        //endRow - the row to end shifting(到下标为endRow的行结束移动)
        //n - the number of rows to shift(有多少行需要移动)
        //copyRowHeight - whether to copy the row height during the shift
        //resetOriginalRowHeight - whether to set the original row's height to the default

 

如:

sheet.shiftRows(insertRow, sheet.getLastRowNum(),1,true,false);
                row=sheet.createRow(insertRow);

 

先把insertRow到最后一行向下移动一行,在移动后的insertRow处(移动后留的空白),新建一个新行,这样就实现了数据行的插入

 

转载于:https://my.oschina.net/stringxy/blog/863256

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常抱歉,我的回答是错误的。在JavaPOI库中,确实有一个名为`shiftRows()`的方法,可以用于移动行和删除行。以下是删除第一行的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class DeleteRowExample { public static void main(String[] args) { try { // 读取工作簿 FileInputStream fis = new FileInputStream(new File("example.xlsx")); Workbook workbook = new XSSFWorkbook(fis); // 获取第一个工作表 Sheet sheet = workbook.getSheetAt(0); // 删除第一行 sheet.shiftRows(1, sheet.getLastRowNum(), -1); // 保存工作簿 FileOutputStream fos = new FileOutputStream(new File("example.xlsx")); workbook.write(fos); fos.close(); System.out.println("Row deleted successfully!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上述代码中,我们首先使用`FileInputStream`读取了一个名为`example.xlsx`的工作簿,并使用`XSSFWorkbook`创建了一个`Workbook`对象。然后,我们获取了第一个工作表,并使用`shiftRows()`方法删除了第一行。最后,我们使用`FileOutputStream`将修改后的工作簿保存到磁盘上。 请注意,`shiftRows()`方法的第一个参数是要移动的起始行索引,第二个参数是要移动的结束行索引,第三个参数是要移动的行数。在我们的例子中,我们将从第二行开始的所有行向上移动了一行,因此第一个参数是1,第二个参数是工作表中的最后一行,第三个参数是-1。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值