Write data into excel file by using maven poi dependencies.

 package com.qa.tests;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.ss.usermodel.WorkbookFactory;

public class WriteDataExcel 

{

public static void main(String[] args) throws Exception

{

String str="D:\\excel\\Book1.xlsx";

FileInputStream file=new FileInputStream(str);

Workbook book = WorkbookFactory.create(file);

Sheet sheet = book.getSheetAt(1);

for(int i=1;i<=16;i++)

{

Row row = sheet.createRow(i);

for(int j=0;j<2;j++)

{

Cell cell = row.createCell(j);

if(j%2==0)

{

cell.setCellValue("username"+i);

}

else

{

cell.setCellValue("password"+i);

}

}

}

file.close();

FileOutputStream fis=new FileOutputStream(new File(str));

book.write(fis);

}

}

Output(After writing data into excel):

Comments

Popular posts from this blog

Run Selenium Tests on Chrome Browser

Collection Framework

Emulate Chrome Browser into mobile View by Using Selenium part-1