使用Java Apache POI在Excel中插入一行

时间:2022-01-31 20:25:39

I am developing desktop application relate to Excel sheets, so I had some problems in inserting rows between two rows, is there any possibility to do it in java using Apache POI

我正在开发与Excel表相关的桌面应用程序,因此在两行之间插入行时有一些问题,是否有可能使用Apache POI在java中实现这一点

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

//Reading the available rows using (sh.getRow(1))

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

//Here i need to insert second row (????)

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

//I have third row here which is already exist (sh.getRow(3))

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

1 个解决方案

#1


27  

I got the solution and which is working very well

我找到了解决方案,而且效果很好

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

Shift number of rows down the sheet .

向下移行数。

sh.shiftRows(2,rows,1);   

here

在这里

  • 2 -- At which we need to insert row
  • 2——插入行
  • rows -- Total rows
  • 行——总行
  • 1 -- How much rows we are going to insert

    1——插入多少行

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

    我们执行上述过程的原因是创建空行,然后只有我们才能创建一个新行

Now we shifted the rows , then we can do our stuffs

现在我们移动行,然后我们可以做我们的东西

Coding : sh.createRow(1);

编码:sh.createRow(1);

The above code used to insert row at 1 th position as we defined.

上面的代码用于将行插入到我们定义的第1位置。

#1


27  

I got the solution and which is working very well

我找到了解决方案,而且效果很好

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

Shift number of rows down the sheet .

向下移行数。

sh.shiftRows(2,rows,1);   

here

在这里

  • 2 -- At which we need to insert row
  • 2——插入行
  • rows -- Total rows
  • 行——总行
  • 1 -- How much rows we are going to insert

    1——插入多少行

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

    我们执行上述过程的原因是创建空行,然后只有我们才能创建一个新行

Now we shifted the rows , then we can do our stuffs

现在我们移动行,然后我们可以做我们的东西

Coding : sh.createRow(1);

编码:sh.createRow(1);

The above code used to insert row at 1 th position as we defined.

上面的代码用于将行插入到我们定义的第1位置。