使用 Java Apache POI 在 Excel 中插入一行 [英] Insert a Row in Excel Using Java Apache POI

查看:140
本文介绍了使用 Java Apache POI 在 Excel 中插入一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发与 Excel 工作表相关的桌面应用程序.我在两行之间插入行时遇到了一些问题.是否有可能使用 Apache POI 在 Java 中执行此操作?

I am developing a desktop application related to Excel sheets. I have some problems inserting rows between two rows. Is there any possibility to do this in Java using Apache POI?

Workbook wb3=WorkbookFactory.create(new FileInputStream("Book1.xls"));
Sheet sh=wb3.getSheet("sheet1");

//使用(sh.getRow(1))读取可用行

//这里我需要插入第二行(????)

//我这里有第三行已经存在(sh.getRow(3))

推荐答案

我有一个非常有效的解决方案:

I have a solution which is working very well:

Workbook wb3=WorkbookFactory.create(new FileInputStream("Book1.xls"));
Sheet sh=wb3.getSheet("sheet1");  
int rows=sh.getLastRowNum();

向下移动工作表的行数.

Shift the number of rows down the sheet.

sh.shiftRows(2,rows,1);   

这里

  • 2 -- 我们需要插入行的位置
  • rows -- 总行数
  • 1 -- 我们要插入多少行
  • 2 -- Position at which we need to insert row
  • rows -- Total rows
  • 1 -- How many rows we are going to insert

我们之所以做上面的过程,是为了做一个空行;只有这样我们才能创建一个新行.

The reason why we are doing the above process is to make an empty row; only then can we create a new row.

现在我们移动了行,然后我们可以做我们的事情

编码:

sh.createRow(1);

上面的代码用于在我们定义的第一个位置插入一行.

The above code is used to insert a row at the 1st position, as we defined.

这篇关于使用 Java Apache POI 在 Excel 中插入一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆