1

I know i am missing the main function here. I want to copy/ write only particular row from my source excel to destination excel using apache POI.

eg. My source excel has 10 rows. I need only 5th row to be copied to my destination excel.

My class,

public class Test1 {
    public static void main(String[] args) throws Exception{
        File srcFile=new File("C:\\Test\\Read.xlsx");
        FileInputStream fis=new FileInputStream(srcFile);

        XSSFWorkbook wb=new XSSFWorkbook(fis);
        XSSFSheet sheet1=wb.getSheetAt(0);

        File desFile=new File("C:\\Test\\Write.xlsx");
        FileOutputStream fout=new FileOutputStream(desFile);

        wb.write(fout);

        wb.close();     
    }
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Ram
  • 11
  • 1
  • 2

1 Answers1

0

As per the Apache POI docs, you can use the XSSFSheet.getRow(int) method to get a row at a specific index.

// 5th row
Row row = sheet.getRow(4);

To add this row to a new workbook, you'll have to iterate through each cell in the row object and set the value of cells in the new workbook to these values. An example can be found here.

zacran
  • 855
  • 6
  • 18